qcloud-iotexplorer-bluetooth-adapter
Advanced tools
Comparing version 0.0.1 to 0.0.2
/// <reference types="miniprogram-api-typings" /> | ||
import { BlueToothBase } from './BlueToothBase'; | ||
import { DeviceAdapter, DeviceInfo } from './DeviceAdapter'; | ||
import EventEmitter from "../libs/event-emmiter"; | ||
export interface BlueToothActions { | ||
@@ -26,2 +27,3 @@ initProductIds: () => Promise<{ | ||
bluetoothApi: any; | ||
h5Websocket: EventEmitter; | ||
} | ||
@@ -49,3 +51,4 @@ export interface SearchDeviceBaseParams { | ||
export declare class BlueToothAdapter extends BlueToothBase { | ||
constructor({ deviceAdapters, actions, bluetoothApi, }: BlueToothAdapterProps); | ||
constructor({ deviceAdapters, actions, bluetoothApi, h5Websocket, }: BlueToothAdapterProps); | ||
_h5Websocket: any; | ||
_bluetoothApi: any; | ||
@@ -52,0 +55,0 @@ _actions: BlueToothActions; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter=void 0;var tslib_1=require("tslib"),utillib_1=require("../libs/utillib"),BlueToothBase_1=require("./BlueToothBase"),DeviceAdapter_1=require("./DeviceAdapter"),nativeBluetoothApi_1=tslib_1.__importDefault(require("./nativeBluetoothApi")),throttle_1=require("../libs/throttle"),BlueToothAdapter=function(e){function t(t){var i=t.deviceAdapters,o=void 0===i?[]:i,r=t.actions,n=t.bluetoothApi,c=e.call(this)||this;c._bluetoothApi={},c._actions={},c._deviceAdapterMap={},c._productIdMap={},c._deviceMap={},c._inited=!1,c._available=!1,c._discovering=!1,c._onBluetoothDeviceFoundHandler=null,c._initPromise=null,c._searchDevicePromise=null;for(var s=0,a=o.length;s<a;s++)Object.prototype.isPrototypeOf.call(DeviceAdapter_1.DeviceAdapter,o[s])?o[s].serviceId?c._deviceAdapterMap[o[s].serviceId]=o[s]:console.error("非法的设备连接器,未配置serviceId",o[s]):console.error("非法的设备连接器",o[s]);return utillib_1.isEmpty(c._deviceAdapterMap)&&console.error("无合法的deviceAdapter"),c._bluetoothApi=n||nativeBluetoothApi_1.default,c._actions=r,c}return tslib_1.__extends(t,e),t.prototype._filterDevices=function(e){var t=this,i=e.devices,o=void 0===i?[]:i,r=e.serviceIds,n=e.deviceName,c=e.ignoreDeviceIds,s=void 0===c?[]:c,a=e.ignoreServiceIds,l=void 0===a?[]:a,u=e.extendInfo,d=void 0===u?{}:u;r&&r.length||(r=this._getSupportServiceIds());var h={};l&&l.length&&(l.forEach((function(e){return h[e]=!0})),r=r.filter((function(e){return!h[e]}))),console.log("support serviceIds",r);for(var v=[],p=r.map((function(e){return t._deviceAdapterMap[e].deviceFilter})),_=function(e,t){if(s.find((function(t){return o[e].deviceId===t})))return{value:void 0};for(var i=void 0,r=0,c=p.length;r<c;r++)if(i=p[r](o[r],d),n){if(i&&i.deviceName===n)return{value:[i]}}else if(i){v.push(i);break}},f=0,b=o.length;f<b;f++){var g=_(f);if("object"==typeof g)return g.value}return v},t.prototype._getSupportServiceIds=function(){return Object.keys(this._deviceAdapterMap)},t.prototype.startBluetoothDevicesDiscovery=function(){return this._bluetoothApi.startBluetoothDevicesDiscovery()},t.prototype.stopBluetoothDevicesDiscovery=function(e){return void 0===e&&(e=!1),this._bluetoothApi.stopBluetoothDevicesDiscovery()},t.prototype.cleanup=function(t){var i=this;e.prototype.cleanup.call(this,t),t||(console.log("cleanup bluetooth adapter"),this._discovering&&this.stopBluetoothDevicesDiscovery(),this._bluetoothApi.closeBluetoothAdapter(),console.log("manually disconnect all device",this._deviceMap),Object.keys(this._deviceMap).forEach((function(e){i._deviceMap[e]&&i._deviceMap[e].disconnectDevice()})))},t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._initPromise||(this._initPromise=new Promise((function(t,i){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,o,r,n,c,s,a=this;return tslib_1.__generator(this,(function(l){switch(l.label){case 0:if(l.trys.push([0,4,,5]),this._inited){if(this._available)return this._initPromise=null,[2,t()];throw{errCode:10001}}return[4,this.initProductIds()];case 1:return l.sent(),e=function(e){var i=e.available,o=e.discovering;console.log("onBluetoothAdapterStateChange",{available:i,discovering:o}),a._available=i,a._discovering=o,a.emit("adapterStateChange",{available:i,discovering:o}),i?(a._inited=!0,t(),a._initPromise=null):a.cleanup()},o=function(e){return a.onBleConnectionStateChange(e)},r=function(e){return a.onBLECharacteristicValueChange(e)},n=throttle_1.throttle(1e3,(function(e){return a.onBluetoothDeviceFound(e)})),this._bluetoothApi.onBluetoothAdapterStateChange(e),this._bluetoothApi.onBLEConnectionStateChange(o),this._bluetoothApi.onBLECharacteristicValueChange(r),this._bluetoothApi.onBluetoothDeviceFound(n),this.addCleanupTask("init",(function(){a._available=a._discovering=a._inited=!1,a._initPromise=null,a._bluetoothApi.offBluetoothAdapterStateChange(e),a._bluetoothApi.offBLEConnectionStateChange(o),a._bluetoothApi.offBLECharacteristicValueChange(r),a._bluetoothApi.offBluetoothDeviceFound(n),a._bluetoothApi.closeBluetoothAdapter()})),[4,this._bluetoothApi.openBluetoothAdapter()];case 2:return l.sent(),c=e,[4,this._bluetoothApi.getBluetoothAdapterState()];case 3:return c.apply(void 0,[l.sent()]),[3,5];case 4:return s=l.sent(),this._available=!1,this._inited=!1,this._initPromise=null,i(this._normalizeError(s)),[3,5];case 5:return[2]}}))}))})))]}))}))},t.prototype.initProductIds=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 e=this,[4,this._actions.initProductIds()];case 1:return e._productIdMap=t.sent(),[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.deviceId,i=e.connected;console.log("onBLEConnectionStateChange",t,i);var o=this.getDeviceAdapter(t);o?o.onBleConnectionStateChange({connected:i}):console.warn("on bLEConnectionStateChange, but no adapter")},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.deviceId,i=e.serviceId,o=e.characteristicId,r=e.value;console.log("onBLECharacteristicValueChange",t,i,o,r);var n=this._deviceMap[t];if(n)return n.onBLECharacteristicValueChange({serviceId:i,characteristicId:o,value:r});console.warn("on onBLECharacteristicValueChange, but no adapter")},t.prototype.getBluetoothDevices=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return[4,this._bluetoothApi.getBluetoothDevices()];case 1:return[2,e.sent().devices.filter((function(e){return"未知设备"!==e.name}))]}}))}))},t.prototype.onBluetoothDeviceFound=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.getBluetoothDevices()];case 1:e=t.sent();try{"function"==typeof this._onBluetoothDeviceFoundHandler&&this._onBluetoothDeviceFoundHandler(e)}catch(e){console.error("_onBluetoothDeviceFoundHandler error",e)}return[2,e]}}))}))},t.prototype.startSearch=function(e){var t=e.serviceId,i=e.serviceIds,o=e.ignoreDeviceIds,r=void 0===o?[]:o,n=e.ignoreServiceIds,c=void 0===n?[]:n,s=e.onSearch,a=void 0===s?utillib_1.noop:s,l=e.onError,u=void 0===l?utillib_1.noop:l,d=e.timeout,h=void 0===d?2e4:d,v=e.extendInfo,p=void 0===v?{}:v;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,n,s,l=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:t&&!i&&(i=[t]),e=0,o=function(e){l.stopSearch(),u(e)},d.label=1;case 1:return d.trys.push([1,3,,4]),[4,this.startBluetoothDevicesDiscovery()];case 2:return d.sent(),this._onBluetoothDeviceFoundHandler=function(t){try{var n=l._filterDevices({devices:t,serviceIds:i,ignoreDeviceIds:r,ignoreServiceIds:c,extendInfo:p});e=n.length,a(n)}catch(e){console.log("onSearch error",e),o(l._normalizeError(e))}},this.onBluetoothDeviceFound(),n=function(e){e.available||o(l._normalizeError({errCode:10001}))},this.on("adapterStateChange",n),this.addCleanupTask("startSearch",(function(){l._onBluetoothDeviceFoundHandler=null,l.off("adapterStateChange",n)})),setTimeout((function(){e||o("未发现设备,请确认设备已开启")}),h),[3,4];case 3:throw s=d.sent(),this.cleanup("startSearch"),this._normalizeError(s);case 4:return[2]}}))}))},t.prototype.stopSearch=function(){this.cleanup("startSearch"),this.stopBluetoothDevicesDiscovery()},t.prototype.searchDevice=function(e){var t=this,i=e.serviceId,o=e.serviceIds,r=e.deviceName,n=e.explorerDeviceId,c=e.ignoreDeviceIds,s=void 0===c?[]:c,a=e.timeout,l=void 0===a?5e3:a,u=e.extendInfo,d=void 0===u?{}:u;if(i&&!o&&(o=[i]),!r&&n){var h=tslib_1.__read(n.split("/"),2)[1];r=h}return console.log("searching for explorerDeviceId => ",r),this._searchDevicePromise||(this._searchDevicePromise=new Promise((function(e,i){return tslib_1.__awaiter(t,void 0,void 0,(function(){var t,n,c,a=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:t=function(e){a.stopBluetoothDevicesDiscovery(),i(a._normalizeError(e)),a._searchDevicePromise=null},n=function(t){a.stopBluetoothDevicesDiscovery(),e(t),a._searchDevicePromise=null},this._onBluetoothDeviceFoundHandler=function(e){try{var i=a._filterDevices({devices:e,serviceIds:o,deviceName:r,ignoreDeviceIds:s,extendInfo:d});console.log("matchedDevices: ",i),i.length>0&&(console.log("doFindDevice",i[0]),n(i[0]))}catch(e){t(e)}},u.label=1;case 1:return u.trys.push([1,3,,4]),[4,this.startBluetoothDevicesDiscovery()];case 2:return u.sent(),console.log("startBluetoothDevicesDiscovery succ"),this.onBluetoothDeviceFound(),setTimeout((function(){n(null)}),l),[3,4];case 3:return c=u.sent(),t(c),[3,4];case 4:return[2]}}))}))})))},t.prototype.getDeviceAdapter=function(e){return this._deviceMap[e]},t.prototype.connectDevice=function(e){var t=e.deviceId,i=e.serviceId,o=e.mac,r=e.deviceName,n=e.name,c=e.productId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,s,a,l=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:o&&console.warn("[DEPRECATED] mac is deprecated, please use deviceName instead."),r=r||o,u.label=1;case 1:if(u.trys.push([1,3,,4]),!(e=this._deviceAdapterMap[i]))throw"无匹配serviceId为"+i+"的 deviceAdapter";return this._deviceMap[t]&&this._deviceMap[t].isConnected?(console.log("find device adapter",this._deviceMap[t]),[2,this._deviceMap[t]]):[4,(s=this._deviceMap[t]=new e({deviceId:t,deviceName:r,productId:c||this._productIdMap[i],name:n,actions:this._actions,bluetoothApi:this._bluetoothApi})).connectDevice()];case 2:return u.sent(),console.log("deviceConnected"),s.on("disconnect",(function(){console.log("ondisconnect, cleanup adapter",s),delete l._deviceMap[t]})),console.log("return adapter"),[2,s];case 3:return a=u.sent(),delete this._deviceMap[t],[2,Promise.reject(a)];case 4:return[2]}}))}))},t}(BlueToothBase_1.BlueToothBase);exports.BlueToothAdapter=BlueToothAdapter; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter=void 0;var tslib_1=require("tslib"),utillib_1=require("../libs/utillib"),BlueToothBase_1=require("./BlueToothBase"),DeviceAdapter_1=require("./DeviceAdapter"),nativeBluetoothApi_1=tslib_1.__importDefault(require("./nativeBluetoothApi")),throttle_1=require("../libs/throttle"),BlueToothAdapter=function(e){function t(t){var i=t.deviceAdapters,o=void 0===i?[]:i,r=t.actions,n=t.bluetoothApi,c=t.h5Websocket,s=e.call(this)||this;s._bluetoothApi={},s._actions={},s._deviceAdapterMap={},s._productIdMap={},s._deviceMap={},s._inited=!1,s._available=!1,s._discovering=!1,s._onBluetoothDeviceFoundHandler=null,s._initPromise=null,s._searchDevicePromise=null;for(var a=0,l=o.length;a<l;a++)Object.prototype.isPrototypeOf.call(DeviceAdapter_1.DeviceAdapter,o[a])?o[a].serviceId?s._deviceAdapterMap[o[a].serviceId]=o[a]:console.error("非法的设备连接器,未配置serviceId",o[a]):console.error("非法的设备连接器",o[a]);return utillib_1.isEmpty(s._deviceAdapterMap)&&console.error("无合法的deviceAdapter"),s._h5Websocket=c,s._bluetoothApi=n||nativeBluetoothApi_1.default,s._actions=r,s}return tslib_1.__extends(t,e),t.prototype._filterDevices=function(e){var t=this,i=e.devices,o=void 0===i?[]:i,r=e.serviceIds,n=e.deviceName,c=e.ignoreDeviceIds,s=void 0===c?[]:c,a=e.ignoreServiceIds,l=void 0===a?[]:a,u=e.extendInfo,d=void 0===u?{}:u;r&&r.length||(r=this._getSupportServiceIds());var h={};l&&l.length&&(l.forEach((function(e){return h[e]=!0})),r=r.filter((function(e){return!h[e]}))),console.log("support serviceIds",r);for(var v=[],p=r.map((function(e){return t._deviceAdapterMap[e].deviceFilter})),_=function(e,t){if(s.find((function(t){return o[e].deviceId===t})))return{value:void 0};for(var i=void 0,r=0,c=p.length;r<c;r++)if(i=p[r](o[r],d),n){if(i&&i.deviceName===n)return{value:[i]}}else if(i){v.push(i);break}},f=0,b=o.length;f<b;f++){var g=_(f);if("object"==typeof g)return g.value}return v},t.prototype._getSupportServiceIds=function(){return Object.keys(this._deviceAdapterMap)},t.prototype.startBluetoothDevicesDiscovery=function(){return this._bluetoothApi.startBluetoothDevicesDiscovery()},t.prototype.stopBluetoothDevicesDiscovery=function(e){return void 0===e&&(e=!1),this._bluetoothApi.stopBluetoothDevicesDiscovery()},t.prototype.cleanup=function(t){var i=this;e.prototype.cleanup.call(this,t),t||(console.log("cleanup bluetooth adapter"),this._discovering&&this.stopBluetoothDevicesDiscovery(),this._bluetoothApi.closeBluetoothAdapter(),console.log("manually disconnect all device",this._deviceMap),Object.keys(this._deviceMap).forEach((function(e){i._deviceMap[e]&&i._deviceMap[e].disconnectDevice()})))},t.prototype.init=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._initPromise||(this._initPromise=new Promise((function(t,i){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,o,r,n,c,s,a=this;return tslib_1.__generator(this,(function(l){switch(l.label){case 0:if(l.trys.push([0,4,,5]),this._inited){if(this._available)return this._initPromise=null,[2,t()];throw{errCode:10001}}return[4,this.initProductIds()];case 1:return l.sent(),e=function(e){var i=e.available,o=e.discovering;console.log("onBluetoothAdapterStateChange",{available:i,discovering:o}),a._available=i,a._discovering=o,a.emit("adapterStateChange",{available:i,discovering:o}),i?(a._inited=!0,t(),a._initPromise=null):a.cleanup()},o=function(e){return a.onBleConnectionStateChange(e)},r=function(e){return a.onBLECharacteristicValueChange(e)},n=throttle_1.throttle(1e3,(function(e){return a.onBluetoothDeviceFound(e)})),this._bluetoothApi.onBluetoothAdapterStateChange(e),this._bluetoothApi.onBLEConnectionStateChange(o),this._bluetoothApi.onBLECharacteristicValueChange(r),this._bluetoothApi.onBluetoothDeviceFound(n),this.addCleanupTask("init",(function(){a._available=a._discovering=a._inited=!1,a._initPromise=null,a._bluetoothApi.offBluetoothAdapterStateChange(e),a._bluetoothApi.offBLEConnectionStateChange(o),a._bluetoothApi.offBLECharacteristicValueChange(r),a._bluetoothApi.offBluetoothDeviceFound(n),a._bluetoothApi.closeBluetoothAdapter()})),[4,this._bluetoothApi.openBluetoothAdapter()];case 2:return l.sent(),c=e,[4,this._bluetoothApi.getBluetoothAdapterState()];case 3:return c.apply(void 0,[l.sent()]),[3,5];case 4:return s=l.sent(),this._available=!1,this._inited=!1,this._initPromise=null,i(this._normalizeError(s)),[3,5];case 5:return[2]}}))}))})))]}))}))},t.prototype.initProductIds=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 e=this,[4,this._actions.initProductIds()];case 1:return e._productIdMap=t.sent(),[2]}}))}))},t.prototype.onBleConnectionStateChange=function(e){var t=e.deviceId,i=e.connected;console.log("onBLEConnectionStateChange",t,i);var o=this.getDeviceAdapter(t);o?o.onBleConnectionStateChange({connected:i}):console.warn("on bLEConnectionStateChange, but no adapter")},t.prototype.onBLECharacteristicValueChange=function(e){var t=e.deviceId,i=e.serviceId,o=e.characteristicId,r=e.value;console.log("onBLECharacteristicValueChange",t,i,o,r);var n=this._deviceMap[t];if(n)return n.onBLECharacteristicValueChange({serviceId:i,characteristicId:o,value:r});console.warn("on onBLECharacteristicValueChange, but no adapter")},t.prototype.getBluetoothDevices=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:return[4,this._bluetoothApi.getBluetoothDevices()];case 1:return[2,e.sent().devices.filter((function(e){return"未知设备"!==e.name}))]}}))}))},t.prototype.onBluetoothDeviceFound=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.getBluetoothDevices()];case 1:e=t.sent();try{"function"==typeof this._onBluetoothDeviceFoundHandler&&this._onBluetoothDeviceFoundHandler(e)}catch(e){console.error("_onBluetoothDeviceFoundHandler error",e)}return[2,e]}}))}))},t.prototype.startSearch=function(e){var t=e.serviceId,i=e.serviceIds,o=e.ignoreDeviceIds,r=void 0===o?[]:o,n=e.ignoreServiceIds,c=void 0===n?[]:n,s=e.onSearch,a=void 0===s?utillib_1.noop:s,l=e.onError,u=void 0===l?utillib_1.noop:l,d=e.timeout,h=void 0===d?2e4:d,v=e.extendInfo,p=void 0===v?{}:v;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,o,n,s,l=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:t&&!i&&(i=[t]),e=0,o=function(e){l.stopSearch(),u(e)},d.label=1;case 1:return d.trys.push([1,3,,4]),[4,this.startBluetoothDevicesDiscovery()];case 2:return d.sent(),this._onBluetoothDeviceFoundHandler=function(t){try{var n=l._filterDevices({devices:t,serviceIds:i,ignoreDeviceIds:r,ignoreServiceIds:c,extendInfo:p});e=n.length,a(n)}catch(e){console.log("onSearch error",e),o(l._normalizeError(e))}},this.onBluetoothDeviceFound(),n=function(e){e.available||o(l._normalizeError({errCode:10001}))},this.on("adapterStateChange",n),this.addCleanupTask("startSearch",(function(){l._onBluetoothDeviceFoundHandler=null,l.off("adapterStateChange",n)})),setTimeout((function(){e||o("未发现设备,请确认设备已开启")}),h),[3,4];case 3:throw s=d.sent(),this.cleanup("startSearch"),this._normalizeError(s);case 4:return[2]}}))}))},t.prototype.stopSearch=function(){this.cleanup("startSearch"),this.stopBluetoothDevicesDiscovery()},t.prototype.searchDevice=function(e){var t=this,i=e.serviceId,o=e.serviceIds,r=e.deviceName,n=e.explorerDeviceId,c=e.ignoreDeviceIds,s=void 0===c?[]:c,a=e.timeout,l=void 0===a?5e3:a,u=e.extendInfo,d=void 0===u?{}:u;if(i&&!o&&(o=[i]),!r&&n){var h=tslib_1.__read(n.split("/"),2)[1];r=h}return console.log("searching for explorerDeviceId => ",r),this._searchDevicePromise||(this._searchDevicePromise=new Promise((function(e,i){return tslib_1.__awaiter(t,void 0,void 0,(function(){var t,n,c,a=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:t=function(e){a.stopBluetoothDevicesDiscovery(),i(a._normalizeError(e)),a._searchDevicePromise=null},n=function(t){a.stopBluetoothDevicesDiscovery(),e(t),a._searchDevicePromise=null},this._onBluetoothDeviceFoundHandler=function(e){try{var i=a._filterDevices({devices:e,serviceIds:o,deviceName:r,ignoreDeviceIds:s,extendInfo:d});console.log("matchedDevices: ",i),i.length>0&&(console.log("doFindDevice",i[0]),n(i[0]))}catch(e){t(e)}},u.label=1;case 1:return u.trys.push([1,3,,4]),[4,this.startBluetoothDevicesDiscovery()];case 2:return u.sent(),console.log("startBluetoothDevicesDiscovery succ"),this.onBluetoothDeviceFound(),setTimeout((function(){n(null)}),l),[3,4];case 3:return c=u.sent(),t(c),[3,4];case 4:return[2]}}))}))})))},t.prototype.getDeviceAdapter=function(e){return this._deviceMap[e]},t.prototype.connectDevice=function(e){var t=e.deviceId,i=e.serviceId,o=e.mac,r=e.deviceName,n=e.name,c=e.productId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var e,s,a,l=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:o&&console.warn("[DEPRECATED] mac is deprecated, please use deviceName instead."),r=r||o,u.label=1;case 1:if(u.trys.push([1,3,,4]),!(e=this._deviceAdapterMap[i]))throw"无匹配serviceId为"+i+"的 deviceAdapter";return this._deviceMap[t]&&this._deviceMap[t].isConnected?(console.log("find device adapter",this._deviceMap[t]),[2,this._deviceMap[t]]):[4,(s=this._deviceMap[t]=new e({deviceId:t,deviceName:r,productId:c||this._productIdMap[i],name:n,actions:this._actions,bluetoothApi:this._bluetoothApi})).connectDevice()];case 2:return u.sent(),console.log("deviceConnected"),s.on("disconnect",(function(){console.log("ondisconnect, cleanup adapter",s),delete l._deviceMap[t]})),console.log("return adapter"),[2,s];case 3:return a=u.sent(),delete this._deviceMap[t],[2,Promise.reject(a)];case 4:return[2]}}))}))},t}(BlueToothBase_1.BlueToothBase);exports.BlueToothAdapter=BlueToothAdapter; | ||
//# sourceMappingURL=BlueToothAdapter.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var utillib_1=require("../libs/utillib"),pifyApiArr=["closeBluetoothAdapter","stopBluetoothDevicesDiscovery","openBluetoothAdapter","getBluetoothAdapterState","getBluetoothDevices","startBluetoothDevicesDiscovery","closeBLEConnection","createBLEConnection","getBLEDeviceServices","getBLEDeviceCharacteristics","notifyBLECharacteristicValueChange","offBluetoothAdapterStateChange","offBLEConnectionStateChange","offBLECharacteristicValueChange","offBluetoothDeviceFound","writeBLECharacteristicValue","setBLEMTU"],callbackApiArr=["onBluetoothAdapterStateChange","onBLEConnectionStateChange","onBLECharacteristicValueChange","onBluetoothDeviceFound"],apis={};pifyApiArr.forEach((function(e){apis[e]=function(t){return utillib_1.pify(wx[e])(t)}})),callbackApiArr.forEach((function(e){apis[e]=function(t){return wx[e](t)}})),exports.default=apis; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tslib_1=require("tslib"),utillib_1=require("../libs/utillib"),pifyApiArr=["closeBluetoothAdapter","stopBluetoothDevicesDiscovery","openBluetoothAdapter","getBluetoothAdapterState","getBluetoothDevices","startBluetoothDevicesDiscovery","closeBLEConnection","getBLEDeviceServices","getBLEDeviceCharacteristics","notifyBLECharacteristicValueChange","offBluetoothAdapterStateChange","offBLEConnectionStateChange","offBLECharacteristicValueChange","offBluetoothDeviceFound","writeBLECharacteristicValue","setBLEMTU"],callbackApiArr=["onBluetoothAdapterStateChange","onBLEConnectionStateChange","onBLECharacteristicValueChange","onBluetoothDeviceFound"],apis={};pifyApiArr.forEach((function(e){apis[e]=function(t){return utillib_1.pify(wx[e])(t)}})),callbackApiArr.forEach((function(e){apis[e]=function(t){return wx[e](t)}})),apis.createBLEConnection=function(e,t){return void 0===t&&(t=!1),tslib_1.__awaiter(void 0,void 0,void 0,(function(){var n,o;return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return r.trys.push([0,2,,9]),[4,utillib_1.pify(wx.createBLEConnection)(e)];case 1:return r.sent(),[3,9];case 2:if(n=r.sent(),!(!t&&n&&n.errMsg&&n.errMsg.indexOf("already connect")>-1))return[3,7];console.log("already connect, try disconnect"),r.label=3;case 3:return r.trys.push([3,5,,6]),[4,apis.closeBLEConnection(e)];case 4:return r.sent(),console.log("disconnect success",e),[3,6];case 5:return o=r.sent(),console.warn("disconnect fail",o),[3,6];case 6:return console.log("try connect again",e),[2,apis.createBLEConnection(e,!0)];case 7:return[2,Promise.reject(n)];case 8:return[3,9];case 9:return[2]}}))}))},exports.default=apis; | ||
//# sourceMappingURL=nativeBluetoothApi.js.map |
@@ -1,3 +0,4 @@ | ||
import { BlueToothAdapter } from "../core"; | ||
import { BlueToothAdapter, BlueToothAdapterProps } from "../core"; | ||
export declare class BlueToothAdapter4H5 extends BlueToothAdapter { | ||
constructor({ bluetoothApi, h5Websocket, ...props }: BlueToothAdapterProps); | ||
} |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter4H5=void 0;var tslib_1=require("tslib"),core_1=require("../core"),BlueToothAdapter4H5=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return tslib_1.__extends(t,e),t}(core_1.BlueToothAdapter);exports.BlueToothAdapter4H5=BlueToothAdapter4H5; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter4H5=void 0;var tslib_1=require("tslib"),core_1=require("../core"),BlueToothBridge_1=require("./BlueToothBridge"),BlueToothAdapter4H5=function(e){function t(t){t.bluetoothApi;var o=t.h5Websocket,r=tslib_1.__rest(t,["bluetoothApi","h5Websocket"]);return e.call(this,tslib_1.__assign(tslib_1.__assign({},r),{h5Websocket:o,bluetoothApi:new BlueToothBridge_1.BlueToothBridge({h5Websocket:o})}))||this}return tslib_1.__extends(t,e),t}(core_1.BlueToothAdapter);exports.BlueToothAdapter4H5=BlueToothAdapter4H5; | ||
//# sourceMappingURL=BlueToothAdapter4H5.js.map |
@@ -14,1 +14,2 @@ export function arrayBufferToHexStringArray(buffer: any): string[]; | ||
export function appendParams(url: any, data: any): any; | ||
export function tryCallHandler(context: any, eventName: any, ...params: any[]): void; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.appendParams=exports.pify=exports.isObject=exports.usePromise=exports.isEmpty=exports.noop=exports.hexToArrayBuffer=exports.arrayBufferToHexStringArray=exports.delay=void 0;var tslib_1=require("tslib");function arrayBufferToHexStringArray(r){try{if("string"==typeof r){var e=[""];return r.split("").forEach((function(r,t){e[e.length-1]+=r,t%2&&e.push("")})),e.splice(e.length-1,1),e}if("[object ArrayBuffer]"!==Object.prototype.toString.call(r))throw"invalid array buffer";for(var t=new DataView(r),n=[],o=0,i=t.byteLength;o<i;o++){var s=(255&t.getUint8(o)).toString(16);s=1===s.length?"0"+s:s,n.push(s.toUpperCase())}return n}catch(r){return console.error("arrayBufferToHexStringArray error",r),[]}}function hexToArrayBuffer(r){return new Uint8Array(r.match(/[\da-f]{2}/gi).map((function(r){return parseInt(r,16)}))).buffer}function noop(){}function isEmpty(r){if(!r)return!0;if(Array.isArray(r))return 0===r.length;for(var e in r)return!1;return!0}function usePromise(){var r,e;return{promise:new Promise((function(t,n){r=t,e=n})),resolve:r,reject:e}}function isObject(r){var e=typeof r;return"function"===e||"object"===e&&!!r}exports.delay=function(r){return new Promise((function(e){return setTimeout((function(){return e()}),r)}))},exports.arrayBufferToHexStringArray=arrayBufferToHexStringArray,exports.hexToArrayBuffer=hexToArrayBuffer,exports.noop=noop,exports.isEmpty=isEmpty,exports.usePromise=usePromise,exports.isObject=isObject,exports.pify=function(r,e){return void 0===e&&(e=wx),function(t){for(var n=[],o=1;o<arguments.length;o++)n[o-1]=arguments[o];return new Promise((function(o,i){r.call.apply(r,tslib_1.__spread([e,tslib_1.__assign(tslib_1.__assign({},t),{success:o,fail:i})],n))}))}},exports.appendParams=function(r,e){var t=[];for(var n in e)void 0!==e[n]&&(isObject(e[n])&&(e[n]=JSON.stringify(e[n])),t.push(n+"="+encodeURIComponent(e[n])));return t.length?(r.indexOf("?")>-1?r+"&":r+"?")+t.join("&"):r}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.tryCallHandler=exports.appendParams=exports.pify=exports.isObject=exports.usePromise=exports.isEmpty=exports.noop=exports.hexToArrayBuffer=exports.arrayBufferToHexStringArray=exports.delay=void 0;var tslib_1=require("tslib");function arrayBufferToHexStringArray(r){try{if("string"==typeof r){var e=[""];return r.split("").forEach((function(r,t){e[e.length-1]+=r,t%2&&e.push("")})),e.splice(e.length-1,1),e}if("[object ArrayBuffer]"!==Object.prototype.toString.call(r))throw"invalid array buffer";for(var t=new DataView(r),n=[],o=0,i=t.byteLength;o<i;o++){var s=(255&t.getUint8(o)).toString(16);s=1===s.length?"0"+s:s,n.push(s.toUpperCase())}return n}catch(r){return console.error("arrayBufferToHexStringArray error",r),[]}}function hexToArrayBuffer(r){return new Uint8Array(r.match(/[\da-f]{2}/gi).map((function(r){return parseInt(r,16)}))).buffer}function noop(){}function isEmpty(r){if(!r)return!0;if(Array.isArray(r))return 0===r.length;for(var e in r)return!1;return!0}function usePromise(){var r,e;return{promise:new Promise((function(t,n){r=t,e=n})),resolve:r,reject:e}}function isObject(r){var e=typeof r;return"function"===e||"object"===e&&!!r}exports.delay=function(r){return new Promise((function(e){return setTimeout((function(){return e()}),r)}))},exports.arrayBufferToHexStringArray=arrayBufferToHexStringArray,exports.hexToArrayBuffer=hexToArrayBuffer,exports.noop=noop,exports.isEmpty=isEmpty,exports.usePromise=usePromise,exports.isObject=isObject,exports.pify=function(r,e){return void 0===e&&(e=wx),function(t){for(var n=[],o=1;o<arguments.length;o++)n[o-1]=arguments[o];return new Promise((function(o,i){r.call.apply(r,tslib_1.__spread([e,tslib_1.__assign(tslib_1.__assign({},t),{success:o,fail:i})],n))}))}},exports.appendParams=function(r,e){var t=[];for(var n in e)void 0!==e[n]&&(isObject(e[n])&&(e[n]=JSON.stringify(e[n])),t.push(n+"="+encodeURIComponent(e[n])));return t.length?(r.indexOf("?")>-1?r+"&":r+"?")+t.join("&"):r},exports.tryCallHandler=function(r,e){for(var t=[],n=2;n<arguments.length;n++)t[n-2]=arguments[n];"function"==typeof r["_"+e+"Handler"]&&r["_"+e+"Handler"].apply(r,tslib_1.__spread(t))}; | ||
//# sourceMappingURL=utillib.js.map |
import { BlueToothAdapter, BlueToothAdapterProps, SearchDeviceParams, StartSearchParams } from "../core"; | ||
import EventEmitter from "../libs/event-emmiter"; | ||
export interface BlueToothAdapter4MpProps extends BlueToothAdapterProps { | ||
h5Websocket?: EventEmitter; | ||
devMode?: boolean; | ||
@@ -17,6 +15,5 @@ } | ||
_h5DiscoveringInUse: boolean; | ||
_h5Websocket: any; | ||
_cleanupTimer: any; | ||
_currentProductId: string; | ||
constructor({ h5Websocket, devMode, ...props }: BlueToothAdapter4MpProps); | ||
constructor({ devMode, ...props }: BlueToothAdapter4MpProps); | ||
setCurrentProduct(productId: any): void; | ||
@@ -23,0 +20,0 @@ startBluetoothDevicesDiscovery(isFromH5?: boolean): any; |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter4Mp=void 0;var tslib_1=require("tslib"),core_1=require("../core"),utillib_1=require("../libs/utillib"),parseAdvertisData=function(e){var t=tslib_1.__assign({},e);if(t.advertisData)try{t.advertisData=utillib_1.arrayBufferToHexStringArray(t.advertisData)}catch(e){console.error("Parse bluetoothe device advertisData fail",e)}return t},BlueToothAdapter4Mp=function(e){function t(t){var s=t.h5Websocket,r=t.devMode,o=tslib_1.__rest(t,["h5Websocket","devMode"]),n=e.call(this,o)||this;return n._h5ChanelOpened=!1,n._localDiscoveringInUse=!1,n._h5DiscoveringInUse=!1,n._h5Websocket=null,n._cleanupTimer=null,n._currentProductId="",s&&"function"==typeof s.on&&(n._h5Websocket=s,n.on("adapterStateChange",(function(e){var t=e.available,s=e.discovering;n.response2BlueToothChanel("onBluetoothAdapterStateChange",{available:t,discovering:s})})),s.on("message",(function(e){var t=e.data,s=e.reqId;return tslib_1.__awaiter(n,void 0,void 0,(function(){var e,o,n,i,a,c,l,u,h,p,v,d,_,g=this;return tslib_1.__generator(this,(function(B){switch(B.label){case 0:switch(console.log("bluetooth ws on message",t),e=t.action,o=t.payload,e){case"bindDevice":return[3,1];case"registryDevice":return[3,5];case"connect":return[3,9];case"disconnect":return[3,10];case"init":return[3,11];case"callApi":return[3,15]}return[3,28];case 1:return B.trys.push([1,3,,4]),i=o.deviceName,a=o.productId,[4,this._actions.bindDevice({deviceId:(r&&a?a:this._currentProductId)+"/"+i})];case 2:return B.sent(),this.response2BlueToothChanel("response",{code:0},s),[3,4];case 3:return n=B.sent(),this.response2BlueToothChanel("response",n,s),[3,4];case 4:return[3,28];case 5:return B.trys.push([5,7,,8]),i=o.deviceName,a=o.productId,[4,this._actions.registerDevice({deviceId:(r&&a?a:this._currentProductId)+"/"+i})];case 6:return B.sent(),this.response2BlueToothChanel("response",{code:0},s),[3,8];case 7:return c=B.sent(),this.response2BlueToothChanel("response",c,s),[3,8];case 8:return[3,28];case 9:return console.log("h5chanel opened"),this._h5ChanelOpened=!0,[3,28];case 10:return console.log("h5chanel closed"),this._h5ChanelOpened=!1,this._h5DiscoveringInUse&&this.stopBluetoothDevicesDiscovery(!0),[3,28];case 11:return B.trys.push([11,13,,14]),[4,this.init()];case 12:return B.sent(),this.response2BlueToothChanel("response",{code:0},s),[3,14];case 13:return l=B.sent(),this.response2BlueToothChanel("response",l,s),[3,14];case 14:return[3,28];case 15:u=o.api,h=o.params,B.label=16;case 16:switch(B.trys.push([16,26,,27]),console.log("call api",u,h),u){case"getBluetoothDevices":return[3,17];case"writeBLECharacteristicValue":return[3,19];case"startBluetoothDevicesDiscovery":return[3,21];case"stopBluetoothDevicesDiscovery":return[3,23]}return[3,25];case 17:return[4,this.getBluetoothDevices()];case 18:return p=B.sent(),this.response2BlueToothChanel("response",{devices:p.map(parseAdvertisData)},s),[2];case 19:return v=h.value,console.log("calling writeBLECharacteristicValue",tslib_1.__assign(tslib_1.__assign({},h),{value:utillib_1.hexToArrayBuffer(v)})),[4,this._bluetoothApi.writeBLECharacteristicValue(tslib_1.__assign(tslib_1.__assign({},h),{value:utillib_1.hexToArrayBuffer(v)}))];case 20:return d=B.sent(),this.response2BlueToothChanel("response",d,s),[2];case 21:return[4,this.startBluetoothDevicesDiscovery(!0)];case 22:return d=B.sent(),this.response2BlueToothChanel("response",d,s),[2];case 23:return[4,this.stopBluetoothDevicesDiscovery(!0)];case 24:return d=B.sent(),this.response2BlueToothChanel("response",d,s),[2];case 25:return[3,27];case 26:return _=B.sent(),console.log("call api fail",_),this.response2BlueToothChanel("response",this._normalizeError(_),s),[3,27];case 27:return wx[u]&&wx[u](tslib_1.__assign(tslib_1.__assign({},h),{success:function(e){console.log("call api success",e),g.response2BlueToothChanel("response",e,s)},fail:function(e){console.log("call api fail",e),g.response2BlueToothChanel("response",g._normalizeError(e),s)}})),[3,28];case 28:return[2]}}))}))}))),n}return tslib_1.__extends(t,e),t.prototype.setCurrentProduct=function(e){this._currentProductId=e},t.prototype.startBluetoothDevicesDiscovery=function(t){return void 0===t&&(t=!1),t?this._h5DiscoveringInUse=!0:this._localDiscoveringInUse=!0,e.prototype.startBluetoothDevicesDiscovery.call(this)},t.prototype.stopBluetoothDevicesDiscovery=function(t){if(void 0===t&&(t=!1),console.log("try call stopBluetoothDevicesDiscovery, isFromH5: "+t+", _h5DiscoveringInUse: "+this._h5DiscoveringInUse+", _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,s){return void 0===t&&(t={}),void 0===s&&(s=""),tslib_1.__awaiter(this,void 0,void 0,(function(){var r;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),this._h5ChanelOpened?(console.log("response2BlueToothChanel",e,t),[4,this._h5Websocket.send("Response",{action:e,payload:t},{reqId:s})]):(console.log("h5 chanel not opened"),[2]);case 1:return o.sent(),[3,3];case 2:return r=o.sent(),console.warn("try send bluetooth message fail",r),[3,3];case 3:return[2]}}))}))},t.prototype.startCleanupTimer=function(){var e=this;clearTimeout(this._cleanupTimer),console.log("start cleanup timer"),this._cleanupTimer=setTimeout((function(){console.log("bluetooth searching or deviceMap not empty, reset cleanup timer",e._discovering,e._deviceMap),e._h5ChanelOpened||e._discovering||!utillib_1.isEmpty(e._deviceMap)?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(s){return[2,e.prototype.init.call(this).then((function(){return t.startCleanupTimer()}))]}))}))},t.prototype.onBleConnectionStateChange=function(t){var s=t.deviceId,r=t.connected;return this.response2BlueToothChanel("onBLEConnectionStateChange",{deviceId:s,connected:r}),e.prototype.onBleConnectionStateChange.call(this,{deviceId:s,connected:r})},t.prototype.onBLECharacteristicValueChange=function(t){var s=t.deviceId,r=t.serviceId,o=t.characteristicId,n=t.value;return this.response2BlueToothChanel("onBLECharacteristicValueChange",{deviceId:s,serviceId:r,characteristicId:o,value:utillib_1.arrayBufferToHexStringArray(n)}),e.prototype.onBLECharacteristicValueChange.call(this,{deviceId:s,serviceId:r,characteristicId:o,value:n})},t.prototype.onBluetoothDeviceFound=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.onBluetoothDeviceFound.call(this)];case 1:return t=s.sent(),this.response2BlueToothChanel("onBluetoothDeviceFound",{devices:t.map(parseAdvertisData)}),[2]}}))}))},t.prototype.startSearch=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.startSearch.call(this,t)];case 1:return s.sent(),this.startCleanupTimer(),[2]}}))}))},t.prototype.searchDevice=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.searchDevice.call(this,t)];case 1:return s.sent(),this.startCleanupTimer(),[2]}}))}))},t}(core_1.BlueToothAdapter);exports.BlueToothAdapter4Mp=BlueToothAdapter4Mp; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.BlueToothAdapter4Mp=void 0;var tslib_1=require("tslib"),core_1=require("../core"),utillib_1=require("../libs/utillib"),parseAdvertisData=function(e){var t=tslib_1.__assign({},e);if(t.advertisData)try{t.advertisData=utillib_1.arrayBufferToHexStringArray(t.advertisData)}catch(e){console.error("Parse bluetoothe device advertisData fail",e)}return t},BlueToothAdapter4Mp=function(e){function t(t){var s=t.devMode,r=tslib_1.__rest(t,["devMode"]),o=e.call(this,r)||this;return o._h5ChanelOpened=!1,o._localDiscoveringInUse=!1,o._h5DiscoveringInUse=!1,o._cleanupTimer=null,o._currentProductId="",o._h5Websocket&&"function"==typeof o._h5Websocket.on&&(o.on("adapterStateChange",(function(e){var t=e.available,s=e.discovering;o.response2BlueToothChanel("onBluetoothAdapterStateChange",{available:t,discovering:s})})),o._h5Websocket.on("message",(function(e){var t=e.data,r=e.reqId;return tslib_1.__awaiter(o,void 0,void 0,(function(){var e,o,n,i,a,c,l,u,h,p,v,d,_,g=this;return tslib_1.__generator(this,(function(B){switch(B.label){case 0:switch(console.log("bluetooth ws on message",t),e=t.action,o=t.payload,e){case"bindDevice":return[3,1];case"registryDevice":return[3,5];case"connect":return[3,9];case"disconnect":return[3,10];case"init":return[3,11];case"callApi":return[3,15]}return[3,28];case 1:return B.trys.push([1,3,,4]),i=o.deviceName,a=o.productId,[4,this._actions.bindDevice({deviceId:(s&&a?a:this._currentProductId)+"/"+i})];case 2:return B.sent(),this.response2BlueToothChanel("response",{code:0},r),[3,4];case 3:return n=B.sent(),this.response2BlueToothChanel("response",n,r),[3,4];case 4:return[3,28];case 5:return B.trys.push([5,7,,8]),i=o.deviceName,a=o.productId,[4,this._actions.registerDevice({deviceId:(s&&a?a:this._currentProductId)+"/"+i})];case 6:return B.sent(),this.response2BlueToothChanel("response",{code:0},r),[3,8];case 7:return c=B.sent(),this.response2BlueToothChanel("response",c,r),[3,8];case 8:return[3,28];case 9:return console.log("h5chanel opened"),this._h5ChanelOpened=!0,[3,28];case 10:return console.log("h5chanel closed"),this._h5ChanelOpened=!1,this._h5DiscoveringInUse&&this.stopBluetoothDevicesDiscovery(!0),[3,28];case 11:return B.trys.push([11,13,,14]),[4,this.init()];case 12:return B.sent(),this.response2BlueToothChanel("response",{code:0},r),[3,14];case 13:return l=B.sent(),this.response2BlueToothChanel("response",l,r),[3,14];case 14:return[3,28];case 15:u=o.api,h=o.params,B.label=16;case 16:switch(B.trys.push([16,26,,27]),console.log("call api",u,h),u){case"getBluetoothDevices":return[3,17];case"writeBLECharacteristicValue":return[3,19];case"startBluetoothDevicesDiscovery":return[3,21];case"stopBluetoothDevicesDiscovery":return[3,23]}return[3,25];case 17:return[4,this.getBluetoothDevices()];case 18:return p=B.sent(),this.response2BlueToothChanel("response",{devices:p.map(parseAdvertisData)},r),[2];case 19:return v=h.value,console.log("calling writeBLECharacteristicValue",tslib_1.__assign(tslib_1.__assign({},h),{value:utillib_1.hexToArrayBuffer(v)})),[4,this._bluetoothApi.writeBLECharacteristicValue(tslib_1.__assign(tslib_1.__assign({},h),{value:utillib_1.hexToArrayBuffer(v)}))];case 20:return d=B.sent(),this.response2BlueToothChanel("response",d,r),[2];case 21:return[4,this.startBluetoothDevicesDiscovery(!0)];case 22:return d=B.sent(),this.response2BlueToothChanel("response",d,r),[2];case 23:return[4,this.stopBluetoothDevicesDiscovery(!0)];case 24:return d=B.sent(),this.response2BlueToothChanel("response",d,r),[2];case 25:return[3,27];case 26:return _=B.sent(),console.log("call api fail",_),this.response2BlueToothChanel("response",this._normalizeError(_),r),[3,27];case 27:return wx[u]&&wx[u](tslib_1.__assign(tslib_1.__assign({},h),{success:function(e){console.log("call api success",e),g.response2BlueToothChanel("response",e,r)},fail:function(e){console.log("call api fail",e),g.response2BlueToothChanel("response",g._normalizeError(e),r)}})),[3,28];case 28:return[2]}}))}))}))),o}return tslib_1.__extends(t,e),t.prototype.setCurrentProduct=function(e){this._currentProductId=e},t.prototype.startBluetoothDevicesDiscovery=function(t){return void 0===t&&(t=!1),t?this._h5DiscoveringInUse=!0:this._localDiscoveringInUse=!0,e.prototype.startBluetoothDevicesDiscovery.call(this)},t.prototype.stopBluetoothDevicesDiscovery=function(t){if(void 0===t&&(t=!1),console.log("try call stopBluetoothDevicesDiscovery, isFromH5: "+t+", _h5DiscoveringInUse: "+this._h5DiscoveringInUse+", _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,s){return void 0===t&&(t={}),void 0===s&&(s=""),tslib_1.__awaiter(this,void 0,void 0,(function(){var r;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),this._h5ChanelOpened?(console.log("response2BlueToothChanel",e,t),[4,this._h5Websocket.send("Response",{action:e,payload:t},{reqId:s})]):(console.log("h5 chanel not opened"),[2]);case 1:return o.sent(),[3,3];case 2:return r=o.sent(),console.warn("try send bluetooth message fail",r),[3,3];case 3:return[2]}}))}))},t.prototype.startCleanupTimer=function(){var e=this;clearTimeout(this._cleanupTimer),console.log("start cleanup timer"),this._cleanupTimer=setTimeout((function(){console.log("bluetooth searching or deviceMap not empty, reset cleanup timer",e._discovering,e._deviceMap),e._h5ChanelOpened||e._discovering||!utillib_1.isEmpty(e._deviceMap)?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(s){return[2,e.prototype.init.call(this).then((function(){return t.startCleanupTimer()}))]}))}))},t.prototype.onBleConnectionStateChange=function(t){var s=t.deviceId,r=t.connected;return this.response2BlueToothChanel("onBLEConnectionStateChange",{deviceId:s,connected:r}),e.prototype.onBleConnectionStateChange.call(this,{deviceId:s,connected:r})},t.prototype.onBLECharacteristicValueChange=function(t){var s=t.deviceId,r=t.serviceId,o=t.characteristicId,n=t.value;return this.response2BlueToothChanel("onBLECharacteristicValueChange",{deviceId:s,serviceId:r,characteristicId:o,value:utillib_1.arrayBufferToHexStringArray(n)}),e.prototype.onBLECharacteristicValueChange.call(this,{deviceId:s,serviceId:r,characteristicId:o,value:n})},t.prototype.onBluetoothDeviceFound=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.onBluetoothDeviceFound.call(this)];case 1:return t=s.sent(),this.response2BlueToothChanel("onBluetoothDeviceFound",{devices:t.map(parseAdvertisData)}),[2]}}))}))},t.prototype.startSearch=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.startSearch.call(this,t)];case 1:return s.sent(),this.startCleanupTimer(),[2]}}))}))},t.prototype.searchDevice=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return[4,e.prototype.searchDevice.call(this,t)];case 1:return s.sent(),this.startCleanupTimer(),[2]}}))}))},t}(core_1.BlueToothAdapter);exports.BlueToothAdapter4Mp=BlueToothAdapter4Mp; | ||
//# sourceMappingURL=BlueToothAdapter4Mp.js.map |
{ | ||
"name": "qcloud-iotexplorer-bluetooth-adapter", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "", | ||
@@ -9,3 +9,3 @@ "main": "lib/index.js", | ||
"build": "npm run build:lib" | ||
}, | ||
}, | ||
"author": "xiaoyuze88@gmail.com", | ||
@@ -12,0 +12,0 @@ "license": "MIT", |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances in 1 package
109620
51
517
14