1 |
- "use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_wjUtils=require("./wjUtils.js"),_wjUtils2=_interopRequireDefault(_wjUtils),_wjService=require("./wjService.js"),_wjService2=_interopRequireDefault(_wjService),_wjDataInteract=require("./wjDataInteract.js"),_wjDataInteract2=_interopRequireDefault(_wjDataInteract),_wjDataEncode=require("./wjDataEncode.js"),_wjDataEncode2=_interopRequireDefault(_wjDataEncode);function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}var timeID,TAG_FUNCTION="function",SDK_VERSION="WJ_BLESDK_WX_GZ_V1.0.0_20250303";function connectDevice(e,o){e&&e.deviceId?_wjService2.default.reallyConnect(e,function(e){0===e.serviceCode?(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:0,msg:e.serviceInfo||"连接成功"}):(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:-1,msg:e.serviceInfo||"连接失败"})}):(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:-1,msg:"设备参数无效,请重新选择设备"})}function scanDevice(s){var a={},u=[];wx.openBluetoothAdapter({success:function(e){wx.startBluetoothDevicesDiscovery({services:[],success:function(e){wx.onBluetoothDeviceFound(function(e){for(var o=0;o<e.devices.length;o++){for(var t=!1,n=0;n<u.length;n++)if(e.devices[o].deviceId==u[n].deviceId){t=!0;break}var i=e.devices[o].name.toUpperCase();if(0==t&&null!=i&&(u.push(e.devices[o]),_wjUtils2.default.showLog("connectedDeviceName:"+i),i.includes("ETC"))){null!=timeID&&(clearTimeout(timeID),timeID=null);var c=e.devices[o].deviceId,d=(_wjUtils2.default.showLog("connectedDeviceId:"+c),{});d.deviceId=c,d.name=i,wx.stopBluetoothDevicesDiscovery({success:function(e){wx.hideLoading(),_wjUtils2.default.showLog("停止扫描,开始连接"),connectDevice(d,function(e){console.log("连接结果:",e),0===e.code?(a.code=0,a.msg="设备名称:"+d.name+" "+e.msg,console.log("设备名称:"+d.name+" "+e.msg)):(a.code=-1,a.msg=e.msg),(void 0===s?"undefined":_typeof(s))==TAG_FUNCTION&&s(a)})},fail:function(e){wx.hideLoading(),_wjUtils2.default.showError("stopBluetoothDevicesDiscovery fail",e),(void 0===s?"undefined":_typeof(s))==TAG_FUNCTION&&s({code:-1,msg:"停止扫描失败"})}});break}}}),timeID=setTimeout(function(){wx.closeBluetoothAdapter(),_wjUtils2.default.showError("conn timeout fail"),(void 0===s?"undefined":_typeof(s))==TAG_FUNCTION&&s({code:-1,msg:"连接超时"}),wx.hideLoading()},5e3)},fail:function(e){_wjUtils2.default.showError("startBluetoothDevicesDiscovery fail",e),(void 0===s?"undefined":_typeof(s))==TAG_FUNCTION&&s({code:-1,msg:"开始扫描失败"})}})},fail:function(){wx.closeBluetoothAdapter({success:function(e){var o="蓝牙适配器未开启";_wjUtils2.default.showError(o),(void 0===s?"undefined":_typeof(s))==TAG_FUNCTION&&s({code:-1,msg:o})}}),wx.hideLoading(),wx.showModal({title:"提示",content:"蓝牙开关未开启或者手机不支持蓝牙功能!"})}})}function disconnectDevice(o){stayBleOn(function(e){console.log("保持蓝牙开启状态",e),_wjService2.default.reallyDisConnect(function(e){0===e.serviceCode?(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:0,msg:"已断连"}):(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:-1,msg:e.serviceInfo||"断开连接失败"})})})}function init(e){console.log("SDK_VERSION",SDK_VERSION),(void 0===e?"undefined":_typeof(e))==TAG_FUNCTION&&e({code:0,data:SDK_VERSION,msg:"初始化成功"})}function getDeviceInfo(o){var e="",t=_wjUtils2.default.getCMD_TYPE();0==t?e="810100C0":1==t?e="A501C0":2==t&&(e="C5040080020200"),t=_wjDataEncode2.default.encode(e),_wjDataInteract2.default._StartSendData(t.serviceData.dataEncode,function(e){0==e.serviceCode?(console.log("设备信息res",e),_wjUtils2.default.showLog("获取设备信息成功"),(void 0===o?"undefined":_typeof(o))==TAG_FUNCTION&&o({code:0,data:_wjUtils2.default.hexToString(e.serviceData.rawData.substring(8)),msg:"获取设备信息成功"})):(void 0===o?"undefined":_typeof(o))==TAG_FUNCTION&&o({code:-1,msg:e.serviceInfo||"获取设备信息失败"})})}function stayBleOn(o){var e=_wjDataEncode2.default.encode("AE0101");_wjDataInteract2.default._StartSendData(e.serviceData.dataEncode,function(e){0==e.serviceCode?(console.log("保持蓝牙开启状态",e),_wjUtils2.default.showLog("保持蓝牙开启状态成功"),(void 0===o?"undefined":_typeof(o))==TAG_FUNCTION&&o({code:0,data:e.serviceData.rawData,msg:"保持蓝牙开启状态成功"})):(void 0===o?"undefined":_typeof(o))==TAG_FUNCTION&&o({code:-1,msg:e.serviceInfo||"保持蓝牙开启状态失败"})})}function transCommand(e,o,t){var n="00",i={};e="01"+parseInt(e.length/2).toString(16).padStart(2,"0")+e,e=_wjDataEncode2.default.encode(e,n,o),_wjDataInteract2.default._StartSendData(e.serviceData.dataEncode,function(e){i=(0==(i=e).serviceCode?_wjUtils2.default.showLog("APDU透传指令成功"):_wjUtils2.default.showLog("APDU透传指令失败"),e),(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(i)})}function scanBle(o,t){var n=[],i=null;t&&"function"==typeof t.onScanResult&&"function"==typeof t.onIsEnable?wx.openBluetoothAdapter({success:function(e){t.onIsEnable({code:0,msg:"蓝牙状态正常"}),wx.startBluetoothDevicesDiscovery({services:[],success:function(e){wx.onBluetoothDeviceFound(function(e){e.devices.forEach(function(o){!n.some(function(e){return e.deviceId===o.deviceId})&&o.name&&(n.push(o),t.onScanResult(n))})}),i=setTimeout(function(){wx.stopBluetoothDevicesDiscovery(),clearTimeout(i)},o||5e3)},fail:function(e){t.onScanResult([]),console.error("扫描蓝牙设备失败:",e)}})},fail:function(e){t.onIsEnable({code:-1,msg:"蓝牙状态异常"}),wx.showModal({title:"提示",content:"请检查手机蓝牙是否正常开启"})}}):console.error("回调函数未正确设置")}function closBluetooth(o){wx.stopBluetoothDevicesDiscovery({success:function(e){(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:0,msg:"蓝牙搜索已停止"})},fail:function(e){(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:-1,msg:"停止蓝牙搜索失败"})}})}function transCmd(e,t,n){if("10"==t)t="A3";else{if("20"!=t)return void((void 0===n?"undefined":_typeof(n))===TAG_FUNCTION&&n({code:-2,msg:"cmdtype 类型不支持"}));t="AC"}var i,c;Array.isArray(e)&&0!==e.length?(i=0,c=[],function o(){i>=e.length?(void 0===n?"undefined":_typeof(n))===TAG_FUNCTION&&n({code:0,data:c,msg:"执行成功"}):transCommand(e[i],t,function(e){0===e.serviceCode?(c.push(e.serviceData.dataBuff.substring(4)),i++,o()):(void 0===n?"undefined":_typeof(n))===TAG_FUNCTION&&n({code:-1,msg:e.serviceInfo||"指令执行失败",data:c})})}()):(void 0===n?"undefined":_typeof(n))===TAG_FUNCTION&&n({code:-1,msg:"指令集参数无效"})}function disconnectDeviceBluetooth(o){_wjService2.default.reallyDisConnect(function(e){(void 0===o?"undefined":_typeof(o))===TAG_FUNCTION&&o({code:0,msg:"已断连"})})}module.exports={scanDevice:scanDevice,connectDevice:connectDevice,init:init,disconnectDevice:disconnectDevice,getDeviceInfo:getDeviceInfo,scanBle:scanBle,closBluetooth:closBluetooth,transCmd:transCmd,disconnectDeviceBluetooth:disconnectDeviceBluetooth};
|