Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-bluetooth-adapter

Package Overview
Dependencies
5
Maintainers
5
Versions
164
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.12.6 to 0.12.7

6

lib/base/DeviceAdapter.d.ts

@@ -58,2 +58,4 @@ /// <reference types="miniprogram-api-typings" />

communicationMode: CommunicationMode;
_batchWriteEnabled: boolean;
_batchWriteInterval: number;
constructor({ deviceId, productId, deviceName, name, // 设备原始名称

@@ -151,2 +153,6 @@ actions, bluetoothApi, h5Websocket, extendInfo, bluetoothAdapter, }: {

onScanAdvert(params: any): void;
batchWrite(_data: Array<string | ArrayBuffer>, _options: {
writeId?: string;
serviceId?: string;
}): Promise<void>;
}

2

lib/base/DeviceAdapter.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DeviceAdapter=void 0;var tslib_1=require("tslib"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),BlueToothBase_1=require("./BlueToothBase"),types_1=require("./types"),_a=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils,arrayBufferToHexStringArray=_a.arrayBufferToHexStringArray,hexToArrayBuffer=_a.hexToArrayBuffer,DeviceAdapter=function(e){function t(t){var i=t.deviceId,r=t.productId,c=t.deviceName,o=t.name,s=t.actions,n=t.bluetoothApi,a=t.h5Websocket,d=t.extendInfo,u=void 0===d?{}:d,h=t.bluetoothAdapter,v=e.call(this)||this;if(v.extendInfo={},v._name="",v._deviceId="",v._deviceName="",v._deviceConnected=!1,v._productId="",v._deviceRegistered=!1,v._characteristicsAutoNotified=!1,v._services=[],v.characteristicsMap={},v._actions={},v._bluetoothApi={},!i)throw"无deviceId";if(!r)throw"productId为空";return v._h5Websocket=a,v._bluetoothApi=n,v._actions=s,v._name=o,v._deviceName=c,v._deviceId=i,v._productId=r,v.extendInfo=u||{},v.bluetoothAdapter=h,v.communicationMode=types_1.CommunicationMode.GATT,v.on("disconnect",(function(){v._characteristicsAutoNotified=!1})),v}return tslib_1.__extends(t,e),t.prototype._getNotifyId=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;i=i||this.serviceId;var r=this.characteristicsMap[i]||{writeIds:[],notifyIds:[],readIds:[],indicateIds:[]};return r.notifyIds[0]||r.indicateIds[0]},Object.defineProperty(t.prototype,"_writeId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).writeIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_notifyId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).notifyIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_readId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_indicateId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).indicateIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceId",{get:function(){return this._deviceId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"productId",{get:function(){return this._productId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceName",{get:function(){return this._deviceName},set:function(e){this._deviceName=e},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isConnected",{get:function(){return this._deviceConnected},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"originName",{get:function(){return this._name},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"explorerDeviceId",{get:function(){return[this._productId||"",this._deviceName||""].join("/")},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"serviceId",{get:function(){return this.constructor.serviceId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceInfo",{get:function(){return{productId:this.productId,deviceName:this.deviceName,deviceId:this.deviceId,explorerDeviceId:this.explorerDeviceId,name:this.originName}},enumerable:!1,configurable:!0}),t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return this.bluetoothAdapter?[4,this.bluetoothAdapter.init()]:[3,2];case 1:return e.sent(),[3,3];case 2:console.warn("DeviceAdapter未注入bluetoothAdapter实例,可能使用的不是最新版本的qcloud-iotexplorer-bluetooth-adapter模块"),e.label=3;case 3:return[2]}}))}))},t.prototype.handleBLEMessage=function(e,t){t.serviceId,t.characteristicId;return{}},t.prototype.registerDevice=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return this._deviceRegistered?[3,2]:[4,this._actions.registerDevice({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId})];case 1:e.sent(),this._deviceRegistered=!0,e.label=2;case 2:return[2]}}))}))},t.prototype.bindDevice=function(e){var t=void 0===e?{}:e,i=t.familyId,r=void 0===i?"":i,c=t.roomId,o=void 0===c?"":c;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,this.registerDevice()];case 1:return i.sent(),e={deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,familyId:r,roomId:o},[4,this._actions.bindDevice(e)];case 2:return i.sent(),this.emit("bind",e),[2,this.explorerDeviceId];case 3:return t=i.sent(),[2,Promise.reject(this._normalizeError(t))];case 4:return[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.connected;console.log("onBleConnectionStateChange => ",t);var i=this._deviceConnected!==t;this._deviceConnected=t,i&&(t?this.emit("connect",tslib_1.__assign({},this.deviceInfo)):this.emit("disconnect",tslib_1.__assign({},this.deviceInfo)),this.emit("bLEConnectionStateChange",{connected:t}))},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.serviceId,i=e.characteristicId,r=e.value;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,c,o,s,n,a,d,u;return tslib_1.__generator(this,(function(h){switch(h.label){case 0:return h.trys.push([0,4,,5]),e=r,r instanceof ArrayBuffer&&(e=arrayBufferToHexStringArray(r)),c=this.handleBLEMessage(e,{serviceId:t,characteristicId:i})||{},o=c.shouldIgnore,s=c.reportData,n=tslib_1.__rest(c,["shouldIgnore","reportData"]),o?[2]:(console.log("[DeviceAdapter] receive data:",e,n),a=Date.now(),d=!1,this._deviceName&&s?(d=!0,"function"!=typeof this._actions.reportDeviceData?[3,2]:[4,this._actions.reportDeviceData({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,data:s,timestamp:a})]):[3,3]);case 1:return h.sent(),[3,3];case 2:console.warn("handleBLEMessage return should report but actions.reportDeviceData is not implement."),h.label=3;case 3:return this.emit("message",tslib_1.__assign(tslib_1.__assign({},n),{timestamp:a,dataReported:d})),[3,5];case 4:return u=h.sent(),console.error("onBLECharacteristicValueChange onError,",u),[3,5];case 5:return[2]}}))}))},t.prototype.disconnectDevice=function(){this._bluetoothApi.closeBLEConnection({deviceId:this._deviceId}),this._deviceConnected=!1,this.emit("disconnect",tslib_1.__assign({},this.deviceInfo))},t.prototype.connectDevice=function(e){var t=(void 0===e?{}:e).autoNotify,i=void 0===t||t;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,r,c;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:e=Date.now(),o.label=1;case 1:return o.trys.push([1,8,,9]),[4,this.init()];case 2:return o.sent(),console.log("start connect device",this.deviceInfo),this.isConnected?(console.log("device is already connected",this.deviceInfo),[2]):(t=Date.now(),[4,this._bluetoothApi.createBLEConnection({deviceId:this._deviceId})]);case 3:if(o.sent(),this._deviceConnected=!0,console.log("createBLEConnection succ",Date.now()-t),!i)return[3,7];o.label=4;case 4:return o.trys.push([4,6,,7]),[4,this.autoNotifyCharacteristics(!0)];case 5:return o.sent(),[3,7];case 6:throw r=o.sent(),this.isConnected&&this.disconnectDevice(),r;case 7:return console.log("connectDevice succeed, time cost: ",Date.now()-e),[3,9];case 8:return c=o.sent(),(c=this._normalizeError(c)).errMsg&&c.errMsg.indexOf("already connect")>-1?(console.log("device is already connected, resolve directly, time cost: ",Date.now()-e),[2,Promise.resolve()]):(console.error("connectDevice error",c),[2,Promise.reject(c)]);case 9:return[2]}}))}))},t.prototype.autoNotifyCharacteristics=function(e){return void 0===e&&(e=!1),tslib_1.__awaiter(this,void 0,void 0,(function(){var t,i,r;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:return this._characteristicsAutoNotified&&!e?(console.log("[DeviceAdapter.autoNotifyCharacteristics] characteristics already notified, no operation performed"),[2]):(t=Date.now(),[4,this.getBLEDeviceServices()]);case 1:return i=c.sent(),console.log("getBLEDeviceServices succ",i,Date.now()-t),this.emit("onGetBLEDeviceServices",i),t=Date.now(),[4,this.getBLEDeviceCharacteristics()];case 2:return r=c.sent(),console.log("getBLEDeviceCharacteristics succ",r,Date.now()-t),this.emit("onGetBLEDeviceCharacteristics",r),t=Date.now(),[4,this.notifyBLECharacteristicValueChange()];case 3:return c.sent(),console.log("notifyBLECharacteristicValueChange succ",Date.now()-t),this._characteristicsAutoNotified=!0,[2]}}))}))},t.prototype.write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,c=void 0===r?"":r,o=i.serviceId,s=void 0===o?"":o;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){if("string"==typeof e)console.log("writeBLECharacteristicValue",e),e=hexToArrayBuffer(e);else if(e instanceof ArrayBuffer)try{console.log("writeBLECharacteristicValue",arrayBufferToHexStringArray(e).join(""))}catch(e){}return[2,this._write(e,{writeId:c,serviceId:s})]}))}))},t.prototype._write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,c=void 0===r?"":r,o=i.serviceId,s=void 0===o?"":o;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,this._bluetoothApi.writeBLECharacteristicValue({deviceId:this._deviceId,characteristicId:c||this._writeId,serviceId:s||this.serviceId,value:e})];case 1:return i.sent(),[3,3];case 2:return t=i.sent(),[2,Promise.reject(this._normalizeError(t))];case 3:return[2]}}))}))},t.prototype.getBLEDeviceServices=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return[4,this._bluetoothApi.getBLEDeviceServices({deviceId:this._deviceId})];case 1:return e=t.sent().services,this._services=e,[2,e]}}))}))},t.prototype.setCharacteristicsIds=function(e,t){var i={notifyIds:[],writeIds:[],indicateIds:[],readIds:[]},r=function(e,t){-1===e.indexOf(t)&&e.push(t)};t.forEach((function(e){var t=e.uuid,c=e.properties,o=c.notify,s=c.write,n=c.indicate,a=c.read;o?r(i.notifyIds,t):s?r(i.writeIds,t):n?r(i.indicateIds,t):a&&r(i.readIds,t)})),this.characteristicsMap[e]=i},t.prototype.getBLEDeviceCharacteristics=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return i=i||this.serviceId,[4,this._bluetoothApi.getBLEDeviceCharacteristics({deviceId:this._deviceId,serviceId:i})];case 1:return e=t.sent().characteristics,this.setCharacteristicsIds(i,e),[2,e]}}))}))},t.prototype.notifyBLECharacteristicValueChange=function(e){var t=void 0===e?{}:e,i=t.characteristicId,r=void 0===i?"":i,c=t.serviceId,o=void 0===c?"":c,s=t.state,n=void 0===s||s;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this._getNotifyId(),o=o||this.serviceId,r?[3,1]:(console.warn("未找到指定service下的notifyId,该设备可能不支持notify"),[3,3]);case 1:return[4,this._bluetoothApi.notifyBLECharacteristicValueChange({deviceId:this._deviceId,characteristicId:r,serviceId:o,state:n})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.readBLECharacteristicValue=function(e){var t=void 0===e?{}:e,i=t.serviceId,r=void 0===i?"":i,c=t.characteristicId,o=void 0===c?"":c;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this.serviceId,o||(o=((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]),o?[3,1]:(console.warn("未找到指定service下的readId,该设备可能不支持read"),[3,3]);case 1:return[4,this._bluetoothApi.readBLECharacteristicValue({deviceId:this._deviceId,characteristicId:o,serviceId:r})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.setBLEMTU=function(e){return this._bluetoothApi.setBLEMTU(tslib_1.__assign({deviceId:this._deviceId},e))},t.prototype.getBLEDeviceRSSI=function(){return this._bluetoothApi.getBLEDeviceRSSI({deviceId:this._deviceId})},t.prototype.destroy=function(){this.emit("destroy")},t.prototype.advertFilter=function(e){return null},t.prototype.onScanAdvert=function(e){},t.serviceId="",t.deviceFilter=function(e,t){},t}(BlueToothBase_1.BlueToothBase);exports.DeviceAdapter=DeviceAdapter;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DeviceAdapter=void 0;var tslib_1=require("tslib"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),BlueToothBase_1=require("./BlueToothBase"),types_1=require("./types"),_a=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils,arrayBufferToHexStringArray=_a.arrayBufferToHexStringArray,hexToArrayBuffer=_a.hexToArrayBuffer,DeviceAdapter=function(e){function t(t){var i=t.deviceId,r=t.productId,o=t.deviceName,c=t.name,n=t.actions,s=t.bluetoothApi,a=t.h5Websocket,d=t.extendInfo,u=void 0===d?{}:d,h=t.bluetoothAdapter,v=e.call(this)||this;if(v.extendInfo={},v._name="",v._deviceId="",v._deviceName="",v._deviceConnected=!1,v._productId="",v._deviceRegistered=!1,v._characteristicsAutoNotified=!1,v._services=[],v.characteristicsMap={},v._actions={},v._bluetoothApi={},v._batchWriteEnabled=!1,v._batchWriteInterval=20,!i)throw"无deviceId";if(!r)throw"productId为空";return v._h5Websocket=a,v._bluetoothApi=s,v._actions=n,v._name=c,v._deviceName=o,v._deviceId=i,v._productId=r,v.extendInfo=u||{},v.bluetoothAdapter=h,v.communicationMode=types_1.CommunicationMode.GATT,v.on("disconnect",(function(){v._characteristicsAutoNotified=!1})),v}return tslib_1.__extends(t,e),t.prototype._getNotifyId=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;i=i||this.serviceId;var r=this.characteristicsMap[i]||{writeIds:[],notifyIds:[],readIds:[],indicateIds:[]};return r.notifyIds[0]||r.indicateIds[0]},Object.defineProperty(t.prototype,"_writeId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).writeIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_notifyId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).notifyIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_readId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"_indicateId",{get:function(){return((this.characteristicsMap[this.serviceId]||{}).indicateIds||[])[0]},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceId",{get:function(){return this._deviceId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"productId",{get:function(){return this._productId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceName",{get:function(){return this._deviceName},set:function(e){this._deviceName=e},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"isConnected",{get:function(){return this._deviceConnected},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"originName",{get:function(){return this._name},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"explorerDeviceId",{get:function(){return[this._productId||"",this._deviceName||""].join("/")},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"serviceId",{get:function(){return this.constructor.serviceId},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"deviceInfo",{get:function(){return{productId:this.productId,deviceName:this.deviceName,deviceId:this.deviceId,explorerDeviceId:this.explorerDeviceId,name:this.originName}},enumerable:!1,configurable:!0}),t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return this.bluetoothAdapter?[4,this.bluetoothAdapter.init()]:[3,2];case 1:return e.sent(),[3,3];case 2:console.warn("DeviceAdapter未注入bluetoothAdapter实例,可能使用的不是最新版本的qcloud-iotexplorer-bluetooth-adapter模块"),e.label=3;case 3:return[2]}}))}))},t.prototype.handleBLEMessage=function(e,t){t.serviceId,t.characteristicId;return{}},t.prototype.registerDevice=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return this._deviceRegistered?[3,2]:[4,this._actions.registerDevice({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId})];case 1:e.sent(),this._deviceRegistered=!0,e.label=2;case 2:return[2]}}))}))},t.prototype.bindDevice=function(e){var t=void 0===e?{}:e,i=t.familyId,r=void 0===i?"":i,o=t.roomId,c=void 0===o?"":o;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,this.registerDevice()];case 1:return i.sent(),e={deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,familyId:r,roomId:c},[4,this._actions.bindDevice(e)];case 2:return i.sent(),this.emit("bind",e),[2,this.explorerDeviceId];case 3:return t=i.sent(),[2,Promise.reject(this._normalizeError(t))];case 4:return[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.connected;console.log("onBleConnectionStateChange => ",t);var i=this._deviceConnected!==t;this._deviceConnected=t,i&&(t?this.emit("connect",tslib_1.__assign({},this.deviceInfo)):this.emit("disconnect",tslib_1.__assign({},this.deviceInfo)),this.emit("bLEConnectionStateChange",{connected:t}))},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.serviceId,i=e.characteristicId,r=e.value;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,c,n,s,a,d,u;return tslib_1.__generator(this,(function(h){switch(h.label){case 0:return h.trys.push([0,4,,5]),e=r,r instanceof ArrayBuffer&&(e=arrayBufferToHexStringArray(r)),o=this.handleBLEMessage(e,{serviceId:t,characteristicId:i})||{},c=o.shouldIgnore,n=o.reportData,s=tslib_1.__rest(o,["shouldIgnore","reportData"]),c?[2]:(console.log("[DeviceAdapter] receive data:",e,s),a=Date.now(),d=!1,this._deviceName&&n?(d=!0,"function"!=typeof this._actions.reportDeviceData?[3,2]:[4,this._actions.reportDeviceData({deviceId:this.explorerDeviceId,deviceName:this._deviceName,productId:this._productId,data:n,timestamp:a})]):[3,3]);case 1:return h.sent(),[3,3];case 2:console.warn("handleBLEMessage return should report but actions.reportDeviceData is not implement."),h.label=3;case 3:return this.emit("message",tslib_1.__assign(tslib_1.__assign({},s),{timestamp:a,dataReported:d})),[3,5];case 4:return u=h.sent(),console.error("onBLECharacteristicValueChange onError,",u),[3,5];case 5:return[2]}}))}))},t.prototype.disconnectDevice=function(){this._bluetoothApi.closeBLEConnection({deviceId:this._deviceId}),this._deviceConnected=!1,this.emit("disconnect",tslib_1.__assign({},this.deviceInfo))},t.prototype.connectDevice=function(e){var t=(void 0===e?{}:e).autoNotify,i=void 0===t||t;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,t,r,o;return tslib_1.__generator(this,(function(c){switch(c.label){case 0:e=Date.now(),c.label=1;case 1:return c.trys.push([1,8,,9]),[4,this.init()];case 2:return c.sent(),console.log("start connect device",this.deviceInfo),this.isConnected?(console.log("device is already connected",this.deviceInfo),[2]):(t=Date.now(),[4,this._bluetoothApi.createBLEConnection({deviceId:this._deviceId})]);case 3:if(c.sent(),this._deviceConnected=!0,console.log("createBLEConnection succ",Date.now()-t),!i)return[3,7];c.label=4;case 4:return c.trys.push([4,6,,7]),[4,this.autoNotifyCharacteristics(!0)];case 5:return c.sent(),[3,7];case 6:throw r=c.sent(),this.isConnected&&this.disconnectDevice(),r;case 7:return console.log("connectDevice succeed, time cost: ",Date.now()-e),[3,9];case 8:return o=c.sent(),(o=this._normalizeError(o)).errMsg&&o.errMsg.indexOf("already connect")>-1?(console.log("device is already connected, resolve directly, time cost: ",Date.now()-e),[2,Promise.resolve()]):(console.error("connectDevice error",o),[2,Promise.reject(o)]);case 9:return[2]}}))}))},t.prototype.autoNotifyCharacteristics=function(e){return void 0===e&&(e=!1),tslib_1.__awaiter(this,void 0,void 0,(function(){var t,i,r;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return this._characteristicsAutoNotified&&!e?(console.log("[DeviceAdapter.autoNotifyCharacteristics] characteristics already notified, no operation performed"),[2]):(t=Date.now(),[4,this.getBLEDeviceServices()]);case 1:return i=o.sent(),console.log("getBLEDeviceServices succ",i,Date.now()-t),this.emit("onGetBLEDeviceServices",i),t=Date.now(),[4,this.getBLEDeviceCharacteristics()];case 2:return r=o.sent(),console.log("getBLEDeviceCharacteristics succ",r,Date.now()-t),this.emit("onGetBLEDeviceCharacteristics",r),t=Date.now(),[4,this.notifyBLECharacteristicValueChange()];case 3:return o.sent(),console.log("notifyBLECharacteristicValueChange succ",Date.now()-t),this._characteristicsAutoNotified=!0,[2]}}))}))},t.prototype.write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,o=void 0===r?"":r,c=i.serviceId,n=void 0===c?"":c;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){if("string"==typeof e)console.log("writeBLECharacteristicValue",e),e=hexToArrayBuffer(e);else if(e instanceof ArrayBuffer)try{console.log("writeBLECharacteristicValue",arrayBufferToHexStringArray(e).join(""))}catch(e){}return[2,this._write(e,{writeId:o,serviceId:n})]}))}))},t.prototype._write=function(e,t){var i=void 0===t?{}:t,r=i.writeId,o=void 0===r?"":r,c=i.serviceId,n=void 0===c?"":c;return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,2,,3]),[4,this._bluetoothApi.writeBLECharacteristicValue({deviceId:this._deviceId,characteristicId:o||this._writeId,serviceId:n||this.serviceId,value:e})];case 1:return i.sent(),[3,3];case 2:return t=i.sent(),[2,Promise.reject(this._normalizeError(t))];case 3:return[2]}}))}))},t.prototype.getBLEDeviceServices=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return[4,this._bluetoothApi.getBLEDeviceServices({deviceId:this._deviceId})];case 1:return e=t.sent().services,this._services=e,[2,e]}}))}))},t.prototype.setCharacteristicsIds=function(e,t){var i={notifyIds:[],writeIds:[],indicateIds:[],readIds:[]},r=function(e,t){-1===e.indexOf(t)&&e.push(t)};t.forEach((function(e){var t=e.uuid,o=e.properties,c=o.notify,n=o.write,s=o.indicate,a=o.read;c?r(i.notifyIds,t):n?r(i.writeIds,t):s?r(i.indicateIds,t):a&&r(i.readIds,t)})),this.characteristicsMap[e]=i},t.prototype.getBLEDeviceCharacteristics=function(e){var t=(void 0===e?{}:e).serviceId,i=void 0===t?"":t;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e;return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return i=i||this.serviceId,[4,this._bluetoothApi.getBLEDeviceCharacteristics({deviceId:this._deviceId,serviceId:i})];case 1:return e=t.sent().characteristics,this.setCharacteristicsIds(i,e),[2,e]}}))}))},t.prototype.notifyBLECharacteristicValueChange=function(e){var t=void 0===e?{}:e,i=t.characteristicId,r=void 0===i?"":i,o=t.serviceId,c=void 0===o?"":o,n=t.state,s=void 0===n||n;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this._getNotifyId(),c=c||this.serviceId,r?[3,1]:(console.warn("未找到指定service下的notifyId,该设备可能不支持notify"),[3,3]);case 1:return[4,this._bluetoothApi.notifyBLECharacteristicValueChange({deviceId:this._deviceId,characteristicId:r,serviceId:c,state:s})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.readBLECharacteristicValue=function(e){var t=void 0===e?{}:e,i=t.serviceId,r=void 0===i?"":i,o=t.characteristicId,c=void 0===o?"":o;return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return r=r||this.serviceId,c||(c=((this.characteristicsMap[this.serviceId]||{}).readIds||[])[0]),c?[3,1]:(console.warn("未找到指定service下的readId,该设备可能不支持read"),[3,3]);case 1:return[4,this._bluetoothApi.readBLECharacteristicValue({deviceId:this._deviceId,characteristicId:c,serviceId:r})];case 2:e.sent(),e.label=3;case 3:return[2]}}))}))},t.prototype.setBLEMTU=function(e){return this._bluetoothApi.setBLEMTU(tslib_1.__assign({deviceId:this._deviceId},e))},t.prototype.getBLEDeviceRSSI=function(){return this._bluetoothApi.getBLEDeviceRSSI({deviceId:this._deviceId})},t.prototype.destroy=function(){this.emit("destroy")},t.prototype.advertFilter=function(e){return null},t.prototype.onScanAdvert=function(e){},t.prototype.batchWrite=function(e,t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){throw new Error("DeviceAdapter.batchWrite not implemented")}))}))},t.serviceId="",t.deviceFilter=function(e,t){},t}(BlueToothBase_1.BlueToothBase);exports.DeviceAdapter=DeviceAdapter;
//# sourceMappingURL=DeviceAdapter.js.map

@@ -6,2 +6,7 @@ import { DeviceAdapter } from '../base';

write(data: any, writeId: any): Promise<void>;
setBatchWriteMode(enabled: boolean, interval?: number): void;
batchWrite(data: Array<string | ArrayBuffer>, options: {
writeId?: string;
serviceId?: string;
}): Promise<void>;
bluetoothAdapter: import("../base").BlueToothAdapter;

@@ -22,2 +27,4 @@ extendInfo: any;

communicationMode: import("../base").CommunicationMode;
_batchWriteEnabled: boolean;
_batchWriteInterval: number;
_getNotifyId({ serviceId }?: {

@@ -107,2 +114,7 @@ serviceId?: string;

write(data: any, writeId: any): Promise<void>;
setBatchWriteMode(enabled: boolean, interval?: number): void;
batchWrite(data: Array<string | ArrayBuffer>, options: {
writeId?: string;
serviceId?: string;
}): Promise<void>;
bluetoothAdapter: import("../base").BlueToothAdapter;

@@ -123,2 +135,4 @@ extendInfo: any;

communicationMode: import("../base").CommunicationMode;
_batchWriteEnabled: boolean;
_batchWriteInterval: number;
_getNotifyId({ serviceId }?: {

@@ -125,0 +139,0 @@ serviceId?: string;

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DeviceAdapter4H5=exports.AdapterDeviceAdapter4H5=void 0;var tslib_1=require("tslib"),base_1=require("../base"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),arrayBufferToHexStringArray=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.arrayBufferToHexStringArray;exports.AdapterDeviceAdapter4H5=function(e){return function(e){function r(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var o=e.apply(this,tslib_1.__spread(r))||this;return o.on("connect",(function(){var e,r,t=o,i=t.explorerDeviceId,n=t.deviceId,d=t.deviceName,c=t.originName,a=t.productId,s=t.extendInfo,p=t.serviceId;null===(e=o._h5Websocket)||void 0===e||e.send("Control",{action:"reportDeviceConnectStatus",payload:{connected:!0,explorerDeviceId:i,deviceId:n}}),null===(r=o._h5Websocket)||void 0===r||r.send("Control",{action:"h5_mirror_connect",payload:{serviceId:p,deviceId:n,deviceName:d,name:c,productId:a,extendInfo:s}})})).on("disconnect",(function(){var e,r,t=o,i=t.explorerDeviceId,n=t.deviceId;null===(e=o._h5Websocket)||void 0===e||e.send("Control",{action:"reportDeviceConnectStatus",payload:{connected:!1,explorerDeviceId:i,deviceId:n}}),null===(r=o._h5Websocket)||void 0===r||r.send("Control",{action:"h5_mirror_disconnect",payload:{explorerDeviceId:i,deviceId:n}})})),o}return tslib_1.__extends(r,e),r.prototype.write=function(e,r){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return"string"==typeof e?console.log("writeBLECharacteristicValue from H5",e):e instanceof ArrayBuffer&&(e=arrayBufferToHexStringArray(e).join("")),[2,this._write(e,r)]}))}))},r}(e)},exports.DeviceAdapter4H5=exports.AdapterDeviceAdapter4H5(base_1.DeviceAdapter);
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.DeviceAdapter4H5=exports.AdapterDeviceAdapter4H5=void 0;var tslib_1=require("tslib"),base_1=require("../base"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),arrayBufferToHexStringArray=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.arrayBufferToHexStringArray;exports.AdapterDeviceAdapter4H5=function(e){return function(e){function r(){for(var r=[],t=0;t<arguments.length;t++)r[t]=arguments[t];var i=e.apply(this,tslib_1.__spread(r))||this;return i.on("connect",(function(){var e,r,t=i,o=t.explorerDeviceId,n=t.deviceId,a=t.deviceName,c=t.originName,d=t.productId,s=t.extendInfo,p=t.serviceId;null===(e=i._h5Websocket)||void 0===e||e.send("Control",{action:"reportDeviceConnectStatus",payload:{connected:!0,explorerDeviceId:o,deviceId:n}}),null===(r=i._h5Websocket)||void 0===r||r.send("Control",{action:"h5_mirror_connect",payload:{serviceId:p,deviceId:n,deviceName:a,name:c,productId:d,extendInfo:s}})})).on("disconnect",(function(){var e,r,t=i,o=t.explorerDeviceId,n=t.deviceId;null===(e=i._h5Websocket)||void 0===e||e.send("Control",{action:"reportDeviceConnectStatus",payload:{connected:!1,explorerDeviceId:o,deviceId:n}}),null===(r=i._h5Websocket)||void 0===r||r.send("Control",{action:"h5_mirror_disconnect",payload:{explorerDeviceId:o,deviceId:n}})})),i}return tslib_1.__extends(r,e),r.prototype.write=function(e,r){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return"string"==typeof e?console.log("writeBLECharacteristicValue from H5",e):e instanceof ArrayBuffer&&(e=arrayBufferToHexStringArray(e).join("")),[2,this._write(e,r)]}))}))},r.prototype.setBatchWriteMode=function(e,r){if(void 0===r&&(r=0),!Number.isSafeInteger(r))throw new Error("DeviceAdapter.setBatchWriteMode fail: param interval not typeof int");if(r<0||r>500)throw new Error("DeviceAdapter.setBatchWriteMode fail: param interval ["+r+"] out of range");this._batchWriteEnabled=!!e,this._batchWriteInterval=r},r.prototype.batchWrite=function(e,r){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,i;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return t=this.bluetoothAdapter,i=e.map((function(e){if("string"==typeof e)return e;if(e instanceof ArrayBuffer)return arrayBufferToHexStringArray(e).join("");throw new Error("DeviceAdapter4H5.batchWrite: invalid param data")})),[4,t._blueToothBridge.control("batchWriteBLECharacteristicValue",{deviceId:this.deviceId,serviceId:r.serviceId||this.serviceId,characteristicId:r.writeId||this._writeId,values:i,interval:this._batchWriteInterval})];case 1:return o.sent(),[2]}}))}))},r}(e)},exports.DeviceAdapter4H5=exports.AdapterDeviceAdapter4H5(base_1.DeviceAdapter);
//# sourceMappingURL=DeviceAdapter4H5.js.map

@@ -71,2 +71,9 @@ /// <reference types="miniprogram-api-typings" />

cleanup(action?: string): void;
protected batchWriteBLECharacteristicValue({ deviceId, serviceId, characteristicId, values, interval, }: {
deviceId: string;
serviceId: string;
characteristicId: string;
values: string[];
interval: number;
}): Promise<void>;
}

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter4Mp=void 0;var tslib_1=require("tslib"),base_1=require("../base"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),_a=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils,arrayBufferToHexStringArray=_a.arrayBufferToHexStringArray,hexToArrayBuffer=_a.hexToArrayBuffer,parseAdvertisData=function(e){var t=tslib_1.__assign({},e);if(e.advertisData)try{t.advertisData=arrayBufferToHexStringArray(e.advertisData)}catch(e){console.error("Parse bluetoothe device advertisData fail",e)}return e.serviceData&&"object"==typeof e.serviceData&&(t.serviceData={},Object.entries(e.serviceData).forEach((function(e){var o=tslib_1.__read(e,2),n=o[0],r=o[1];r instanceof ArrayBuffer&&(t.serviceData[n]=arrayBufferToHexStringArray(r))}))),t},BlueToothAdapter4Mp=function(e){function t(t){var o=t.bluetoothApi,n=tslib_1.__rest(t,["bluetoothApi"]),r=e.call(this,tslib_1.__assign({bluetoothApi:o},n))||this;return r._h5ChanelOpened=!1,r._localDiscoveringInUse=!1,r._h5DiscoveringInUse=!1,r._cleanupTimer=null,r._currentProductId="",r.handleH5WsMessage=null,r.deviceDelayDisconnectQueue=[],r._mpBleConnectionStateCache={},r._h5Websocket&&"function"==typeof r._h5Websocket.on&&(r.on("adapterStateChange",(function(e){var t=e.available,o=e.discovering;r.response2BlueToothChanel("onBluetoothAdapterStateChange",{available:t,discovering:o})})),r._h5Websocket.on("message",(function(e){var t=e.data,o=e.reqId;return tslib_1.__awaiter(r,void 0,void 0,(function(){var e,n,r,s,i,c,a,l,u,h,d,v,p,_,f,D,g,y=this;return tslib_1.__generator(this,(function(C){switch(C.label){case 0:if(console.log("bluetooth ws on message",t),"function"==typeof this.handleH5WsMessage)try{if(this.handleH5WsMessage({data:t,reqId:o}))return console.log("h5 message already handled by handleH5WsMessage implement."),[2]}catch(e){console.error("call handleH5WsMessage error",e)}switch(e=t.action,n=t.payload,e){case"reportDeviceConnectStatus":return[3,1];case"syncDeviceConnectStatus":return[3,2];case"bindDevice":return[3,3];case"registryDevice":return[3,7];case"connect":return[3,11];case"disconnect":return[3,12];case"init":return[3,13];case"callApi":return[3,17]}return[3,32];case 1:return r=n.connected,s=n.explorerDeviceId,i=n.deviceId,this.onDeviceConnectStatusChange({connected:r,explorerDeviceId:s,deviceId:i}),[3,32];case 2:return c=n.devices,(_=void 0===c?[]:c).forEach((function(e){var t=e.deviceId,o=e.connected,n=y._mpBleConnectionStateCache[t]||!1;n!==o&&(console.log("[syncDeviceConnectStatus] deviceId="+t+",","h5.connected="+o+", actualConnected="+n),y.response2BlueToothChanel("onBLEConnectionStateChange",{deviceId:t,connected:n}))})),[3,32];case 3:return C.trys.push([3,5,,6]),l=n.deviceName,u=n.productId,[4,this._actions.bindDevice({productId:this.devMode&&u?u:this._currentProductId,deviceName:l})];case 4:return C.sent(),this.response2BlueToothChanel("response",{code:0},o),[3,6];case 5:return a=C.sent(),this.response2BlueToothChanel("response",a,o),[3,6];case 6:return[3,32];case 7:return C.trys.push([7,9,,10]),l=n.deviceName,u=n.productId,[4,this._actions.registerDevice({productId:this.devMode&&u?u:this._currentProductId,deviceName:l})];case 8:return C.sent(),this.response2BlueToothChanel("response",{code:0},o),[3,10];case 9:return h=C.sent(),this.response2BlueToothChanel("response",h,o),[3,10];case 10:return[3,32];case 11:return console.log("h5chanel opened"),this._h5ChanelOpened=!0,[3,32];case 12:return console.log("h5chanel closed"),this._h5ChanelOpened=!1,this._h5DiscoveringInUse&&this.stopBluetoothDevicesDiscovery(!0),[3,32];case 13:return C.trys.push([13,15,,16]),[4,this.init()];case 14:return C.sent(),this.response2BlueToothChanel("response",{code:0},o),[3,16];case 15:return d=C.sent(),this.response2BlueToothChanel("response",d,o),[3,16];case 16:return[3,32];case 17:v=n.api,p=n.params,C.label=18;case 18:switch(C.trys.push([18,30,,31]),console.log("call api",v,p),v){case"createBLEConnection":return[3,19];case"getBluetoothDevices":return[3,21];case"writeBLECharacteristicValue":return[3,23];case"startBluetoothDevicesDiscovery":return[3,25];case"stopBluetoothDevicesDiscovery":return[3,27]}return[3,29];case 19:return this.tryCancelDisconnectDevice(p.deviceId),[4,this._bluetoothApi.createBLEConnection(p)];case 20:return C.sent(),this.response2BlueToothChanel("response",{},o),[2];case 21:return[4,this.getBluetoothDevices()];case 22:return _=C.sent(),this.response2BlueToothChanel("response",{devices:_.map(parseAdvertisData)},o),[2];case 23:return f=p.value,console.log("calling writeBLECharacteristicValue",tslib_1.__assign(tslib_1.__assign({},p),{value:hexToArrayBuffer(f)})),[4,this._bluetoothApi.writeBLECharacteristicValue(tslib_1.__assign(tslib_1.__assign({},p),{value:hexToArrayBuffer(f)}))];case 24:return D=C.sent(),this.response2BlueToothChanel("response",D,o),[2];case 25:return[4,this.startBluetoothDevicesDiscovery({isFromH5:!0,interval:1e3})];case 26:return D=C.sent(),this.response2BlueToothChanel("response",D,o),[2];case 27:return[4,this.stopBluetoothDevicesDiscovery(!0)];case 28:return D=C.sent(),this.response2BlueToothChanel("response",D,o),[2];case 29:return[3,31];case 30:return g=C.sent(),console.error("[BlueToothAdapter4Mp] call bluetoothApi fail",g),this.response2BlueToothChanel("response",this._normalizeError(g),o),[2];case 31:return wx[v]&&wx[v](tslib_1.__assign(tslib_1.__assign({},p),{success:function(e){console.log("[BlueToothAdapter4Mp] call wx api success",e),y.response2BlueToothChanel("response",e,o)},fail:function(e){console.log("call api fail",e),y.response2BlueToothChanel("response",y._normalizeError(e),o)}})),[3,32];case 32:return[2]}}))}))}))),r}return tslib_1.__extends(t,e),t.prototype.tryCancelDisconnectDevice=function(e){var t=this.deviceDelayDisconnectQueue.findIndex((function(t){return t.deviceId===e}));if(t>-1){var o=this.deviceDelayDisconnectQueue[t];return clearTimeout(o.timer),this.deviceDelayDisconnectQueue.splice(t,1),console.log("Cancel disconnect device: "+e),!0}return console.log("Try cancel disconnect device: "+e+", but not found in queue, maybe it's already disconnected"),!1},t.prototype.disconnectDevice=function(e,t){var o=this,n=e.deviceId,r=e.explorerDeviceId;void 0===t&&(t=0),console.log("call disconnectDevice",{deviceId:n,explorerDeviceId:r});var s=this._deviceConnectStatusStore.get({deviceId:n,explorerDeviceId:r});if(null==s?void 0:s.connected)if(t>0){var i=setTimeout((function(){console.log("execute closeBLEConnection for deviceId: "+n+" after "+t+"ms"),o._bluetoothApi.closeBLEConnection({deviceId:n})}),t);console.log("Will disconnect device: "+n+" after "+t+"ms..."),this.deviceDelayDisconnectQueue.push({deviceId:n,timer:i})}else this._bluetoothApi.closeBLEConnection({deviceId:n});else console.log("call disconnectDevice, but device maybe not connected",s,this._deviceConnectStatusStore.getAll())},t.prototype.setCurrentProduct=function(e){this._currentProductId=e},t.prototype.startBluetoothDevicesDiscovery=function(t){void 0===t&&(t={});var o=t.isFromH5,n=void 0!==o&&o,r=tslib_1.__rest(t,["isFromH5"]);return n?this._h5DiscoveringInUse=!0:this._localDiscoveringInUse=!0,e.prototype.startBluetoothDevicesDiscovery.call(this,r)},t.prototype.stopBluetoothDevicesDiscovery=function(t){if(void 0===t&&(t=!1),console.log("try call stopBluetoothDevicesDiscovery,\n isFromH5: "+t+", _h5DiscoveringInUse: "+this._h5DiscoveringInUse+",\n _localDiscoveringInUse: "+this._localDiscoveringInUse),t?this._h5DiscoveringInUse=!1:this._localDiscoveringInUse=!1,this._h5DiscoveringInUse||!this._localDiscoveringInUse)return e.prototype.stopBluetoothDevicesDiscovery.call(this)},t.prototype.response2BlueToothChanel=function(e,t,o){return void 0===t&&(t={}),void 0===o&&(o=""),tslib_1.__awaiter(this,void 0,void 0,(function(){var n;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),this._h5ChanelOpened?[4,this._h5Websocket.send("Response",{action:e,payload:t},{reqId:o})]:[2];case 1:return r.sent(),[3,3];case 2:return n=r.sent(),console.warn("try send bluetooth message fail",n),[3,3];case 3:return[2]}}))}))},t.prototype.startCleanupTimer=function(){var e=this;clearTimeout(this._cleanupTimer),this._cleanupTimer=setTimeout((function(){var t=e._deviceAdapterStore.getAll().filter((function(e){return e.isConnected}));console.log("bluetooth searching or deviceMap not empty, reset cleanup timer",e._discovering,t),e._h5ChanelOpened||e._discovering||t.length?e.startCleanupTimer():e.cleanup()}),3e4)},t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t=this;return tslib_1.__generator(this,(function(o){return[2,e.prototype.init.call(this).then((function(){return t.startCleanupTimer()}))]}))}))},t.prototype.onBleConnectionStateChange=function(t){var o=t.deviceId,n=t.connected;return this.response2BlueToothChanel("onBLEConnectionStateChange",{deviceId:o,connected:n}),this._mpBleConnectionStateCache[o]=n,e.prototype.onBleConnectionStateChange.call(this,{deviceId:o,connected:n})},t.prototype.onBLECharacteristicValueChange=function(t){var o=t.deviceId,n=t.serviceId,r=t.characteristicId,s=t.value;return this.response2BlueToothChanel("onBLECharacteristicValueChange",{deviceId:o,serviceId:n,characteristicId:r,value:arrayBufferToHexStringArray(s)}),e.prototype.onBLECharacteristicValueChange.call(this,{deviceId:o,serviceId:n,characteristicId:r,value:s})},t.prototype.getBluetoothDevices=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,o=this;return tslib_1.__generator(this,(function(n){switch(n.label){case 0:return[4,e.prototype.getBluetoothDevices.call(this)];case 1:return(t=n.sent()).forEach((function(e){e.lastFoundTime=o._deviceLastFoundTime[e.deviceId]})),[2,t]}}))}))},t.prototype.onBluetoothDeviceFound=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var o,n,r=this;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return o=Date.now(),Array.isArray(null==t?void 0:t.devices)&&t.devices.forEach((function(e){r._deviceLastFoundTime[e.deviceId]=o})),[4,e.prototype.onBluetoothDeviceFound.call(this,t)];case 1:return n=s.sent(),this.response2BlueToothChanel("onBluetoothDeviceFound",{devices:n.map(parseAdvertisData)}),[2]}}))}))},t.prototype.startSearch=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return[4,e.prototype.startSearch.call(this,t)];case 1:return o.sent(),this.startCleanupTimer(),[2]}}))}))},t.prototype.searchDevice=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var o;return tslib_1.__generator(this,(function(n){switch(n.label){case 0:return[4,e.prototype.searchDevice.call(this,t)];case 1:return o=n.sent(),this.startCleanupTimer(),[2,o]}}))}))},t.prototype.cleanup=function(t){e.prototype.cleanup.call(this,t),this._mpBleConnectionStateCache={}},t}(base_1.BlueToothAdapter);exports.BlueToothAdapter4Mp=BlueToothAdapter4Mp;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter4Mp=void 0;var tslib_1=require("tslib"),base_1=require("../base"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),_a=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils,arrayBufferToHexStringArray=_a.arrayBufferToHexStringArray,hexToArrayBuffer=_a.hexToArrayBuffer,delay=_a.delay,parseAdvertisData=function(e){var t=tslib_1.__assign({},e);if(e.advertisData)try{t.advertisData=arrayBufferToHexStringArray(e.advertisData)}catch(e){console.error("Parse bluetoothe device advertisData fail",e)}return e.serviceData&&"object"==typeof e.serviceData&&(t.serviceData={},Object.entries(e.serviceData).forEach((function(e){var r=tslib_1.__read(e,2),s=r[0],n=r[1];n instanceof ArrayBuffer&&(t.serviceData[s]=arrayBufferToHexStringArray(n))}))),t},BlueToothAdapter4Mp=function(e){function t(t){var r=t.bluetoothApi,s=tslib_1.__rest(t,["bluetoothApi"]),n=e.call(this,tslib_1.__assign({bluetoothApi:r},s))||this;return n._h5ChanelOpened=!1,n._localDiscoveringInUse=!1,n._h5DiscoveringInUse=!1,n._cleanupTimer=null,n._currentProductId="",n.handleH5WsMessage=null,n.deviceDelayDisconnectQueue=[],n._mpBleConnectionStateCache={},n._h5Websocket&&"function"==typeof n._h5Websocket.on&&(n.on("adapterStateChange",(function(e){var t=e.available,r=e.discovering;n.response2BlueToothChanel("onBluetoothAdapterStateChange",{available:t,discovering:r})})),n._h5Websocket.on("message",(function(e){var t=e.data,r=e.reqId;return tslib_1.__awaiter(n,void 0,void 0,(function(){var e,s,n,o,i,c,a,l,u,h,d,v,p,_,f,y,g,D,B=this;return tslib_1.__generator(this,(function(C){switch(C.label){case 0:if(console.log("bluetooth ws on message",t),"function"==typeof this.handleH5WsMessage)try{if(this.handleH5WsMessage({data:t,reqId:r}))return console.log("h5 message already handled by handleH5WsMessage implement."),[2]}catch(e){console.error("call handleH5WsMessage error",e)}switch(e=t.action,s=t.payload,e){case"reportDeviceConnectStatus":return[3,1];case"syncDeviceConnectStatus":return[3,2];case"bindDevice":return[3,3];case"registryDevice":return[3,7];case"connect":return[3,11];case"disconnect":return[3,12];case"init":return[3,13];case"batchWriteBLECharacteristicValue":return[3,17];case"callApi":return[3,21]}return[3,36];case 1:return n=s.connected,o=s.explorerDeviceId,i=s.deviceId,this.onDeviceConnectStatusChange({connected:n,explorerDeviceId:o,deviceId:i}),[3,36];case 2:return c=s.devices,(f=void 0===c?[]:c).forEach((function(e){var t=e.deviceId,r=e.connected,s=B._mpBleConnectionStateCache[t]||!1;s!==r&&(console.log("[syncDeviceConnectStatus] deviceId="+t+",","h5.connected="+r+", actualConnected="+s),B.response2BlueToothChanel("onBLEConnectionStateChange",{deviceId:t,connected:s}))})),[3,36];case 3:return C.trys.push([3,5,,6]),l=s.deviceName,u=s.productId,[4,this._actions.bindDevice({productId:this.devMode&&u?u:this._currentProductId,deviceName:l})];case 4:return C.sent(),this.response2BlueToothChanel("response",{code:0},r),[3,6];case 5:return a=C.sent(),this.response2BlueToothChanel("response",a,r),[3,6];case 6:return[3,36];case 7:return C.trys.push([7,9,,10]),l=s.deviceName,u=s.productId,[4,this._actions.registerDevice({productId:this.devMode&&u?u:this._currentProductId,deviceName:l})];case 8:return C.sent(),this.response2BlueToothChanel("response",{code:0},r),[3,10];case 9:return h=C.sent(),this.response2BlueToothChanel("response",h,r),[3,10];case 10:return[3,36];case 11:return console.log("h5chanel opened"),this._h5ChanelOpened=!0,[3,36];case 12:return console.log("h5chanel closed"),this._h5ChanelOpened=!1,this._h5DiscoveringInUse&&this.stopBluetoothDevicesDiscovery(!0),[3,36];case 13:return C.trys.push([13,15,,16]),[4,this.init()];case 14:return C.sent(),this.response2BlueToothChanel("response",{code:0},r),[3,16];case 15:return d=C.sent(),this.response2BlueToothChanel("response",d,r),[3,16];case 16:return[3,36];case 17:return C.trys.push([17,19,,20]),[4,this.batchWriteBLECharacteristicValue(s)];case 18:return C.sent(),this.response2BlueToothChanel("response",{},r),[3,20];case 19:return v=C.sent(),console.error("[BlueToothAdapter4Mp] batchWriteBLECharacteristicValue fail",v),this.response2BlueToothChanel("response",this._normalizeError(v),r),[3,20];case 20:return[3,36];case 21:p=s.api,_=s.params,C.label=22;case 22:switch(C.trys.push([22,34,,35]),console.log("call api",p,_),p){case"createBLEConnection":return[3,23];case"getBluetoothDevices":return[3,25];case"writeBLECharacteristicValue":return[3,27];case"startBluetoothDevicesDiscovery":return[3,29];case"stopBluetoothDevicesDiscovery":return[3,31]}return[3,33];case 23:return this.tryCancelDisconnectDevice(_.deviceId),[4,this._bluetoothApi.createBLEConnection(_)];case 24:return C.sent(),this.response2BlueToothChanel("response",{},r),[2];case 25:return[4,this.getBluetoothDevices()];case 26:return f=C.sent(),this.response2BlueToothChanel("response",{devices:f.map(parseAdvertisData)},r),[2];case 27:return y=_.value,console.log("calling writeBLECharacteristicValue",tslib_1.__assign(tslib_1.__assign({},_),{value:hexToArrayBuffer(y)})),[4,this._bluetoothApi.writeBLECharacteristicValue(tslib_1.__assign(tslib_1.__assign({},_),{value:hexToArrayBuffer(y)}))];case 28:return g=C.sent(),this.response2BlueToothChanel("response",g,r),[2];case 29:return[4,this.startBluetoothDevicesDiscovery({isFromH5:!0,interval:1e3})];case 30:return g=C.sent(),this.response2BlueToothChanel("response",g,r),[2];case 31:return[4,this.stopBluetoothDevicesDiscovery(!0)];case 32:return g=C.sent(),this.response2BlueToothChanel("response",g,r),[2];case 33:return[3,35];case 34:return D=C.sent(),console.error("[BlueToothAdapter4Mp] call bluetoothApi fail",D),this.response2BlueToothChanel("response",this._normalizeError(D),r),[2];case 35:return wx[p]&&wx[p](tslib_1.__assign(tslib_1.__assign({},_),{success:function(e){console.log("[BlueToothAdapter4Mp] call wx api success",e),B.response2BlueToothChanel("response",e,r)},fail:function(e){console.log("call api fail",e),B.response2BlueToothChanel("response",B._normalizeError(e),r)}})),[3,36];case 36:return[2]}}))}))}))),n}return tslib_1.__extends(t,e),t.prototype.tryCancelDisconnectDevice=function(e){var t=this.deviceDelayDisconnectQueue.findIndex((function(t){return t.deviceId===e}));if(t>-1){var r=this.deviceDelayDisconnectQueue[t];return clearTimeout(r.timer),this.deviceDelayDisconnectQueue.splice(t,1),console.log("Cancel disconnect device: "+e),!0}return console.log("Try cancel disconnect device: "+e+", but not found in queue, maybe it's already disconnected"),!1},t.prototype.disconnectDevice=function(e,t){var r=this,s=e.deviceId,n=e.explorerDeviceId;void 0===t&&(t=0),console.log("call disconnectDevice",{deviceId:s,explorerDeviceId:n});var o=this._deviceConnectStatusStore.get({deviceId:s,explorerDeviceId:n});if(null==o?void 0:o.connected)if(t>0){var i=setTimeout((function(){console.log("execute closeBLEConnection for deviceId: "+s+" after "+t+"ms"),r._bluetoothApi.closeBLEConnection({deviceId:s})}),t);console.log("Will disconnect device: "+s+" after "+t+"ms..."),this.deviceDelayDisconnectQueue.push({deviceId:s,timer:i})}else this._bluetoothApi.closeBLEConnection({deviceId:s});else console.log("call disconnectDevice, but device maybe not connected",o,this._deviceConnectStatusStore.getAll())},t.prototype.setCurrentProduct=function(e){this._currentProductId=e},t.prototype.startBluetoothDevicesDiscovery=function(t){void 0===t&&(t={});var r=t.isFromH5,s=void 0!==r&&r,n=tslib_1.__rest(t,["isFromH5"]);return s?this._h5DiscoveringInUse=!0:this._localDiscoveringInUse=!0,e.prototype.startBluetoothDevicesDiscovery.call(this,n)},t.prototype.stopBluetoothDevicesDiscovery=function(t){if(void 0===t&&(t=!1),console.log("try call stopBluetoothDevicesDiscovery,\n isFromH5: "+t+", _h5DiscoveringInUse: "+this._h5DiscoveringInUse+",\n _localDiscoveringInUse: "+this._localDiscoveringInUse),t?this._h5DiscoveringInUse=!1:this._localDiscoveringInUse=!1,this._h5DiscoveringInUse||!this._localDiscoveringInUse)return e.prototype.stopBluetoothDevicesDiscovery.call(this)},t.prototype.response2BlueToothChanel=function(e,t,r){return void 0===t&&(t={}),void 0===r&&(r=""),tslib_1.__awaiter(this,void 0,void 0,(function(){var s;return tslib_1.__generator(this,(function(n){switch(n.label){case 0:return n.trys.push([0,2,,3]),this._h5ChanelOpened?[4,this._h5Websocket.send("Response",{action:e,payload:t},{reqId:r})]:[2];case 1:return n.sent(),[3,3];case 2:return s=n.sent(),console.warn("try send bluetooth message fail",s),[3,3];case 3:return[2]}}))}))},t.prototype.startCleanupTimer=function(){var e=this;clearTimeout(this._cleanupTimer),this._cleanupTimer=setTimeout((function(){var t=e._deviceAdapterStore.getAll().filter((function(e){return e.isConnected}));console.log("bluetooth searching or deviceMap not empty, reset cleanup timer",e._discovering,t),e._h5ChanelOpened||e._discovering||t.length?e.startCleanupTimer():e.cleanup()}),3e4)},t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t=this;return tslib_1.__generator(this,(function(r){return[2,e.prototype.init.call(this).then((function(){return t.startCleanupTimer()}))]}))}))},t.prototype.onBleConnectionStateChange=function(t){var r=t.deviceId,s=t.connected;return this.response2BlueToothChanel("onBLEConnectionStateChange",{deviceId:r,connected:s}),this._mpBleConnectionStateCache[r]=s,e.prototype.onBleConnectionStateChange.call(this,{deviceId:r,connected:s})},t.prototype.onBLECharacteristicValueChange=function(t){var r=t.deviceId,s=t.serviceId,n=t.characteristicId,o=t.value;return this.response2BlueToothChanel("onBLECharacteristicValueChange",{deviceId:r,serviceId:s,characteristicId:n,value:arrayBufferToHexStringArray(o)}),e.prototype.onBLECharacteristicValueChange.call(this,{deviceId:r,serviceId:s,characteristicId:n,value:o})},t.prototype.getBluetoothDevices=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t,r=this;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.getBluetoothDevices.call(this)];case 1:return(t=s.sent()).forEach((function(e){e.lastFoundTime=r._deviceLastFoundTime[e.deviceId]})),[2,t]}}))}))},t.prototype.onBluetoothDeviceFound=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var r,s,n=this;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return r=Date.now(),Array.isArray(null==t?void 0:t.devices)&&t.devices.forEach((function(e){n._deviceLastFoundTime[e.deviceId]=r})),[4,e.prototype.onBluetoothDeviceFound.call(this,t)];case 1:return s=o.sent(),this.response2BlueToothChanel("onBluetoothDeviceFound",{devices:s.map(parseAdvertisData)}),[2]}}))}))},t.prototype.startSearch=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return[4,e.prototype.startSearch.call(this,t)];case 1:return r.sent(),this.startCleanupTimer(),[2]}}))}))},t.prototype.searchDevice=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var r;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.searchDevice.call(this,t)];case 1:return r=s.sent(),this.startCleanupTimer(),[2,r]}}))}))},t.prototype.cleanup=function(t){e.prototype.cleanup.call(this,t),this._mpBleConnectionStateCache={}},t.prototype.batchWriteBLECharacteristicValue=function(e){var t=e.deviceId,r=e.serviceId,s=e.characteristicId,n=e.values,o=e.interval;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,i,c,a,l,u,h;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:console.log("[BlueToothAdapter4Mp] batchWriteBLECharacteristicValue ",{deviceId:t,serviceId:r,characteristicId:s,values:n,interval:o}),e=!0,d.label=1;case 1:d.trys.push([1,9,10,11]),i=tslib_1.__values(n),c=i.next(),d.label=2;case 2:return c.done?[3,8]:(a=c.value,e?(e=!1,[3,5]):[3,3]);case 3:return Number.isSafeInteger(o)&&o>0?[4,delay(o)]:[3,5];case 4:d.sent(),d.label=5;case 5:return[4,this._bluetoothApi.writeBLECharacteristicValue({deviceId:t,serviceId:r,characteristicId:s,value:hexToArrayBuffer(a)})];case 6:d.sent(),d.label=7;case 7:return c=i.next(),[3,2];case 8:return[3,11];case 9:return l=d.sent(),u={error:l},[3,11];case 10:try{c&&!c.done&&(h=i.return)&&h.call(i)}finally{if(u)throw u.error}return[7];case 11:return[2]}}))}))},t}(base_1.BlueToothAdapter);exports.BlueToothAdapter4Mp=BlueToothAdapter4Mp;
//# sourceMappingURL=BlueToothAdapter4Mp.js.map

@@ -20,2 +20,4 @@ import { DeviceAdapter } from '../base';

communicationMode: import("../base").CommunicationMode;
_batchWriteEnabled: boolean;
_batchWriteInterval: number;
_getNotifyId({ serviceId }?: {

@@ -95,2 +97,6 @@ serviceId?: string;

onScanAdvert(params: any): void;
batchWrite(_data: (string | ArrayBuffer)[], _options: {
writeId?: string;
serviceId?: string;
}): Promise<void>;
_cleanupMap: {};

@@ -124,2 +130,4 @@ _normalizeError(error: any): any;

communicationMode: import("../base").CommunicationMode;
_batchWriteEnabled: boolean;
_batchWriteInterval: number;
_getNotifyId({ serviceId }?: {

@@ -199,2 +207,6 @@ serviceId?: string;

onScanAdvert(params: any): void;
batchWrite(_data: (string | ArrayBuffer)[], _options: {
writeId?: string;
serviceId?: string;
}): Promise<void>;
_cleanupMap: {};

@@ -201,0 +213,0 @@ _normalizeError(error: any): any;

{
"name": "qcloud-iotexplorer-bluetooth-adapter",
"version": "0.12.6",
"version": "0.12.7",
"description": "",

@@ -26,3 +26,3 @@ "main": "lib/index.js",

},
"gitHead": "2946157ebe30d9f3353a9eeed0bf1e811f81147b"
"gitHead": "bb94f36227bb64568e17a44a4b177a6f59a85b67"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc