You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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);function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}var connectedDeviceId,timeID,onDisconn,services="0000fee7-0000-1000-8000-00805f9b34fb",notifyServiceId="0000fec8-0000-1000-8000-00805f9b34fb",writeServiceId="0000fec7-0000-1000-8000-00805f9b34fb",services_IOS="FEE7",notifyServiceId_IOS="FEC8",writeServiceId_IOS="FEC7",reality_notifyServiceId=void 0,reality_writeServiceId=void 0,reality_services=void 0,TAG_FUNCTION="function";function reallyConnect(a,s){var c={},r=(console.log("/***********Runing :: Do reallyConnect() begin *************/"),onDisconn=s,[]);my.closeBluetoothAdapter(),my.openBluetoothAdapter({success:function(e){my.startBluetoothDevicesDiscovery({services:[],success:function(e){_onBLEConnectionStateChanged(),my.onBluetoothDeviceFound(function(e){for(var c=0;c<e.devices.length;c++){for(var i=!1,n=0;n<r.length;n++)if(e.devices[c].deviceId==r[n].deviceId){i=!0;break}var o=e.devices[c].name;if(console.log("connectedDeviceName",o),0==i&&(""!=o||null!=o)&&(r.push(e.devices[c]),-1!=o.indexOf("WJ")||-1!=o.indexOf("WanJi"))){my.offBluetoothDeviceFound(),connectedDeviceId=e.devices[c].deviceId;var t={};t.name=o,t.deviceId=connectedDeviceId,my.stopBluetoothDevicesDiscovery({success:function(e){console.log("停止扫描,开始连接");_connectDevice(t,function(e){(void 0===a?"undefined":_typeof(a))==TAG_FUNCTION&&a(e)},function(e){(void 0===s?"undefined":_typeof(s))==TAG_FUNCTION&&s(e)})}});break}}})},fail:function(e){console.log("scanerror:"+e),c.code=10024,c.msg="scanerror fail!",(void 0===a?"undefined":_typeof(a))==TAG_FUNCTION&&a(c)}}),timeID=setTimeout(function(){my.stopBluetoothDevicesDiscovery(),my.offBluetoothDeviceFound(),console.log("scan timeout"),c.code=-3,c.msg="scan timeout fail!",(void 0===a?"undefined":_typeof(a))==TAG_FUNCTION&&a(c)},5e3)},fail:function(e){console.log("openadapter:"+e),c.code=10023,c.msg="openadapter fail!",(void 0===a?"undefined":_typeof(a))==TAG_FUNCTION&&a(c)}})}function _connectDevice(e,c,i){var n={},i=(onDisconn=i,e.name);console.log("name:",i),-1!=i.indexOf("WJ")||-1!=i.indexOf("WanJi")?(connectedDeviceId=e.deviceId,my.connectBLEDevice({deviceId:connectedDeviceId,success:function(e){console.log("已连接,开始使能服务",connectedDeviceId),_enableService(function(e){0==e.code?(console.log("已连接,并使能成功"),(n=e).msg="conned and enable success",n.data=connectedDeviceId):(n=e).code=10023,(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(n)})},fail:function(e){console.log("creatcon error:"+e),n.code=10021,n.msg="creatcon error fail!",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(n)}})):(n.code=10020,n.msg="device's name error",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(n))}function _enableService(t){console.log("_enableService start!");var a={};my.getBLEDeviceServices({deviceId:connectedDeviceId,isPrimary:!0,success:function(e){for(var c=0;c<e.services.length;c++){var i=e.services[c].serviceId;_WJUtils2.default.showLog("serviceuuid:",i),i!==services&&i!==services_IOS||(reality_services=i,_WJUtils2.default.showLog("开始获取特征"),my.getBLEDeviceCharacteristics({deviceId:connectedDeviceId,serviceId:i,success:function(e){for(var c=0,i=0;i<e.characteristics.length;i++){var n=e.characteristics[i].characteristicId,o=e.characteristics[i].properties;_WJUtils2.default.showLog("chauuid:",n),_WJUtils2.default.showLog("valueID:",o),n===notifyServiceId||n===notifyServiceId_IOS?(reality_notifyServiceId=n,c++):n!==writeServiceId&&n!==writeServiceId_IOS||(reality_writeServiceId=n,c++)}c<2?(a.code=-1,console.log("getBLEDeviceCharacteristics temp<2!"),a.msg="getBLEDeviceCharacteristics temp<2!",(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(a)):my.notifyBLECharacteristicValueChange({deviceId:connectedDeviceId,serviceId:reality_services,characteristicId:reality_notifyServiceId,state:!0,success:function(e){_onBLECharacteristicValueChange(),null!=timeID&&(clearTimeout(timeID),timeID=null),console.log("notifyBLECharacteristicValueChange success!"),a.code=0,a.msg="enable success!",(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(a)},fail:function(){a.code=-2,a.msg="notifyBLECharacteristicValueChange fail!",console.log("notifyBLECharacteristicValueChange fail!"),(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(a)}})},fail:function(){a.code=-3,a.msg="getBLEDeviceCharacteristics fail!",console.log("getBLEDeviceCharacteristics fail!"),(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(a)}}))}},fail:function(e){a.code=-4,a.msg="getBLEDeviceServices fail!",console.log("getBLEDeviceServices fail!"),(void 0===t?"undefined":_typeof(t))==TAG_FUNCTION&&t(a)}})}function reallyDisConnect(c,i){var n={};my.disconnectBLEDevice({deviceId:c,success:function(e){my.closeBluetoothAdapter({success:function(e){my.offBLEConnectionStateChanged(),n.code=0,n.msg="disconnect success",n.data=c,(void 0===i?"undefined":_typeof(i))==TAG_FUNCTION&&i(n)},fail:function(e){n.code=10031,n.msg="disconnect success but close fail",console.error("disconnect fail but close fail",e),(void 0===i?"undefined":_typeof(i))==TAG_FUNCTION&&i(n)}})},fail:function(e){n.code=10030,n.msg="disconnect fail",console.error("disconnect fail",e),(void 0===i?"undefined":_typeof(i))==TAG_FUNCTION&&i(n)}})}function _onBLEConnectionStateChanged(){var c={};console.log("start _onBLEConnectionStateChanged"),my.offBLEConnectionStateChanged(),my.onBLEConnectionStateChanged(function(e){console.log(e.deviceId+" is "+e.connected),0==e.connected?(my.closeBluetoothAdapter({success:function(e){}}),my.offBLEConnectionStateChanged(),my.offBLECharacteristicValueChange(),c.code=1,c.msg="disconnected"):(c.code=0,c.msg="connected"),(void 0===onDisconn?"undefined":_typeof(onDisconn))==TAG_FUNCTION&&onDisconn(c)})}function _onBLECharacteristicValueChange(){console.log("start _onBLECharacteristicValueChange"),my.offBLECharacteristicValueChange(),my.onBLECharacteristicValueChange(function(e){e.characteristicId===reality_notifyServiceId&&(console.log("rev:"+e.value),1==ListenFlag)&&(void 0===DataListenerCallBack?"undefined":_typeof(DataListenerCallBack))==TAG_FUNCTION&&DataListenerCallBack(e.value)})}var ListenFlag=void 0,DataListenerCallBack=void 0;function SetDataListenerCallBack(e,c){1==e?(ListenFlag=!0,DataListenerCallBack=c):0==e&&(ListenFlag=!1)}function reallyWriteBLECharacteristicValue(e,c){var i={};my.writeBLECharacteristicValue({deviceId:connectedDeviceId,serviceId:reality_services,characteristicId:reality_writeServiceId,value:e,success:function(e){console.log("writeBLECharacteristicValue success",e),i.code=0,i.msg="write success",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(i)},fail:function(e){console.log("writeBLECharacteristicValue fail",e),i.code=10040,i.msg="write fail",(void 0===c?"undefined":_typeof(c))==TAG_FUNCTION&&c(i)}})}module.exports={reallyConnect:reallyConnect,_connectDevice:_connectDevice,reallyDisConnect:reallyDisConnect,reallyWriteBLECharacteristicValue:reallyWriteBLECharacteristicValue,SetDataListenerCallBack:SetDataListenerCallBack};