2 lines
7.4 KiB
JavaScript
2 lines
7.4 KiB
JavaScript
"use strict";var e=Object.defineProperty,t=(t,s,a)=>(((t,s,a)=>{s in t?e(t,s,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[s]=a})(t,"symbol"!=typeof s?s+"":s,a),a);const s=require("../common/vendor.js"),a=class e{constructor(t,s,a){const o=e.resultsMap.get(t);o?(this.code=t,this.message=a||o.message,this.data=s||o.data):(this.code=t,this.message=a||"",this.data=s||{})}static get Success(){return new e(e.codes.Success)}static get Fail(){return new e(e.codes.Fail)}static get NotMoreData(){return new e(e.codes.NotMoreData)}static get NotAvailableBluetooth(){return new e(e.codes.NotAvailableBluetooth)}static get NotAvailableBluetoothPermission(){return new e(e.codes.NotAvailableBluetoothPermission)}static get NotAvailableWeChatNearbyDevicesPermission(){return new e(e.codes.NotAvailableWeChatNearbyDevicesPermission)}static get NotAvailableWeChatLocationPermission(){return new e(e.codes.NotAvailableWeChatLocationPermission)}static get NotAvailableWeChatNearbyDevicesEmpty(){return new e(e.codes.NotAvailableWeChatNearbyDevicesEmpty)}static get NotAvailableWeChatBluetoothPermission(){return new e(e.codes.NotAvailableWeChatBluetoothPermission)}static get DeviceHasBeenReset(){return new e(e.codes.DeviceHasBeenReset)}static get NotRegisteredLock(){return new e(e.codes.NotRegisteredLock)}static get NotTokenLock(){return new e(e.codes.NotTokenLock)}static get NotMoreKeyLock(){return new e(e.codes.NotMoreKeyLock)}static get ReadyHasKeyLock(){return new e(e.codes.ReadyHasKeyLock)}static get ReadyHasPassword(){return new e(e.codes.ReadyHasPassword)}};t(a,"codes",{Success:0,Fail:-1,NotMoreData:-10,NotAvailableBluetooth:-20,NotAvailableBluetoothPermission:-21,NotAvailableWeChatNearbyDevicesPermission:-22,NotAvailableWeChatLocationPermission:-23,NotAvailableWeChatNearbyDevicesEmpty:-24,NotAvailableWeChatBluetoothPermission:-25,DeviceHasBeenReset:-30,NotRegisteredLock:4,NotTokenLock:6,NotMoreKeyLock:12,ReadyHasKeyLock:15,ReadyHasPassword:251}),t(a,"resultsMap",new Map([[a.codes.Success,{message:"成功",data:{}}],[a.codes.Fail,{message:"失败",data:{}}],[a.codes.NotMoreData,{message:"没有更多数据",data:{}}],[a.codes.NotAvailableBluetooth,{message:"蓝牙尚未打开,请先打开蓝牙",data:{}}],[a.codes.NotAvailableBluetoothPermission,{message:"小程序蓝牙功能被禁用,请打开小程序蓝牙权限",data:{}}],[a.codes.NotAvailableWeChatNearbyDevicesPermission,{message:"蓝牙功能需要附近设备权限,请前往设置开启微信的附近设备权限后再试",data:{}}],[a.codes.NotAvailableWeChatLocationPermission,{message:"蓝牙功能需要定位权限,请前往设置开启微信的定位权限后再试",data:{}}],[a.codes.NotAvailableWeChatNearbyDevicesEmpty,{message:"蓝牙功能需要定位服务,请前往设置开启定位服务后再试",data:{}}],[a.codes.NotAvailableWeChatBluetoothPermission,{message:"微信的蓝牙权限被禁用,请前往设置开启微信的蓝牙权限后再试",data:{}}],[a.codes.DeviceHasBeenReset,{message:"设备已被重置",data:{}}],[a.codes.NotRegisteredLock,{message:"用户在锁端未注册",data:{}}],[a.codes.NotTokenLock,{message:"用户在锁端token失效",data:{}}],[a.codes.NotMoreKeyLock,{message:"锁端钥匙数量已达上限",data:{}}],[a.codes.ReadyHasKeyLock,{message:"用户已是锁端用户",data:{}}],[a.codes.ReadyHasPassword,{message:"该密码已存在",data:{}}]]));let o=a;async function i(e,t){if(1e4===e.errCode){const e=await new Promise((e=>{s.index.openBluetoothAdapter({success(){e(o.Success)},async fail(t){e(await i(t))}})}));return e.code===o.Success.code?await t():e}return 10001===e.errCode?3===e.state?o.NotAvailableWeChatBluetoothPermission:o.NotAvailableBluetooth:3===e.errno?o.NotAvailableWeChatNearbyDevicesPermission:103===e.errno?o.NotAvailableBluetoothPermission:1509008===e.errno?o.NotAvailableWeChatLocationPermission:"openBluetoothAdapter:fail already opened"===e.errMsg?o.Success:o.Fail}function c(){return new Promise((e=>{s.index.startBluetoothDevicesDiscovery({success(){e(o.Success)},async fail(t){e(await i(t,c))}})}))}function r(){return new Promise((e=>{s.index.getBluetoothDevices({success(t){e(new o(o.Success.code,t.devices))},async fail(t){e(await i(t,r))}})}))}function n(){return new Promise((e=>{s.index.stopBluetoothDevicesDiscovery({success(){e(o.Success)},async fail(t){e(await i(t))}})}))}function l(e,t=0){return new Promise((a=>{s.index.createBLEConnection({deviceId:e,timeout:1e4,async success(){const t=await d(e);await u(e,t.data.serviceId,t.data.notifyCharacteristicId),a(t)},async fail(s){if(1509007===s.errno){const t=await d(e);await u(e,t.data.serviceId,t.data.notifyCharacteristicId),a(t)}else 1509001===s.errno&&t<1?a(o.Fail):a(t<1?await l(e,t+1):o.Fail)}})}))}async function d(e){const{code:t,data:a}=await function(e){return new Promise((t=>{s.index.getBLEDeviceServices({deviceId:e,success(e){let s;for(let t=0;t<e.services.length;t++)-1!==e.services[t].uuid.indexOf("FFF0")&&(s=e.services[t].uuid);t(s?new o(o.Success.code,{serviceId:s}):o.Fail)},fail(){t(o.Fail)}})}))}(e);if(t===o.Success.code){const{serviceId:t}=a,{code:i,data:{notifyCharacteristicId:c,writeCharacteristicId:r}}=await function(e,t){return new Promise((a=>{s.index.getBLEDeviceCharacteristics({deviceId:e,serviceId:t,success(e){let t,s;for(let a=0;a<e.characteristics.length;a++){const o=e.characteristics[a];o.properties.notify&&(t=o.uuid),o.properties.write&&(s=o.uuid)}a(t&&s?new o(o.Success.code,{notifyCharacteristicId:t,writeCharacteristicId:s}):o.Fail)},fail(){a(o.Fail)}})}))}(e,t);return i===o.Success.code?new o(o.Success.code,{deviceId:e,serviceId:t,notifyCharacteristicId:c,writeCharacteristicId:r}):o.Fail}return o.Fail}function u(e,t,a){return new Promise((i=>{s.index.notifyBLECharacteristicValueChange({deviceId:e,serviceId:t,characteristicId:a,state:!0,success(){i(o.Success)},fail(){i(o.Fail)}})}))}exports.Result=o,exports.closeBLEConnection=function(e){return new Promise((t=>{s.index.closeBLEConnection({deviceId:e,success(){console.log("断开连接成功"),t(o.Success)},fail(){console.log("断开连接失败"),t(o.Fail)}})}))},exports.closeBluetoothAdapter=function(){s.index.closeBluetoothAdapter()},exports.createBLEConnection=l,exports.getBluetoothDevices=r,exports.offBluetoothAdapterStateChange=function(){s.index.offBluetoothAdapterStateChange()},exports.onBLECharacteristicValueChange=function(e){s.index.onBLECharacteristicValueChange((t=>{e(t)}))},exports.searchAndConnectDevice=function(e,t=!0){let s,a;return new Promise((async i=>{const d=await c();if(d.code===o.Success.code){let c=!1;a=setTimeout((async()=>{await n(),clearInterval(s),i(c?o.Fail:o.NotAvailableWeChatNearbyDevicesEmpty)}),10500),s=setInterval((async()=>{var d,u;const v=await r();if(v.code===o.Success.code){const r=v.data;!1===c&&r.length>0&&(c=!0);for(let c=0;c<r.length;c++)if((null==(d=r[c])?void 0:d.name)===e){const e=null==(u=r[c])?void 0:u.advertisServiceUUIDs[0];if(e&&"758824"===e.slice(2,8)){if(await n(),clearTimeout(a),clearInterval(s),"00"===e.slice(30,32)&&t)i(o.DeviceHasBeenReset);else{const e=await l(r[c].deviceId);i(e)}break}}}else i(v)}),1e3)}else i(d)}))},exports.startBluetoothDevicesDiscovery=c,exports.stopBluetoothDevicesDiscovery=n,exports.writeBLECharacteristicValue=function(e,t,a,i){return new Promise((c=>{const r=Math.ceil(i.length/20);let n=0;for(let l=0;l<r;l++){const d=i.slice(20*l,l===r-1?i.length:20*(l+1));s.index.writeBLECharacteristicValue({deviceId:e,serviceId:t,characteristicId:a,value:d.buffer,success(){n++,n===r&&c(o.Success)},fail(){c(o.Fail)}})}}))};
|