Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-bluetooth-adapter

Package Overview
Dependencies
Maintainers
5
Versions
164
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qcloud-iotexplorer-bluetooth-adapter - npm Package Compare versions

Comparing version 0.12.19 to 0.12.20

10

lib/base/DeviceAdapter.d.ts

@@ -147,3 +147,11 @@ /// <reference types="miniprogram-api-typings" />

mtu: number;
}): any;
}): Promise<{
mtu: number;
}>;
getBLEMTU(params?: {
deviceId?: string;
writeType?: 'write' | 'writeNoResponse';
}): Promise<{
mtu: number;
}>;
getBLEDeviceRSSI(): any;

@@ -150,0 +158,0 @@ destroy(): 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,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;
"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.getBLEMTU=function(e){return void 0===e&&(e={}),this._bluetoothApi.getBLEMTU(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

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

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _systemInfoCache,tslib_1=require("tslib"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),pify=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.pify,pifyApiArr=["closeBluetoothAdapter","stopBluetoothDevicesDiscovery","readBLECharacteristicValue","getBluetoothAdapterState","getBluetoothDevices","startBluetoothDevicesDiscovery","closeBLEConnection","getBLEDeviceServices","getBLEDeviceCharacteristics","notifyBLECharacteristicValueChange","writeBLECharacteristicValue","setBLEMTU"],callbackApiArr=["onBluetoothAdapterStateChange","onBLEConnectionStateChange","onBLECharacteristicValueChange","onBluetoothDeviceFound","onBLEPeripheralConnectionStateChanged","offBluetoothAdapterStateChange","offBLEConnectionStateChange","offBLECharacteristicValueChange","offBluetoothDeviceFound","offBLEPeripheralConnectionStateChanged"],pifyPeripheralApiArr=["addService","close","removeService","startAdvertising","stopAdvertising","writeCharacteristicValue"],callbackPeripheralApiArr=["onCharacteristicReadRequest","onCharacteristicSubscribed","onCharacteristicUnsubscribed","onCharacteristicWriteRequest","offCharacteristicReadRequest","offCharacteristicSubscribed","offCharacteristicUnsubscribed","offCharacteristicWriteRequest"],apis={};pifyApiArr.forEach((function(e){apis[e]=function(t){return pify(wx[e])(t)}})),callbackApiArr.forEach((function(e){apis[e]=function(t){return wx[e](t)}})),apis.createBLEConnection=function(e,t){var r=(void 0===t?{}:t).isRetry,i=void 0!==r&&r;return tslib_1.__awaiter(void 0,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,pify(wx.createBLEConnection)(e)];case 1:return r.sent(),[3,3];case 2:return t=r.sent(),!i&&t&&t.errMsg&&t.errMsg.indexOf("already connect")>-1?(console.log("device is already connected, resolve directly"),[2,Promise.resolve()]):[2,Promise.reject(t)];case 3:return[2]}}))}))},apis.createBLEPeripheralServer=function(e){return tslib_1.__awaiter(void 0,void 0,void 0,(function(){var t,r;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return[4,pify(wx.createBLEPeripheralServer)(e)];case 1:return t=i.sent().server,r={},pifyPeripheralApiArr.forEach((function(e){r[e]=function(r){return pify(t[e],t)(r)}})),callbackPeripheralApiArr.forEach((function(e){r[e]=function(r){return t[e](r)}})),[2,r]}}))}))},apis.openBluetoothAdapter=function(e){return tslib_1.__awaiter(void 0,void 0,void 0,(function(){var t,r;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,apis.getSystemInfo({useCache:!1})];case 1:if((null==(t=i.sent())?void 0:t.platform)&&t.platform.toLowerCase().indexOf("android")>-1){if(!(null==t?void 0:t.locationEnabled))throw{code:"Location:Disable",msg:"请打开系统地理位置开关"};if(!(null==t?void 0:t.locationAuthorized))throw{code:"Location:UnAuthorized",msg:"请到系统设置授予微信使用地理位置权限"}}return[4,pify(wx.openBluetoothAdapter)(e)];case 2:return i.sent(),[3,4];case 3:if(3===(null==(r=i.sent())?void 0:r.state))throw{code:null==r?void 0:r.errCode,msg:"请到系统设置授权微信使用蓝牙权限"};throw r;case 4:return[2]}}))}))},apis.getSystemInfo=function(e){var t=(void 0===e?{}:e).useCache,r=void 0===t||t;return tslib_1.__awaiter(void 0,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return _systemInfoCache&&r?[2,_systemInfoCache]:[4,pify(wx.getSystemInfo)()];case 1:return[2,_systemInfoCache=e.sent()]}}))}))},exports.default=apis;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _systemInfoCache,tslib_1=require("tslib"),qcloud_iotexplorer_appdev_sdk_1=require("qcloud-iotexplorer-appdev-sdk"),compatPify=qcloud_iotexplorer_appdev_sdk_1.AppDevSdk.utils.compatPify,pifyApiArr=["closeBluetoothAdapter","stopBluetoothDevicesDiscovery","readBLECharacteristicValue","getBluetoothAdapterState","getBluetoothDevices","startBluetoothDevicesDiscovery","closeBLEConnection","getBLEDeviceServices","getBLEDeviceCharacteristics","notifyBLECharacteristicValueChange","writeBLECharacteristicValue","setBLEMTU","getBLEMTU"],callbackApiArr=["onBluetoothAdapterStateChange","onBLEConnectionStateChange","onBLECharacteristicValueChange","onBluetoothDeviceFound","onBLEPeripheralConnectionStateChanged","offBluetoothAdapterStateChange","offBLEConnectionStateChange","offBLECharacteristicValueChange","offBluetoothDeviceFound","offBLEPeripheralConnectionStateChanged"],pifyPeripheralApiArr=["addService","close","removeService","startAdvertising","stopAdvertising","writeCharacteristicValue"],callbackPeripheralApiArr=["onCharacteristicReadRequest","onCharacteristicSubscribed","onCharacteristicUnsubscribed","onCharacteristicWriteRequest","offCharacteristicReadRequest","offCharacteristicSubscribed","offCharacteristicUnsubscribed","offCharacteristicWriteRequest"],apis={};pifyApiArr.forEach((function(e){apis[e]=function(t){return compatPify(wx[e])(t)}})),callbackApiArr.forEach((function(e){apis[e]=function(t){return wx[e](t)}})),apis.createBLEConnection=function(e,t){var r=(void 0===t?{}:t).isRetry,i=void 0!==r&&r;return tslib_1.__awaiter(void 0,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,compatPify(wx.createBLEConnection)(e)];case 1:return r.sent(),[3,3];case 2:return t=r.sent(),!i&&t&&t.errMsg&&t.errMsg.indexOf("already connect")>-1?(console.log("device is already connected, resolve directly"),[2,Promise.resolve()]):[2,Promise.reject(t)];case 3:return[2]}}))}))},apis.createBLEPeripheralServer=function(e){return tslib_1.__awaiter(void 0,void 0,void 0,(function(){var t,r;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return[4,compatPify(wx.createBLEPeripheralServer)(e)];case 1:return t=i.sent().server,r={},pifyPeripheralApiArr.forEach((function(e){r[e]=function(r){return compatPify(t[e],t)(r)}})),callbackPeripheralApiArr.forEach((function(e){r[e]=function(r){return t[e](r)}})),[2,r]}}))}))},apis.openBluetoothAdapter=function(e){return tslib_1.__awaiter(void 0,void 0,void 0,(function(){var t,r;return tslib_1.__generator(this,(function(i){switch(i.label){case 0:return i.trys.push([0,3,,4]),[4,apis.getSystemInfo({useCache:!1})];case 1:if((null==(t=i.sent())?void 0:t.platform)&&t.platform.toLowerCase().indexOf("android")>-1){if(!(null==t?void 0:t.locationEnabled))throw{code:"Location:Disable",msg:"请打开系统地理位置开关"};if(!(null==t?void 0:t.locationAuthorized))throw{code:"Location:UnAuthorized",msg:"请到系统设置授予微信使用地理位置权限"}}return[4,compatPify(wx.openBluetoothAdapter)(e)];case 2:return i.sent(),[3,4];case 3:if(3===(null==(r=i.sent())?void 0:r.state))throw{code:null==r?void 0:r.errCode,msg:"请到系统设置授权微信使用蓝牙权限"};throw r;case 4:return[2]}}))}))},apis.getSystemInfo=function(e){var t=(void 0===e?{}:e).useCache,r=void 0===t||t;return tslib_1.__awaiter(void 0,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return _systemInfoCache&&r?[2,_systemInfoCache]:[4,compatPify(wx.getSystemInfo)()];case 1:return[2,_systemInfoCache=e.sent()]}}))}))},exports.default=apis;
//# sourceMappingURL=nativeBluetoothApi.js.map

@@ -93,3 +93,11 @@ import { DeviceAdapter } from '../base';

mtu: number;
}): any;
}): Promise<{
mtu: number;
}>;
getBLEMTU(params?: {
deviceId?: string;
writeType?: "write" | "writeNoResponse";
}): Promise<{
mtu: number;
}>;
getBLEDeviceRSSI(): any;

@@ -200,3 +208,11 @@ destroy(): void;

mtu: number;
}): any;
}): Promise<{
mtu: number;
}>;
getBLEMTU(params?: {
deviceId?: string;
writeType?: "write" | "writeNoResponse";
}): Promise<{
mtu: number;
}>;
getBLEDeviceRSSI(): any;

@@ -203,0 +219,0 @@ destroy(): void;

@@ -91,3 +91,11 @@ import { DeviceAdapter } from '../base';

mtu: number;
}): any;
}): Promise<{
mtu: number;
}>;
getBLEMTU(params?: {
deviceId?: string;
writeType?: "write" | "writeNoResponse";
}): Promise<{
mtu: number;
}>;
getBLEDeviceRSSI(): any;

@@ -200,3 +208,11 @@ destroy(): void;

mtu: number;
}): any;
}): Promise<{
mtu: number;
}>;
getBLEMTU(params?: {
deviceId?: string;
writeType?: "write" | "writeNoResponse";
}): Promise<{
mtu: number;
}>;
getBLEDeviceRSSI(): any;

@@ -203,0 +219,0 @@ destroy(): void;

{
"name": "qcloud-iotexplorer-bluetooth-adapter",
"version": "0.12.19",
"version": "0.12.20",
"description": "",

@@ -19,4 +19,4 @@ "main": "lib/index.js",

"lodash.uniqwith": "^4.5.0",
"qcloud-iotexplorer-appdev-sdk": "^2.3.12",
"qcloud-iotexplorer-common-libs": "^0.12.14",
"qcloud-iotexplorer-appdev-sdk": "^2.3.13",
"qcloud-iotexplorer-common-libs": "^0.12.15",
"tslib": "^2.0.1"

@@ -27,3 +27,3 @@ },

},
"gitHead": "bb1bad2ea0f91ce15b1dadc287a0bb25d17feb07"
"gitHead": "d208071b4debbdfa750bbe038599f9151748bf0b"
}

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc