Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

wjService.js 13KB

1
  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),_wjDataDecode=require("./wjDataDecode"),_wjDataDecode2=_interopRequireDefault(_wjDataDecode);function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}var reality_indicateServiceId,reality_notifyServiceId,reality_writeServiceId,reality_services,ListenFlag=void 0,DataListenerCallBack=void 0,connectedDeviceId=void 0,timeID=void 0,TAG_FUNCTION="function",sendCallback=void 0,TimerID=null,handshake=!1,services="0000FEE7-0000-1000-8000-00805F9B34FB",retdata="";function reallyScanConnect(c){var a={},l=(_wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/"),[]);wx.openBluetoothAdapter({success:function(e){wx.startBluetoothDevicesDiscovery({services:[],success:function(e){wx.onBluetoothDeviceFound(function(e){for(var i=0;i<e.devices.length;i++){for(var t=!1,o=0;o<l.length;o++)if(e.devices[i].deviceId==l[o].deviceId){t=!0;break}if(_wjUtils2.default.showLog("connectedDeviceName",e.devices[i].name),0==t){l.push(e.devices[i]);var n=e.devices[i].name.toUpperCase();if(n.includes("ETC"))if("break"===function(){connectedDeviceId=e.devices[i].deviceId,_wjUtils2.default.showLog("connectedDeviceId:"+connectedDeviceId);var t={};return t.device_name=n,t.device_id=connectedDeviceId,wx.stopBluetoothDevicesDiscovery({success:function(e){null!=timeID&&(clearTimeout(timeID),timeID=null),_wjUtils2.default.showLog("停止扫描,开始连接"),reallyConnect(t,function(e){(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(e)})},fail:function(e){_wjUtils2.default.showError("stopBluetoothDevicesDiscovery fail",e),a.serviceCode=1103,a.serviceInfo="stopBluetoothDevicesDiscovery fail",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)}}),"break"}())break}}})},fail:function(e){_wjUtils2.default.showError("startBluetoothDevicesDiscovery fail",e),a.serviceCode=1102,a.serviceInfo="startBluetoothDevicesDiscovery fail",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)}})},fail:function(e){_wjUtils2.default.showError("openBluetoothAdapter fail",e),a.serviceCode=1101,a.serviceInfo="openBluetoothAdapter fail",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)}}),timeID=setTimeout(function(){wx.closeBluetoothAdapter(),wx.stopBluetoothDevicesDiscovery(),_wjUtils2.default.showLog("scan timeout fail"),a.serviceCode=-13,a.serviceInfo="scan timeout fail",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)},5e3)}function reallyConnect(e,t){_wjUtils2.default.showLog("/***********Runing :: Do reallyConnect() begin *************/");var i={},o=(o=e.name||e.device_name).toUpperCase();_wjUtils2.default.showLog("name",o),o.includes("ETC")?_fullResetBluetooth(function(){_startNewConnection(e,t)}):(i.serviceCode=1104,i.serviceInfo="device name error",(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(i))}function _fullResetBluetooth(e){_resetBleState(),connectedDeviceId?wx.closeBLEConnection({deviceId:connectedDeviceId,complete:function(){_closeAndReopenBluetooth(e)}}):_closeAndReopenBluetooth(e)}function _closeAndReopenBluetooth(t){wx.closeBluetoothAdapter({complete:function(){setTimeout(function(){wx.openBluetoothAdapter({success:function(e){_wjUtils2.default.showLog("蓝牙初始化成功"),(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t()},fail:function(e){_wjUtils2.default.showError("蓝牙初始化失败",e),(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t()}})},1e3)}})}function _startNewConnection(t,i){var o=2<arguments.length&&void 0!==arguments[2]?arguments[2]:0,n={};_onBLEConnectionStateChange(),connectedDeviceId=t.deviceId||t.device_id,wx.createBLEConnection({deviceId:connectedDeviceId,success:function(e){_wjUtils2.default.showLog("已连接,开始使能服务:"+connectedDeviceId),setTimeout(function(){_enableService(function(e){0==e.serviceCode?(_wjUtils2.default.showLog("已连接,并使能成功"),(n=e).serviceInfo="已连接,并使能成功!",(void 0===i?"undefined":_typeof(i))==TAG_FUNCTION&&i(n)):o<2?(_wjUtils2.default.showLog("服务使能失败,正在进行第"+(o+1)+"次重试"),_fullResetBluetooth(function(){_startNewConnection(t,i,o+1)})):((n=e).serviceInfo="连接成功,但服务使能失败!",(void 0===i?"undefined":_typeof(i))==TAG_FUNCTION&&i(n))})},500)},fail:function(e){o<2?(_wjUtils2.default.showLog("连接失败,正在进行第"+(o+1)+"次重试"),_fullResetBluetooth(function(){_startNewConnection(t,i,o+1)})):(_wjUtils2.default.showError("createBLEConnection fail",e),n.serviceCode=1105,n.serviceInfo="createBLEConnection fail",(void 0===i?"undefined":_typeof(i))==TAG_FUNCTION&&i(n))}}),null!=timeID&&clearTimeout(timeID),timeID=setTimeout(function(){o<2?(_wjUtils2.default.showLog("连接超时,正在进行第"+(o+1)+"次重试"),_fullResetBluetooth(function(){_startNewConnection(t,i,o+1)})):(wx.closeBluetoothAdapter(),_wjUtils2.default.showError("conn timeout fail"),n.serviceCode=-13,n.serviceInfo="conn timeout fail!",(void 0===i?"undefined":_typeof(i))==TAG_FUNCTION&&i(n))},5e3)}function _enableService(c){_wjUtils2.default.showLog("/***********Runing :: Do _enableService() begin *************/");var a={};wx.getBLEDeviceServices({deviceId:connectedDeviceId,success:function(e){for(var t=0;t<e.services.length;t++){var i=e.services[t].uuid;_wjUtils2.default.showLog("serviceuuid:"+i),i==services&&(reality_services=i,wx.getBLEDeviceCharacteristics({deviceId:connectedDeviceId,serviceId:reality_services,success:function(e){var t=0;reality_indicateServiceId="";for(var i=0;i<e.characteristics.length;i++){var o=e.characteristics[i].uuid,n=e.characteristics[i].properties;_wjUtils2.default.showLog("chauuid:"+o),n.indicate?(reality_indicateServiceId=o,t++):n.notify?(reality_notifyServiceId=o,t++):n.write&&(reality_writeServiceId=o,t++)}""==reality_indicateServiceId&&(reality_indicateServiceId=reality_notifyServiceId),t<2?(a.serviceCode=1110,a.serviceInfo="getBLEDeviceCharacteristics temp<2!",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)):wx.notifyBLECharacteristicValueChange({deviceId:connectedDeviceId,serviceId:reality_services,characteristicId:reality_indicateServiceId,state:!0,success:function(e){_SetDataListenerCallBack(!0,datacallback),_onBLECharacteristicValueChange(),null!=timeID&&(clearTimeout(timeID),timeID=null);var t=setTimeout(function(){handshake||(_wjUtils2.default.showLog("握手超时3.5秒,自动认定为连接成功"),handshake=!0)},3500),i=setInterval(function(){handshake&&(console.log("握手认证成功"),handshake=!1,a.serviceCode=0,a.serviceInfo="enable success!",clearInterval(i),clearTimeout(t),console.log("握手成功之后",a.serviceCode),(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION)&&c(a)},5)},fail:function(){a.serviceCode=1109,a.serviceInfo="notifyBLECharacteristicValueChange fail!",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)}})},fail:function(){a.serviceCode=1108,a.serviceInfo="getBLEDeviceCharacteristics fail!",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)}}))}},fail:function(){a.serviceCode=1107,a.serviceInfo="getBLEDeviceServices fail!",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(a)}})}function _onBLEConnectionStateChange(){try{wx.offBLEConnectionStateChange()}catch(e){_wjUtils2.default.showLog("移除状态监听器失败:",e)}wx.onBLEConnectionStateChange(function(e){_wjUtils2.default.showLog("device "+e.deviceId+" state has changed, connected: "+e.connected),0==e.connected&&wx.closeBLEConnection({deviceId:e.deviceId,success:function(e){_wjUtils2.default.showLog("closeBLEConnection:",e),_resetBleState()},complete:function(){wx.closeBluetoothAdapter({success:function(e){_wjUtils2.default.showLog("closeBluetoothAdapter",e)}})}})})}function _resetBleState(){handshake=ListenFlag=!1,(reality_services=reality_writeServiceId=reality_notifyServiceId=reality_indicateServiceId=connectedDeviceId=DataListenerCallBack=null)!=TimerID&&(clearTimeout(TimerID),TimerID=null),null!=timeID&&(clearTimeout(timeID),timeID=null)}function reallyDisConnect(t){var i={};null!=connectedDeviceId?(wx.closeBLEConnection({deviceId:connectedDeviceId,success:function(e){_wjUtils2.default.showLog("closeBLEConnection:",e),i.serviceCode=0,i.serviceInfo="断开连接成功",_resetBleState(),(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(i)},fail:function(e){_wjUtils2.default.showError("closeBLEConnection fail:",JSON.stringify(e)),i.serviceCode=-1,i.serviceInfo="断开连接失败",(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(i)}}),wx.offBLECharacteristicValueChange(),console.log("移除特征值变化监听成功")):(i.serviceCode=-1,i.serviceInfo="没有已连接的设备",(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(i))}function _writeBLECharacteristicValue(e,t){var i={};wx.writeBLECharacteristicValue({deviceId:connectedDeviceId,serviceId:reality_services,characteristicId:reality_writeServiceId,value:e,success:function(e){_wjUtils2.default.showLog("writeBLECharacteristicValue success",e.errMsg),i.serviceCode=0,i.serviceInfo=e.errMsg,(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(i)}})}function _onBLECharacteristicValueChange(){wx.onBLECharacteristicValueChange(function(e){_wjUtils2.default.showLog(e.deviceId+" 'characteristic has changed"),console.log("rev:"+e),e.characteristicId===reality_indicateServiceId&&1==ListenFlag&&(void 0===DataListenerCallBack?"undefined":_typeof(DataListenerCallBack))==TAG_FUNCTION&&DataListenerCallBack(e.value)})}function _SetDataListenerCallBack(e,t){1==e?(ListenFlag=!0,DataListenerCallBack=t):0==e&&(ListenFlag=!1)}function datacallback(e){_SetDataListenerCallBack(!0,datacallback),_ReceiveData(e)}function _ReceiveData(e){var t,i={},o=_wjUtils2.default.getProtocolType();0==o?t=_wjDataDecode2.default.decode_GB(e):1==o&&(t=_wjDataDecode2.default.decode_WX(e)),0===t.serviceCode&&(o=t.serviceData.strData,"2711"===t.serviceData.strData.slice(8,12)&&52===t.serviceData.strData.length||"2713"===t.serviceData.strData.slice(8,12)?("2711"===t.serviceData.strData.slice(8,12)&&52===t.serviceData.strData.length&&(handshake=!0),null!=TimerID&&(clearTimeout(TimerID),TimerID=null),_SetDataListenerCallBack(!1),i.serviceCode=0,i.serviceInfo="数据接收成功",i.serviceData=o,console.log("result_DataInteract",i),retdata=i.serviceData):null!=(e=PackageDecode(o))?(_wjUtils2.default.showLog(TimerID,"接收数据成功,取消时钟"),null!=TimerID&&(clearTimeout(TimerID),TimerID=null),_SetDataListenerCallBack(!1),i.serviceCode=0,i.serviceInfo="数据接收成功",i.serviceData={},i.serviceData.dataBuff=e):(_wjUtils2.default.showError(TimerID,"数据解析失败,取消时钟"),null!=TimerID&&(clearTimeout(TimerID),TimerID=null),wjService._SetDataListenerCallBack(!1),i.serviceCode=-1,i.serviceInfo="数据解析失败"),(void 0===sendCallback?"undefined":_typeof(sendCallback))==TAG_FUNCTION)&&sendCallback(i)}function PackageDecode(e){return _wjUtils2.default.showLog("WJPackageDecode:",e),null}function _StartSendData(a,e){var l={},r=0,s=0;null!=TimerID&&(clearTimeout(TimerID),TimerID=null),_wjDataDecode2.default.init(),"fe0100164e2300030a06080012024f4b100018002000"==a?_SetDataListenerCallBack(!1):(TimerID=setTimeout(pro_timeout,1e4),_wjUtils2.default.showLog(TimerID,"开启时钟"),_SetDataListenerCallBack(!0,datacallback)),sendCallback=e,function t(){var e="";40*s>=a[r].length&&(r++,s=0);console.log("currentsegment",s,"currentPackage",r,a);r<a.length&&(e=a[r].length>=40*(s+1)?a[r].substring(40*s,40*(s+1)):a[r].substring(40*s,a[r].length));console.log("currentsegment",s,"currentPackage",r,a);if(""==e)_wjUtils2.default.showLog("数据发送完毕!"),"fe0100164e2300030a06080012024f4b100018002000"==a[l.serviceCode=0]&&(void 0===sendCallback?"undefined":_typeof(sendCallback))==TAG_FUNCTION&&sendCallback(l);else{for(var i=_wjUtils2.default.hexStr2byteArray(e),o=new ArrayBuffer(i.byteLength),n=new DataView(o),c=0;c<i.byteLength;c++)n.setUint8(c,i[c]);_wjUtils2.default.showLog("发送数据:",e),_writeBLECharacteristicValue(o,function(e){0==e.serviceCode?(s++,t()):(_wjUtils2.default.showError(TimerID,"数据发送失败,取消时钟"),null!=TimerID&&(clearTimeout(TimerID),TimerID=null),_SetDataListenerCallBack(!1),l.serviceCode=-2,l.serviceInfo="数据发送失败",(void 0===sendCallback?"undefined":_typeof(sendCallback))==TAG_FUNCTION&&sendCallback(l))})}}()}function pro_timeout(){var e={};_wjUtils2.default.showError(TimerID,"数据接收超时,取消时钟"),null!=TimerID&&(clearTimeout(TimerID),TimerID=null),_SetDataListenerCallBack(!1),e.serviceCode=-3,e.serviceInfo="数据接收超时",(void 0===sendCallback?"undefined":_typeof(sendCallback))==TAG_FUNCTION&&sendCallback(e)}module.exports={reallyConnect:reallyConnect,reallyScanConnect:reallyScanConnect,reallyDisConnect:reallyDisConnect,_writeBLECharacteristicValue:_writeBLECharacteristicValue,_SetDataListenerCallBack:_SetDataListenerCallBack,_enableService:_enableService};