mini_program_gizwits_sdk 3.6.4 → 3.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +3 -3
- package/package.json +1 -1
- package/yarn-error.log +6901 -0
- package/dist/src/GizLog.d.ts +0 -18
- package/dist/src/crc/calculators/crc16modbus.d.ts +0 -2
- package/dist/src/crc/calculators/crc32.d.ts +0 -2
- package/dist/src/crc/crc16modbus.d.ts +0 -7
- package/dist/src/crc/crc32.d.ts +0 -7
- package/dist/src/crc/create_buffer.d.ts +0 -3
- package/dist/src/crc/define_crc.d.ts +0 -7
- package/dist/src/crc/index.d.ts +0 -19
- package/dist/src/crc/types.d.ts +0 -11
- package/dist/src/errorCode.d.ts +0 -3
- package/dist/src/globalData.d.ts +0 -2
- package/dist/src/handler/EventListener.d.ts +0 -9
- package/dist/src/handler/ble.d.ts +0 -105
- package/dist/src/handler/lan.d.ts +0 -78
- package/dist/src/handler/socket.d.ts +0 -151
- package/dist/src/openApiRequest.d.ts +0 -3
- package/dist/src/productConfigFileManage.d.ts +0 -11
- package/dist/src/protocol/Bind.d.ts +0 -7
- package/dist/src/protocol/DeviceInfo.d.ts +0 -11
- package/dist/src/protocol/DiscoverUDP.d.ts +0 -17
- package/dist/src/protocol/GetDeviceStatus.d.ts +0 -5
- package/dist/src/protocol/Login.d.ts +0 -12
- package/dist/src/protocol/Ntp.d.ts +0 -5
- package/dist/src/protocol/OTA/AwaitNoti.d.ts +0 -7
- package/dist/src/protocol/OTA/CheckCanOTA.d.ts +0 -10
- package/dist/src/protocol/OTA/OTAPackPackage.d.ts +0 -8
- package/dist/src/protocol/OTA/OtaComplete.d.ts +0 -8
- package/dist/src/protocol/OTA/PreOTA.d.ts +0 -11
- package/dist/src/protocol/OTA/ResetPosition.d.ts +0 -6
- package/dist/src/protocol/OffLineData.d.ts +0 -23
- package/dist/src/protocol/ProtocolBase.d.ts +0 -11
- package/dist/src/protocol/Reset.d.ts +0 -8
- package/dist/src/protocol/SetReset.d.ts +0 -7
- package/dist/src/protocol/WifiConfig.d.ts +0 -6
- package/dist/src/protocol/dataPoint.d.ts +0 -34
- package/dist/src/protocol/tool.d.ts +0 -14
- package/dist/src/randomCode.d.ts +0 -3
- package/dist/src/request.d.ts +0 -1
- package/dist/src/sdk.d.ts +0 -136
- package/dist/src/sentry.d.ts +0 -2
- package/dist/src/services/devices.d.ts +0 -47
- package/dist/src/services/login.d.ts +0 -4
- package/dist/src/services/monitor.d.ts +0 -12
- package/dist/src/services/ota.d.ts +0 -7
- package/dist/src/services/tool.d.ts +0 -4
- package/dist/src/services/uploadP0.d.ts +0 -14
- package/dist/src/sleep.d.ts +0 -2
- package/dist/src/types/index.d.ts +0 -361
- package/dist/src/utils.d.ts +0 -46
- package/dist/src/wechatApi.d.ts +0 -19
- package/dist/src/wifiConfig/ConfigBase.d.ts +0 -27
- package/dist/src/wifiConfig/ap.d.ts +0 -11
- package/dist/src/wifiConfig/ble.d.ts +0 -41
- package/dist/src/wifiConfig/nfc/disposeData.d.ts +0 -2
- package/dist/src/wifiConfig/nfc/formatData.d.ts +0 -3
- package/dist/src/wifiConfig/nfc/formatSSID.d.ts +0 -6
- package/dist/src/wifiConfig/nfc/index.d.ts +0 -22
- package/dist/src/wifiConfig/nfc/isIncludedCH.d.ts +0 -1
- package/dist/src/wifiConfig/nfc/nfcAConnect.d.ts +0 -6
- package/dist/src/wifiConfig/nfc/nfcATransceive.d.ts +0 -5
- package/dist/src/wifiConfig/nfc/ssIDToASC.d.ts +0 -1
- package/dist/src/wifiConfig/nfc/stringToGbk.d.ts +0 -1
- package/dist/src/wifiConfig/nfc/transceive.wx.d.ts +0 -2
- package/dist/src/wifiConfig/nfc/types.d.ts +0 -15
- package/index.ts +0 -8
- package/src/GizLog.ts +0 -51
- package/src/crc/calculators/crc16modbus.ts +0 -36
- package/src/crc/calculators/crc32.ts +0 -47
- package/src/crc/crc16modbus.ts +0 -3
- package/src/crc/crc32.ts +0 -4
- package/src/crc/create_buffer.ts +0 -7
- package/src/crc/define_crc.ts +0 -9
- package/src/crc/index.ts +0 -10
- package/src/crc/package.json +0 -3
- package/src/crc/types.ts +0 -10
- package/src/errorCode.ts +0 -83
- package/src/globalData.ts +0 -9
- package/src/handler/EventListener.ts +0 -44
- package/src/handler/ble.ts +0 -1369
- package/src/handler/lan.ts +0 -389
- package/src/handler/socket.ts +0 -686
- package/src/openApiRequest.ts +0 -50
- package/src/productConfigFileManage.ts +0 -72
- package/src/protocol/Bind.ts +0 -22
- package/src/protocol/DeviceInfo.ts +0 -37
- package/src/protocol/DiscoverUDP.ts +0 -80
- package/src/protocol/GetDeviceStatus.ts +0 -61
- package/src/protocol/Login.ts +0 -43
- package/src/protocol/Ntp.ts +0 -13
- package/src/protocol/OTA/AwaitNoti.ts +0 -14
- package/src/protocol/OTA/CheckCanOTA.ts +0 -23
- package/src/protocol/OTA/OTAPackPackage.ts +0 -37
- package/src/protocol/OTA/OtaComplete.ts +0 -17
- package/src/protocol/OTA/PreOTA.ts +0 -48
- package/src/protocol/OTA/ResetPosition.ts +0 -14
- package/src/protocol/OffLineData.ts +0 -81
- package/src/protocol/ProtocolBase.ts +0 -54
- package/src/protocol/Reset.ts +0 -16
- package/src/protocol/SetReset.ts +0 -13
- package/src/protocol/WifiConfig.ts +0 -42
- package/src/protocol/dataPoint.ts +0 -687
- package/src/protocol/tool.ts +0 -140
- package/src/randomCode.ts +0 -25
- package/src/request.ts +0 -22
- package/src/sdk.ts +0 -1536
- package/src/sentry.ts +0 -55
- package/src/services/devices.ts +0 -313
- package/src/services/login.ts +0 -11
- package/src/services/monitor.ts +0 -117
- package/src/services/ota.ts +0 -17
- package/src/services/tool.ts +0 -10
- package/src/services/uploadP0.ts +0 -57
- package/src/sleep.ts +0 -2
- package/src/types/index.ts +0 -456
- package/src/utils.ts +0 -449
- package/src/wechatApi.ts +0 -211
- package/src/wifiConfig/ConfigBase.ts +0 -235
- package/src/wifiConfig/ap.ts +0 -185
- package/src/wifiConfig/ble.ts +0 -460
- package/src/wifiConfig/nfc/disposeData.ts +0 -63
- package/src/wifiConfig/nfc/formatData.ts +0 -46
- package/src/wifiConfig/nfc/formatSSID.ts +0 -59
- package/src/wifiConfig/nfc/index.ts +0 -134
- package/src/wifiConfig/nfc/isIncludedCH.ts +0 -8
- package/src/wifiConfig/nfc/nfcAConnect.ts +0 -30
- package/src/wifiConfig/nfc/nfcATransceive.ts +0 -29
- package/src/wifiConfig/nfc/ssIDToASC.ts +0 -12
- package/src/wifiConfig/nfc/stringToGbk.ts +0 -51
- package/src/wifiConfig/nfc/transceive.wx.ts +0 -124
- package/src/wifiConfig/nfc/types.ts +0 -18
- package/src/wifiConfig/timeZoneName.json +0 -1
package/src/handler/ble.ts
DELETED
|
@@ -1,1369 +0,0 @@
|
|
|
1
|
-
import GizLog from '../GizLog';
|
|
2
|
-
import Bind from '../protocol/Bind';
|
|
3
|
-
import { padBoradcastData } from '../protocol/dataPoint';
|
|
4
|
-
import GetDeviceStatus from '../protocol/GetDeviceStatus';
|
|
5
|
-
import Login from '../protocol/Login';
|
|
6
|
-
import ProtocolBase from '../protocol/ProtocolBase';
|
|
7
|
-
import { hexStrint2byte } from '../protocol/tool';
|
|
8
|
-
import { reportEvent } from '../services/monitor';
|
|
9
|
-
import sleep from '../sleep';
|
|
10
|
-
import {MD5} from 'crypto-js';
|
|
11
|
-
import {crc32} from '../crc'
|
|
12
|
-
import {
|
|
13
|
-
ab2hex,
|
|
14
|
-
ab2numbers,
|
|
15
|
-
arrayBufferToWordArray,
|
|
16
|
-
BTDevice2GDevice,
|
|
17
|
-
convertToServiceUuids,
|
|
18
|
-
numberArray2Uint8Array,
|
|
19
|
-
parseBroadcastData,
|
|
20
|
-
unionBy,
|
|
21
|
-
wrapErrorInfo
|
|
22
|
-
} from '../utils';
|
|
23
|
-
import { downloadFile, retryConnect, unpackWriteBLECharacteristicValue } from '../wechatApi';
|
|
24
|
-
import EventListener from './EventListener';
|
|
25
|
-
import { checkUpdate } from '../services/ota';
|
|
26
|
-
import errorCode from '../errorCode';
|
|
27
|
-
import DeviceInfo from '../protocol/DeviceInfo';
|
|
28
|
-
import CheckCanOTA from '../protocol/OTA/CheckCanOTA';
|
|
29
|
-
import PreOTA from '../protocol/OTA/PreOTA';
|
|
30
|
-
import ResetPosition from '../protocol/OTA/ResetPosition';
|
|
31
|
-
import OTAPackPackage from '../protocol/OTA/OTAPackPackage';
|
|
32
|
-
import AwaitNoti from '../protocol/OTA/AwaitNoti';
|
|
33
|
-
import OtaComplete from '../protocol/OTA/OtaComplete';
|
|
34
|
-
import { BleConnectDevice, IDevice, IOTAType, IResult, TBleHandleEvent, BleConstructorParams, GizwitsWxBlueToothDevice, IStartScanResult, TBleDataListener, IDeviceReq, StartUpgradeParams, CheckOTAParams } from '../types';
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
function createUUID32() {
|
|
38
|
-
let dt = new Date().getTime();
|
|
39
|
-
let uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
|
|
40
|
-
/[xy]/g,
|
|
41
|
-
function (c) {
|
|
42
|
-
let r = (dt + Math.random() * 16) % 16 | 0;
|
|
43
|
-
dt = Math.floor(dt / 16);
|
|
44
|
-
return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
|
|
45
|
-
}
|
|
46
|
-
);
|
|
47
|
-
return uuid;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function reportConnectingEvent(eventType: "BleDeviceConnectEnd" | "BleDeviceConnectBind" | "BleDeviceConnectLogin") {
|
|
51
|
-
return function (
|
|
52
|
-
target: any,
|
|
53
|
-
propertyName: string,
|
|
54
|
-
descriptor: TypedPropertyDescriptor<any>
|
|
55
|
-
|
|
56
|
-
) {
|
|
57
|
-
const { value: method } = descriptor;
|
|
58
|
-
descriptor.value = function (...args: any[]) {
|
|
59
|
-
const ret = method!.apply(this, args);
|
|
60
|
-
return ret.then(({ _failureType_, ...res }) => {
|
|
61
|
-
reportEvent({
|
|
62
|
-
device: args[0] as IDevice,
|
|
63
|
-
eventID: (this as unknown as any).__event_id__,
|
|
64
|
-
eventType,
|
|
65
|
-
success: res.success,
|
|
66
|
-
});
|
|
67
|
-
return {
|
|
68
|
-
...res,
|
|
69
|
-
_failureType_: eventType,
|
|
70
|
-
};
|
|
71
|
-
})
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function reportConnectEvent(
|
|
77
|
-
target: any,
|
|
78
|
-
propertyName: string,
|
|
79
|
-
descriptor: TypedPropertyDescriptor<any>
|
|
80
|
-
) {
|
|
81
|
-
const { value: method } = descriptor;
|
|
82
|
-
descriptor.value = function (...args: any[]) {
|
|
83
|
-
const that = Object.create(this);
|
|
84
|
-
that.__event_id__ = createUUID32();
|
|
85
|
-
reportEvent({
|
|
86
|
-
device: args[0] as IDevice,
|
|
87
|
-
eventID: that.__event_id__,
|
|
88
|
-
eventType: 'BleDeviceConnectStart',
|
|
89
|
-
success: true,
|
|
90
|
-
});
|
|
91
|
-
const ret = method!.apply(that, args);
|
|
92
|
-
return ret.then(({ _failureType_, ...res }) => {
|
|
93
|
-
reportEvent({
|
|
94
|
-
device: args[0] as IDevice,
|
|
95
|
-
eventID: that.__event_id__,
|
|
96
|
-
eventType: 'BleDeviceConnectFinish',
|
|
97
|
-
success: res.success,
|
|
98
|
-
...(res.success ? {} : { failureType: _failureType_ })
|
|
99
|
-
});
|
|
100
|
-
return res;
|
|
101
|
-
})
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export class BleHandle extends EventListener<TBleHandleEvent> {
|
|
106
|
-
private serviceUUIDSuffix: string;
|
|
107
|
-
// private characteristicUUIDSuffix: string;
|
|
108
|
-
public connectedList: BleConnectDevice[] = [];
|
|
109
|
-
private connectingDevId: string | null = null;
|
|
110
|
-
public pks: string[] = [];
|
|
111
|
-
private listenDevOfflineTimer: any = null;
|
|
112
|
-
|
|
113
|
-
private methodQueue: any = Promise.resolve(); // 发送指令的队列
|
|
114
|
-
private boradcastTimer: any;
|
|
115
|
-
private boradcastDataCache: any = {}
|
|
116
|
-
private baseServices: string[] = ['ABF8', 'ABF0', 'F0AB', 'F8AB'];
|
|
117
|
-
|
|
118
|
-
private tmpBleData = ''; // 接收分包数据用
|
|
119
|
-
private tmpDataNum = 0; // 剩余字节数
|
|
120
|
-
private disableScanFlag = false;
|
|
121
|
-
|
|
122
|
-
private peripheralServer: WechatMiniprogram.BLEPeripheralServer | null = null;
|
|
123
|
-
|
|
124
|
-
private isActive = true; // 小程序是否活跃
|
|
125
|
-
private isInitSuccess = false;
|
|
126
|
-
private isRunInit = false;
|
|
127
|
-
|
|
128
|
-
private _offlineThreshold = null;
|
|
129
|
-
public set offlineThreshold(value) {
|
|
130
|
-
this._offlineThreshold = value;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
public get offlineThreshold() {
|
|
134
|
-
return this._offlineThreshold || 10000;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
// private globalListenerMap = {};
|
|
139
|
-
public scanList: IDevice[] = [];
|
|
140
|
-
constructor({
|
|
141
|
-
pks = [],
|
|
142
|
-
bleHandleParams = {
|
|
143
|
-
serviceUUIDSuffix: 'abf0',
|
|
144
|
-
characteristicUUIDSuffix: 'abf7',
|
|
145
|
-
},
|
|
146
|
-
offlineThreshold,
|
|
147
|
-
}: BleConstructorParams) {
|
|
148
|
-
super();
|
|
149
|
-
const { serviceUUIDSuffix } = bleHandleParams;
|
|
150
|
-
this.serviceUUIDSuffix = serviceUUIDSuffix;
|
|
151
|
-
this.pks = pks;
|
|
152
|
-
// TODO
|
|
153
|
-
// 不要直接使用 globalListenerMap
|
|
154
|
-
// addDeviceDataListener addDeviceListListener addBleErrorListener
|
|
155
|
-
// 或 addEventListener 参数 deviceData deviceList error
|
|
156
|
-
// this.globalListenerMap = globalListenerMap;
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* 重新初始化蓝牙的时候,把连接中的设备都断开
|
|
160
|
-
*/
|
|
161
|
-
|
|
162
|
-
this.offlineThreshold = offlineThreshold;
|
|
163
|
-
this.listenDevOffline();
|
|
164
|
-
this.listenConnection();
|
|
165
|
-
|
|
166
|
-
wx.onAppShow(this.onAppShow);
|
|
167
|
-
wx.onAppHide(this.onAppHide);
|
|
168
|
-
|
|
169
|
-
wx.onBluetoothAdapterStateChange(this.onBluetoothAdapterStateChange)
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
private onBluetoothAdapterStateChange = (res: WechatMiniprogram.OnBluetoothAdapterStateChangeListenerResult) => {
|
|
173
|
-
GizLog.debug("onBluetoothAdapterStateChange", res, this.isRunInit, this.isInitSuccess)
|
|
174
|
-
if (res.available) {
|
|
175
|
-
if (this.isRunInit && this.isInitSuccess === false) {
|
|
176
|
-
// 重新初始化蓝牙
|
|
177
|
-
this.init();
|
|
178
|
-
}
|
|
179
|
-
} else {
|
|
180
|
-
// 不可用状态
|
|
181
|
-
this.scanList = [];
|
|
182
|
-
this.notifyDevices();
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
private onAppShow = () => {
|
|
187
|
-
// 更新所有蓝牙设备的Ctime
|
|
188
|
-
this.isActive = true;
|
|
189
|
-
this.scanList.map(item => {
|
|
190
|
-
item.ctime = Date.now();
|
|
191
|
-
})
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private onAppHide = () => {
|
|
195
|
-
this.isActive = false;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
private listenDevOffline = () => {
|
|
200
|
-
// 定时检查设备列表,移除超时设备
|
|
201
|
-
this.listenDevOfflineTimer = setInterval(() => {
|
|
202
|
-
// 非激活状态不处理
|
|
203
|
-
if (!this.isActive) return;
|
|
204
|
-
const now = new Date().getTime();
|
|
205
|
-
const newDeviceList = this.scanList.filter((d) => {
|
|
206
|
-
const isConnected = this.connectedList.find(item => d.bleDeviceID == item.deviceId);
|
|
207
|
-
const isConnecting = this.connectingDevId === d.bleDeviceID;
|
|
208
|
-
// 已经连接不删除
|
|
209
|
-
if (isConnected || isConnecting){
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
// 广播类型的设备由于性能问题,双倍的时间才触发离线
|
|
213
|
-
if (d.connectType === "BLE_BROADCAST") {
|
|
214
|
-
return now - d.ctime < this.offlineThreshold * 2;
|
|
215
|
-
}
|
|
216
|
-
return now - d.ctime < this.offlineThreshold;
|
|
217
|
-
});
|
|
218
|
-
if (newDeviceList.length !== this.scanList.length) {
|
|
219
|
-
this.scanList = newDeviceList;
|
|
220
|
-
// TODO 不要直接修改保护的对象
|
|
221
|
-
// this.gizSdk.bleDevices = newDeviceList;
|
|
222
|
-
this.notifyDevices();
|
|
223
|
-
}
|
|
224
|
-
}, 500);
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
private openBluetoothAdapter = (mode: 'central' | 'peripheral' = 'central') => {
|
|
228
|
-
return new Promise((resolve, reject) => {
|
|
229
|
-
wx.openBluetoothAdapter({
|
|
230
|
-
mode,
|
|
231
|
-
success: () => {
|
|
232
|
-
resolve({ success: true });
|
|
233
|
-
wx.onBLECharacteristicValueChange(
|
|
234
|
-
this.handleOnBLECharacteristicValueChange
|
|
235
|
-
);
|
|
236
|
-
},
|
|
237
|
-
fail: (err) => {
|
|
238
|
-
GizLog.error('openBluetoothAdapter fail', new Error(JSON.stringify(err)));
|
|
239
|
-
this.notifyError({ errMsg: err.errMsg, errCode: -1 });
|
|
240
|
-
reject({success: false, err});
|
|
241
|
-
},
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
private handleOnBLECharacteristicValueChange = async (
|
|
247
|
-
curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult
|
|
248
|
-
) => {
|
|
249
|
-
// 不存在数据
|
|
250
|
-
if (!curDevice.value) {
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
const hexString = ab2hex(curDevice.value);
|
|
254
|
-
try {
|
|
255
|
-
const data = new ProtocolBase(hexStrint2byte(hexString));
|
|
256
|
-
// 能解析包头,覆盖数据
|
|
257
|
-
this.tmpBleData = hexString;
|
|
258
|
-
this.tmpDataNum =
|
|
259
|
-
data.len -
|
|
260
|
-
curDevice.value.byteLength +
|
|
261
|
-
4 +
|
|
262
|
-
Math.ceil(data.len.toString(2).length / 8);
|
|
263
|
-
} catch (error) {
|
|
264
|
-
// 无法解析包头
|
|
265
|
-
if (this.tmpBleData) {
|
|
266
|
-
// 有没有接收完的数据
|
|
267
|
-
this.tmpBleData += hexString;
|
|
268
|
-
this.tmpDataNum -= curDevice.value.byteLength;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
if (this.tmpDataNum > 0 || !this.tmpBleData) {
|
|
273
|
-
// 没有接收完数据 或者没有有效数据
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
this.notiDeviceDataToSdk(curDevice, this.tmpBleData);
|
|
278
|
-
|
|
279
|
-
this.tmpBleData = '';
|
|
280
|
-
this.tmpDataNum = 0;
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
private notiDeviceDataToSdk = (curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult, data: string) => {
|
|
284
|
-
if (this.listenerMap['GizBleDeviceData']) {
|
|
285
|
-
this.listenerMap['GizBleDeviceData'].map((item) => {
|
|
286
|
-
item(curDevice, data);
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
private notifyError = (err: { errCode: number; errMsg: string }) => {
|
|
292
|
-
// TODO 具体是哪台设备的错误信息
|
|
293
|
-
// this.globalListenerMap['onBleHandleError']?.map((notifyFn) => {
|
|
294
|
-
// notifyFn(err);
|
|
295
|
-
// });
|
|
296
|
-
if (this.listenerMap['GizBleError']) {
|
|
297
|
-
this.listenerMap['GizBleError'].map((item) => {
|
|
298
|
-
item(err);
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
|
|
303
|
-
private notifyDevices = () => {
|
|
304
|
-
if (this.listenerMap['GizBleDeviceList']) {
|
|
305
|
-
this.listenerMap['GizBleDeviceList'].map((item) => {
|
|
306
|
-
item(this.scanList);
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
private listenConnection = () => {
|
|
312
|
-
/**
|
|
313
|
-
* 之前的写法会导致每订阅一个设备都会创建一次监听
|
|
314
|
-
* 监听也不会销毁
|
|
315
|
-
* 但是这里其实sdk来做重连也有一定的歧义
|
|
316
|
-
* 比如说设备被重置之后,假设有逻辑做了解除连接,但是可能会被这里救起来
|
|
317
|
-
* 另一个角度来分析,蓝牙连接上之后,没有那么容易断开
|
|
318
|
-
*/
|
|
319
|
-
wx.onBLEConnectionStateChange((res) => {
|
|
320
|
-
if (res.connected === false) {
|
|
321
|
-
const index = this.connectedList.findIndex(item => item.deviceId === res.deviceId);
|
|
322
|
-
if (index !== -1) {
|
|
323
|
-
this.connectedList.splice(index, 1);
|
|
324
|
-
this.notifyDevices();
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
private init = async () => {
|
|
331
|
-
this.isRunInit = true;
|
|
332
|
-
const { platform } = wx.getSystemInfoSync();
|
|
333
|
-
await this.openBluetoothAdapter();
|
|
334
|
-
if (platform === 'ios') {
|
|
335
|
-
// ios 需要打开两次
|
|
336
|
-
await this.openBluetoothAdapter('peripheral');
|
|
337
|
-
}
|
|
338
|
-
// 初始化广播
|
|
339
|
-
try {
|
|
340
|
-
this.peripheralServer = (await wx.createBLEPeripheralServer()).server;
|
|
341
|
-
GizLog.info('GIZ_SDK: 广播初始化成功', this.peripheralServer)
|
|
342
|
-
} catch (error) {
|
|
343
|
-
GizLog.error('GIZ_SDK: 广播初始化失败', new Error(JSON.stringify(error)))
|
|
344
|
-
}
|
|
345
|
-
const res = await this.getBluetoothAdapterState();
|
|
346
|
-
this.isInitSuccess = true
|
|
347
|
-
|
|
348
|
-
wx.onBluetoothDeviceFound((resData) => {
|
|
349
|
-
this.handleBluetoothDeviceFound(resData);
|
|
350
|
-
});
|
|
351
|
-
return res;
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
public checkPermission = async () => {
|
|
355
|
-
const settings = await wx.getSetting();
|
|
356
|
-
const hasBlePermission = settings.authSetting['scope.bluetooth'];
|
|
357
|
-
|
|
358
|
-
if (!hasBlePermission) {
|
|
359
|
-
try {
|
|
360
|
-
await wx.authorize({ scope: 'scope.bluetooth' });
|
|
361
|
-
return await this.init();
|
|
362
|
-
} catch (err) {
|
|
363
|
-
GizLog.log('授权失败', err);
|
|
364
|
-
// remove me in the future
|
|
365
|
-
this.openBluetoothAdapter();
|
|
366
|
-
this.notifyError({ errMsg: err.errMsg, errCode: -1 });
|
|
367
|
-
return {success: false,}
|
|
368
|
-
}
|
|
369
|
-
} else {
|
|
370
|
-
return await this.init();
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
|
|
374
|
-
private getBluetoothAdapterState = async () => {
|
|
375
|
-
GizLog.log('getBluetoothAdapterState', await wx.getBluetoothAdapterState());
|
|
376
|
-
const { errMsg, available, ...adaptState } = await wx.getBluetoothAdapterState();
|
|
377
|
-
return {
|
|
378
|
-
data: {
|
|
379
|
-
...adaptState,
|
|
380
|
-
},
|
|
381
|
-
err: wrapErrorInfo(errMsg),
|
|
382
|
-
success: available,
|
|
383
|
-
};
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
getDeviceStatus = async (
|
|
387
|
-
deviceId: string,
|
|
388
|
-
productKey: string,
|
|
389
|
-
gwDid?: string,
|
|
390
|
-
attrNames?: string[]
|
|
391
|
-
) => {
|
|
392
|
-
const value = await GetDeviceStatus.pack(productKey, gwDid, attrNames);
|
|
393
|
-
if (value) {
|
|
394
|
-
return await this.write(deviceId, numberArray2Uint8Array(value).buffer);
|
|
395
|
-
}
|
|
396
|
-
return {
|
|
397
|
-
success: false,
|
|
398
|
-
};
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
private updateDevice = (device: IDevice) => {
|
|
402
|
-
// 更新蓝牙设备到scanlist
|
|
403
|
-
const id = device.bleDeviceID;
|
|
404
|
-
const index = this.scanList.findIndex(item => id == item.bleDeviceID);
|
|
405
|
-
if (index !== -1) {
|
|
406
|
-
this.scanList[index] = device;
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
private updateCtime = (device: WechatMiniprogram.BlueToothDevice) => {
|
|
411
|
-
const id = device.deviceId;
|
|
412
|
-
const index = this.scanList.findIndex(item => id == item.bleDeviceID);
|
|
413
|
-
if (index !== -1) {
|
|
414
|
-
this.scanList[index].ctime = Date.now();
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
private checkDeviceIsChange = (a: IDevice, b: IDevice) => {
|
|
419
|
-
//检查设备是否发生变更
|
|
420
|
-
|
|
421
|
-
if (a.bleWorkStatus !== b.bleWorkStatus ||
|
|
422
|
-
a.name !== b.name ||
|
|
423
|
-
a.isBleOnline !== b.isBleOnline ||
|
|
424
|
-
a.isOnline !== b.isOnline
|
|
425
|
-
) {
|
|
426
|
-
return true;
|
|
427
|
-
}
|
|
428
|
-
return false;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
private handleBluetoothDeviceFound = (
|
|
432
|
-
resData: WechatMiniprogram.OnBluetoothDeviceFoundListenerResult,
|
|
433
|
-
) => {
|
|
434
|
-
/**
|
|
435
|
-
* TODO
|
|
436
|
-
* 流程可以优化
|
|
437
|
-
* 这里多次解析了设备的广播内容
|
|
438
|
-
*/
|
|
439
|
-
(unionBy(resData.devices, 'deviceId') as GizwitsWxBlueToothDevice[]).map((device) => {
|
|
440
|
-
const { advertisData, deviceId, name, localName } = device;
|
|
441
|
-
const advertisDataStr = ab2hex(advertisData);
|
|
442
|
-
|
|
443
|
-
// 从服务uuid里面扫描广播类的设备
|
|
444
|
-
let broadcastCmdData: any = null;
|
|
445
|
-
try {
|
|
446
|
-
broadcastCmdData = parseBroadcastData(ab2numbers(advertisData));
|
|
447
|
-
} catch (error) {
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
const targetBleDev = this.scanList.find(
|
|
451
|
-
(d) => d.bleDeviceID === deviceId
|
|
452
|
-
);
|
|
453
|
-
// 先检查是否是广播设备的业务指令,如果是就更新数据,不往下判断
|
|
454
|
-
if (broadcastCmdData && broadcastCmdData.cmd === 0x94) {
|
|
455
|
-
// 业务指令
|
|
456
|
-
const advertisDataString = ab2hex(advertisData);
|
|
457
|
-
if (this.boradcastDataCache[deviceId] && this.boradcastDataCache[deviceId] === advertisDataString) {
|
|
458
|
-
// 广播包数据相同,已经处理过了,不再处理
|
|
459
|
-
return false;
|
|
460
|
-
}
|
|
461
|
-
if (!targetBleDev) {
|
|
462
|
-
// 目标设备不存在
|
|
463
|
-
return false;
|
|
464
|
-
}
|
|
465
|
-
this.boradcastDataCache[deviceId] = ab2hex(advertisData);
|
|
466
|
-
const fullData = padBoradcastData(broadcastCmdData.data.map(item => parseInt(item, 16)))
|
|
467
|
-
|
|
468
|
-
this.updateCtime(device)
|
|
469
|
-
this.notiDeviceDataToSdk(device as any, fullData);
|
|
470
|
-
return;
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
// 是广播设备,并且是发现包的命令
|
|
474
|
-
if (broadcastCmdData && broadcastCmdData.cmd === 0x19) {
|
|
475
|
-
const target = this.pks.find(item => item === broadcastCmdData.data.join(""));
|
|
476
|
-
|
|
477
|
-
if (target) {
|
|
478
|
-
// 更新通讯方式
|
|
479
|
-
device.communicationType = 'BLE_BROADCAST';
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
// 开始解析普通设备
|
|
484
|
-
if (targetBleDev) {
|
|
485
|
-
// 存在设备,更新ctime 并更新属性
|
|
486
|
-
const newDevice = BTDevice2GDevice(device, this.pks);
|
|
487
|
-
|
|
488
|
-
if (newDevice.productKey && newDevice.mac) {
|
|
489
|
-
// 广播设备没有 bleWorkStatus
|
|
490
|
-
const isChange = this.checkDeviceIsChange(newDevice, targetBleDev);
|
|
491
|
-
// 更新设备
|
|
492
|
-
newDevice.ctime = Date.now();
|
|
493
|
-
this.updateDevice(newDevice);
|
|
494
|
-
// GizLog.debug("GIZ_SDK: same device", newDevice, isChange);
|
|
495
|
-
if (isChange) {
|
|
496
|
-
this.notifyDevices();
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
} else {
|
|
500
|
-
// 新增设备
|
|
501
|
-
|
|
502
|
-
let isValidDevice = this.pks.some((pk) =>
|
|
503
|
-
advertisDataStr.includes(pk)
|
|
504
|
-
);
|
|
505
|
-
if (isValidDevice) {
|
|
506
|
-
// device.communicationType = 'BLE';
|
|
507
|
-
const newDevice = BTDevice2GDevice(device, this.pks);
|
|
508
|
-
if (newDevice.productKey && newDevice.mac) {
|
|
509
|
-
this.scanList.push(newDevice);
|
|
510
|
-
this.notifyDevices();
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
});
|
|
515
|
-
};
|
|
516
|
-
|
|
517
|
-
public startScan = async (
|
|
518
|
-
delay: number = 5000,
|
|
519
|
-
services?: string[]
|
|
520
|
-
) => {
|
|
521
|
-
if (this.disableScanFlag) {
|
|
522
|
-
return {
|
|
523
|
-
scanList: []
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
return new Promise<IStartScanResult>(async (res) => {
|
|
527
|
-
GizLog.debug("GIZ_SDK: start scan", delay);
|
|
528
|
-
await wx.startBluetoothDevicesDiscovery({
|
|
529
|
-
// 如果有传的话,就按传入的方式来搜索
|
|
530
|
-
services: services || this.baseServices,
|
|
531
|
-
powerLevel: 'high',
|
|
532
|
-
allowDuplicatesKey: true,
|
|
533
|
-
interval: 200,
|
|
534
|
-
});
|
|
535
|
-
|
|
536
|
-
if (delay > 0) {
|
|
537
|
-
setTimeout(() => {
|
|
538
|
-
this.stopScan();
|
|
539
|
-
res({
|
|
540
|
-
scanList: this.scanList,
|
|
541
|
-
});
|
|
542
|
-
}, delay);
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
};
|
|
546
|
-
|
|
547
|
-
public stopScan = async () => {
|
|
548
|
-
try {
|
|
549
|
-
return await wx.stopBluetoothDevicesDiscovery();
|
|
550
|
-
} catch (error) {
|
|
551
|
-
return error;
|
|
552
|
-
}
|
|
553
|
-
};
|
|
554
|
-
|
|
555
|
-
public disableScan = async () => {
|
|
556
|
-
this.disableScanFlag = true;
|
|
557
|
-
const data = await wx.stopBluetoothDevicesDiscovery();
|
|
558
|
-
// 等系统通知停止扫描
|
|
559
|
-
await sleep(200)
|
|
560
|
-
return data;
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
public enableScan = () => {
|
|
564
|
-
this.disableScanFlag = false;
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
@reportConnectingEvent('BleDeviceConnectEnd')
|
|
568
|
-
private async connectDeviceBle(device: IDevice) {
|
|
569
|
-
try {
|
|
570
|
-
// 连接前 先停止搜索
|
|
571
|
-
await this.disableScan();
|
|
572
|
-
const connectRes = await retryConnect(device.bleDeviceID)
|
|
573
|
-
this.enableScan();
|
|
574
|
-
GizLog.debug("connect res", connectRes)
|
|
575
|
-
|
|
576
|
-
// if (connectRes && connectRes.errCode == 0 && connectRes.errMsg === 'createBLEConnection:ok') {
|
|
577
|
-
// this.listenConnection(deviceId);
|
|
578
|
-
// }
|
|
579
|
-
} catch (error) {
|
|
580
|
-
// 已经连接,继续往下走登录流程
|
|
581
|
-
GizLog.error('GIZ_SDK: createBLEConnection error', new Error(JSON.stringify(error)))
|
|
582
|
-
if (error?.errMsg !== 'createBLEConnection:fail:already connect') {
|
|
583
|
-
this.notifyError(error);
|
|
584
|
-
return { success: false, message: error?.errMsg };
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
const getServicesRes = (await wx
|
|
590
|
-
.getBLEDeviceServices({ deviceId: device.bleDeviceID })
|
|
591
|
-
.catch((err) => this.notifyError(err))) as any;
|
|
592
|
-
|
|
593
|
-
const serviceId = getServicesRes?.services.find(
|
|
594
|
-
(service) =>
|
|
595
|
-
service.isPrimary &&
|
|
596
|
-
service.uuid.toLowerCase().includes(this.serviceUUIDSuffix)
|
|
597
|
-
)?.uuid;
|
|
598
|
-
if (!serviceId) {
|
|
599
|
-
return {
|
|
600
|
-
message: 'can not find valid serviceId',
|
|
601
|
-
success: false,
|
|
602
|
-
};
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
const getCharRes = (await wx
|
|
607
|
-
.getBLEDeviceCharacteristics({
|
|
608
|
-
deviceId: device.bleDeviceID,
|
|
609
|
-
serviceId,
|
|
610
|
-
})
|
|
611
|
-
.catch((err) => this.notifyError(err))) as any;
|
|
612
|
-
|
|
613
|
-
const characteristicId = getCharRes?.characteristics[0].uuid!;
|
|
614
|
-
|
|
615
|
-
return {
|
|
616
|
-
success: true,
|
|
617
|
-
data: {
|
|
618
|
-
serviceId,
|
|
619
|
-
characteristicId,
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
@reportConnectingEvent('BleDeviceConnectBind')
|
|
625
|
-
private async connectDeviceBindRequest(device: IDevice) {
|
|
626
|
-
this.write(device.bleDeviceID, numberArray2Uint8Array(Bind.pack()).buffer);
|
|
627
|
-
const data = await this.handleBindReq(device.bleDeviceID);
|
|
628
|
-
GizLog.log('handleBindReq', data);
|
|
629
|
-
if (!data.success) {
|
|
630
|
-
// 绑定失败,异常退出断开蓝牙
|
|
631
|
-
await wx.closeBLEConnection({
|
|
632
|
-
deviceId: device.bleDeviceID,
|
|
633
|
-
});
|
|
634
|
-
}
|
|
635
|
-
return data;
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
@reportConnectingEvent('BleDeviceConnectLogin')
|
|
639
|
-
private async connectDeviceLoginRequest(device: IDevice, passcode: string) {
|
|
640
|
-
this.write(
|
|
641
|
-
device.bleDeviceID,
|
|
642
|
-
numberArray2Uint8Array(Login.pack({ passcode })).buffer
|
|
643
|
-
);
|
|
644
|
-
const loginData = (await this.handleLoginReq(device.bleDeviceID).catch((err) =>
|
|
645
|
-
this.notifyError(err)
|
|
646
|
-
)) as any;
|
|
647
|
-
return {
|
|
648
|
-
success: !!loginData?.success,
|
|
649
|
-
message: 'ok',
|
|
650
|
-
};
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
@reportConnectEvent
|
|
654
|
-
private async _connectDevice(device: IDevice) {
|
|
655
|
-
const connectRet = await this.connectDeviceBle(device);
|
|
656
|
-
if (!connectRet.success) {
|
|
657
|
-
return connectRet;
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
const { serviceId, characteristicId } = connectRet.data;
|
|
661
|
-
|
|
662
|
-
// 登录成功 把设备插入 connectedList
|
|
663
|
-
const isNewDevice = this.connectedList.every(
|
|
664
|
-
(d) => d.deviceId !== device.bleDeviceID
|
|
665
|
-
);
|
|
666
|
-
isNewDevice &&
|
|
667
|
-
this.connectedList.push({ deviceId: device.bleDeviceID, serviceId, characteristicId });
|
|
668
|
-
|
|
669
|
-
// 连接成功以后默认监听设备
|
|
670
|
-
await wx
|
|
671
|
-
.notifyBLECharacteristicValueChange({
|
|
672
|
-
deviceId: device.bleDeviceID,
|
|
673
|
-
serviceId,
|
|
674
|
-
characteristicId,
|
|
675
|
-
state: true,
|
|
676
|
-
type: 'notification',
|
|
677
|
-
})
|
|
678
|
-
.catch((err) => this.notifyError(err));
|
|
679
|
-
|
|
680
|
-
const bindRet = await this.connectDeviceBindRequest(device);
|
|
681
|
-
|
|
682
|
-
if (!bindRet.success) {
|
|
683
|
-
return bindRet;
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
const loginRet = await this.connectDeviceLoginRequest(device, bindRet.data);
|
|
687
|
-
return loginRet;
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
public connectDevice = async (device: IDevice) => {
|
|
692
|
-
// 先锁定这个设备 防止被剔除
|
|
693
|
-
try {
|
|
694
|
-
this.connectingDevId = device.bleDeviceID;
|
|
695
|
-
return await this._connectDevice(device);
|
|
696
|
-
} finally {
|
|
697
|
-
this.connectingDevId = null;
|
|
698
|
-
}
|
|
699
|
-
};
|
|
700
|
-
|
|
701
|
-
/**
|
|
702
|
-
*
|
|
703
|
-
* @param deviceId
|
|
704
|
-
* @param filter
|
|
705
|
-
* @returns
|
|
706
|
-
* 监听指定设备的原始数据
|
|
707
|
-
* 3s超时,用来处理和设备一来一回的交互
|
|
708
|
-
*/
|
|
709
|
-
private handleWatchBleData = async <T>(deviceId: string, filter: TBleDataListener<T>, timeout = 3000): Promise<IResult<T>> => {
|
|
710
|
-
return new Promise((res) => {
|
|
711
|
-
let watchWapper = (
|
|
712
|
-
_curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult,
|
|
713
|
-
hexString: string
|
|
714
|
-
) => {
|
|
715
|
-
// 不是这里关心的数据
|
|
716
|
-
if (deviceId !== _curDevice.deviceId) {
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
719
|
-
const resData = filter(_curDevice, hexString);
|
|
720
|
-
if (resData) {
|
|
721
|
-
// 结束监听
|
|
722
|
-
res({
|
|
723
|
-
success: true,
|
|
724
|
-
data: resData
|
|
725
|
-
});
|
|
726
|
-
this.removeEventListener('GizBleDeviceData', watchWapper);
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
this.addEventListener('GizBleDeviceData', watchWapper);
|
|
730
|
-
|
|
731
|
-
// 设置超时时间
|
|
732
|
-
setTimeout(() => {
|
|
733
|
-
if (watchWapper) {
|
|
734
|
-
this.removeEventListener('GizBleDeviceData', watchWapper);
|
|
735
|
-
res({
|
|
736
|
-
success: false,
|
|
737
|
-
err: errorCode.GIZ_SDK_DEVICE_NO_RESPONSE
|
|
738
|
-
});
|
|
739
|
-
}
|
|
740
|
-
}, timeout)
|
|
741
|
-
})
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
private handleBindReq = (deviceId: string) => {
|
|
745
|
-
// 处理设备回复绑定消息
|
|
746
|
-
return new Promise<IDeviceReq<string>>((res) => {
|
|
747
|
-
const timer = setTimeout(() => {
|
|
748
|
-
GizLog.debug('绑定协议执行超时');
|
|
749
|
-
res({
|
|
750
|
-
success: false,
|
|
751
|
-
data: ''
|
|
752
|
-
})
|
|
753
|
-
}, 5* 1000)
|
|
754
|
-
const onListener = (
|
|
755
|
-
_curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult,
|
|
756
|
-
hexString: string
|
|
757
|
-
) => {
|
|
758
|
-
if (_curDevice.deviceId !== deviceId) {
|
|
759
|
-
return;
|
|
760
|
-
}
|
|
761
|
-
timer && clearTimeout(timer)
|
|
762
|
-
GizLog.log('监听蓝牙特征值发生变化');
|
|
763
|
-
const bindData = new Bind(hexStrint2byte(hexString));
|
|
764
|
-
this.removeEventListener('GizBleDeviceData', onListener);
|
|
765
|
-
if (bindData.passcode) {
|
|
766
|
-
// 存在passcode
|
|
767
|
-
res({
|
|
768
|
-
success: true,
|
|
769
|
-
data: bindData.passcode,
|
|
770
|
-
});
|
|
771
|
-
} else {
|
|
772
|
-
res({
|
|
773
|
-
success: false,
|
|
774
|
-
data: '',
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
|
-
|
|
779
|
-
this.watchBleDevice(deviceId, onListener);
|
|
780
|
-
});
|
|
781
|
-
};
|
|
782
|
-
|
|
783
|
-
private handleLoginReq = (deviceId) => {
|
|
784
|
-
return new Promise<IDeviceReq<String>>((res) => {
|
|
785
|
-
const timer = setTimeout(() => {
|
|
786
|
-
GizLog.debug('登录协议执行超时');
|
|
787
|
-
res({
|
|
788
|
-
success: false,
|
|
789
|
-
data: ''
|
|
790
|
-
})
|
|
791
|
-
}, 5* 1000)
|
|
792
|
-
const onListener = (
|
|
793
|
-
_curDevice: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult,
|
|
794
|
-
hexString: string
|
|
795
|
-
) => {
|
|
796
|
-
//TODO 销毁监听
|
|
797
|
-
if (_curDevice.deviceId !== deviceId) {
|
|
798
|
-
return;
|
|
799
|
-
}
|
|
800
|
-
timer && clearTimeout(timer)
|
|
801
|
-
GizLog.log('设备回复LoginReq', hexString);
|
|
802
|
-
const loginData = new Login(hexStrint2byte(hexString));
|
|
803
|
-
this.removeEventListener('GizBleDeviceData', onListener);
|
|
804
|
-
if (loginData.result === true) {
|
|
805
|
-
res({
|
|
806
|
-
success: true,
|
|
807
|
-
data: loginData.did,
|
|
808
|
-
});
|
|
809
|
-
} else {
|
|
810
|
-
res({
|
|
811
|
-
success: false,
|
|
812
|
-
data: '',
|
|
813
|
-
});
|
|
814
|
-
}
|
|
815
|
-
};
|
|
816
|
-
this.watchBleDevice(deviceId, onListener);
|
|
817
|
-
});
|
|
818
|
-
};
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
public startUpgrade =async ({device, type, onEvent, onProgress, productKey}: StartUpgradeParams) => {
|
|
822
|
-
// 上报开始
|
|
823
|
-
onEvent({
|
|
824
|
-
code: "GizOTAEventPretreatment"
|
|
825
|
-
})
|
|
826
|
-
const res = await this.checkUpdate({device, type});
|
|
827
|
-
GizLog.debug("检查设备是否有新固件", res)
|
|
828
|
-
const deviceId = device.bleDeviceID;
|
|
829
|
-
|
|
830
|
-
if (res.success) {
|
|
831
|
-
const url = (res.data as any).download_url;
|
|
832
|
-
const softVer = (res.data as any).soft_ver
|
|
833
|
-
// 开始下载
|
|
834
|
-
onEvent({
|
|
835
|
-
code: "GizOTAEventDownload"
|
|
836
|
-
})
|
|
837
|
-
let fileUrl = '';
|
|
838
|
-
try {
|
|
839
|
-
const downloadRes = await downloadFile({
|
|
840
|
-
url: url,
|
|
841
|
-
})
|
|
842
|
-
fileUrl = (downloadRes as any).tempFilePath
|
|
843
|
-
} catch (error) {
|
|
844
|
-
GizLog.error("固件下载失败", error)
|
|
845
|
-
|
|
846
|
-
return {
|
|
847
|
-
success: false,
|
|
848
|
-
err: errorCode.GIZ_SDK_OTA_FIRMWARE_DOWNLOAD_FAILED
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
GizLog.debug("下载固件成功", fileUrl)
|
|
852
|
-
|
|
853
|
-
// 开始传输
|
|
854
|
-
onEvent({
|
|
855
|
-
code: "GizOTAEventTransmit"
|
|
856
|
-
})
|
|
857
|
-
|
|
858
|
-
/**
|
|
859
|
-
* 询问设备是否可以升级
|
|
860
|
-
*/
|
|
861
|
-
const filterCanUpdateFunc = (_, hexString) => {
|
|
862
|
-
const bleBytesData = hexStrint2byte(hexString)
|
|
863
|
-
const parseData = new ProtocolBase(bleBytesData);
|
|
864
|
-
switch (parseData.cmd) {
|
|
865
|
-
case '0042': {
|
|
866
|
-
const data = new CheckCanOTA(bleBytesData)
|
|
867
|
-
return data
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
return null;
|
|
871
|
-
}
|
|
872
|
-
/**
|
|
873
|
-
* 监听预升级设备回复
|
|
874
|
-
*/
|
|
875
|
-
const filterPreUpdateFunc = (_, hexString) => {
|
|
876
|
-
const bleBytesData = hexStrint2byte(hexString)
|
|
877
|
-
const parseData = new ProtocolBase(bleBytesData);
|
|
878
|
-
switch (parseData.cmd) {
|
|
879
|
-
case '0044': {
|
|
880
|
-
const data = new PreOTA(bleBytesData)
|
|
881
|
-
return data
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
return null;
|
|
885
|
-
}
|
|
886
|
-
/**
|
|
887
|
-
* 等待设备通知可以继续ota
|
|
888
|
-
*/
|
|
889
|
-
const filterAwaitOtaContinueFunc = (_, hexString) => {
|
|
890
|
-
const bleBytesData = hexStrint2byte(hexString)
|
|
891
|
-
const parseData = new ProtocolBase(bleBytesData);
|
|
892
|
-
switch (parseData.cmd) {
|
|
893
|
-
case '0052': {
|
|
894
|
-
const data = new AwaitNoti(bleBytesData)
|
|
895
|
-
return data
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
return null;
|
|
899
|
-
}
|
|
900
|
-
GizLog.debug("查询设备是否可以OTA")
|
|
901
|
-
|
|
902
|
-
const canUpdataData = CheckCanOTA.pack(type);
|
|
903
|
-
await this.write(deviceId, numberArray2Uint8Array(canUpdataData).buffer);
|
|
904
|
-
const canUpdateRes = await this.handleWatchBleData<CheckCanOTA>(deviceId, filterCanUpdateFunc);
|
|
905
|
-
|
|
906
|
-
GizLog.debug("设备回复是否可以OTA", canUpdateRes)
|
|
907
|
-
|
|
908
|
-
if (canUpdateRes.success && canUpdateRes.data.type !== type) {
|
|
909
|
-
// 类型不匹配
|
|
910
|
-
GizLog.error(null, new Error("OTA类型不匹配"))
|
|
911
|
-
|
|
912
|
-
return {
|
|
913
|
-
success: false,
|
|
914
|
-
err: errorCode.GIZ_SDK_OTA_FIRMWARE_TYPE_NOT_MATCH
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
// 发送预更新指令
|
|
919
|
-
if (canUpdateRes.success && canUpdateRes.data.state ===0) {
|
|
920
|
-
// 允许更新 发送预升级请求
|
|
921
|
-
// 获取文件 计算md5 和 crc
|
|
922
|
-
const fs = wx.getFileSystemManager()
|
|
923
|
-
let fileData: any = []
|
|
924
|
-
try {
|
|
925
|
-
fileData = await fs.readFileSync(fileUrl, 'hex', 0)
|
|
926
|
-
} catch (error) {
|
|
927
|
-
GizLog.error("读取固件失败", error)
|
|
928
|
-
return {
|
|
929
|
-
success: false,
|
|
930
|
-
err: errorCode.GIZ_SDK_OTA_FIRMWARE_DOWNLOAD_FAILED
|
|
931
|
-
}
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
const byteArray = fileData.match(/.{1,2}/g).map(function (byte) {
|
|
935
|
-
return parseInt(byte, 16);
|
|
936
|
-
});
|
|
937
|
-
const wordArray = arrayBufferToWordArray(byteArray)
|
|
938
|
-
const md5 = MD5(wordArray).toString()
|
|
939
|
-
|
|
940
|
-
const crc32Data = crc32(byteArray);
|
|
941
|
-
|
|
942
|
-
const preOtaData = PreOTA.pack(productKey, softVer, md5, byteArray.length, crc32Data)
|
|
943
|
-
|
|
944
|
-
await this.write(deviceId, numberArray2Uint8Array(preOtaData).buffer);
|
|
945
|
-
const preOtaRes = await this.handleWatchBleData<PreOTA>(deviceId, filterPreUpdateFunc, 5 * 1000);
|
|
946
|
-
|
|
947
|
-
GizLog.debug("设备回复ota 预请求", preOtaRes)
|
|
948
|
-
|
|
949
|
-
if (preOtaRes.success && preOtaRes.data.state === 0) {
|
|
950
|
-
// 开始位置
|
|
951
|
-
let startPoint = 0;
|
|
952
|
-
|
|
953
|
-
if (preOtaRes.data.processedLen > 0) {
|
|
954
|
-
// 校对已接收的crc32 和当前文件相同部分的crc32 是否相同
|
|
955
|
-
const currentProcessedCrc32 = crc32(byteArray.slice(0, preOtaRes.data.processedLen)).toString(16);
|
|
956
|
-
if (currentProcessedCrc32 === preOtaRes.data.processedCrc32) {
|
|
957
|
-
//如果相同
|
|
958
|
-
startPoint = preOtaRes.data.processedLen;
|
|
959
|
-
GizLog.debug(`断点续传,从 ${startPoint} 开始`)
|
|
960
|
-
|
|
961
|
-
} else {
|
|
962
|
-
GizLog.debug(`从位置0开始OTA`)
|
|
963
|
-
|
|
964
|
-
// 如果不相同,包可能发生了问题,通知设备从0 开始
|
|
965
|
-
await this.write(deviceId, numberArray2Uint8Array(ResetPosition.pack(0)).buffer);
|
|
966
|
-
await sleep(100)
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
/**
|
|
971
|
-
* 开始发送数据包
|
|
972
|
-
*/
|
|
973
|
-
// 总固件长度
|
|
974
|
-
const fileLen = byteArray.length;
|
|
975
|
-
// 最大包长度
|
|
976
|
-
const maxPackageLen = preOtaRes.data.packageMaxLen;
|
|
977
|
-
// 当前包号
|
|
978
|
-
let packageIndex = Math.ceil(startPoint / maxPackageLen);
|
|
979
|
-
|
|
980
|
-
let repeatNum = 0; // 当前重试次数
|
|
981
|
-
|
|
982
|
-
while (startPoint < fileLen) {
|
|
983
|
-
|
|
984
|
-
const packageData = byteArray.slice(startPoint, startPoint + maxPackageLen);
|
|
985
|
-
|
|
986
|
-
// 组包
|
|
987
|
-
const otaPackage = OTAPackPackage.pack(packageData, packageIndex)
|
|
988
|
-
// 发送
|
|
989
|
-
await this.write(deviceId, numberArray2Uint8Array(otaPackage).buffer);
|
|
990
|
-
const filterOtaProgressFunc = (_, hexString) => {
|
|
991
|
-
const bleBytesData = hexStrint2byte(hexString)
|
|
992
|
-
const parseData = new ProtocolBase(bleBytesData);
|
|
993
|
-
switch (parseData.cmd) {
|
|
994
|
-
case '0048': {
|
|
995
|
-
const data = new OTAPackPackage(bleBytesData)
|
|
996
|
-
if (data.packageNum === packageIndex) {
|
|
997
|
-
// 返回包号相同的数据
|
|
998
|
-
return data;
|
|
999
|
-
}
|
|
1000
|
-
return null
|
|
1001
|
-
}
|
|
1002
|
-
}
|
|
1003
|
-
return null;
|
|
1004
|
-
}
|
|
1005
|
-
// 监听回复
|
|
1006
|
-
const otaRes = await this.handleWatchBleData<OTAPackPackage>(deviceId, filterOtaProgressFunc, 5 * 1000);
|
|
1007
|
-
let hasError = false;
|
|
1008
|
-
if (otaRes.success === false) hasError = true
|
|
1009
|
-
|
|
1010
|
-
if (otaRes.success && otaRes.data.state !== 0 && otaRes.data.state !== 4) hasError= true
|
|
1011
|
-
if (hasError) {
|
|
1012
|
-
repeatNum += 1;
|
|
1013
|
-
GizLog.debug(`发送OTA包失败,重试次数 ${repeatNum}`, otaRes)
|
|
1014
|
-
|
|
1015
|
-
// 错误计数加1 重试
|
|
1016
|
-
if (repeatNum > 3) {
|
|
1017
|
-
// ota 失败,断开连接,返回错误
|
|
1018
|
-
GizLog.debug(`发送OTA包失败次数超过限制`)
|
|
1019
|
-
return {
|
|
1020
|
-
success: false,
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
continue;
|
|
1024
|
-
}
|
|
1025
|
-
|
|
1026
|
-
GizLog.debug("设备接收ota子包成功", otaRes)
|
|
1027
|
-
|
|
1028
|
-
// 发送成功,重新计数
|
|
1029
|
-
repeatNum = 0;
|
|
1030
|
-
|
|
1031
|
-
// 回复成功移动指针
|
|
1032
|
-
startPoint += maxPackageLen;
|
|
1033
|
-
packageIndex += 1;
|
|
1034
|
-
|
|
1035
|
-
let currentProgress = parseInt(`${(packageIndex * maxPackageLen / fileLen) * 100}`)
|
|
1036
|
-
if (currentProgress > 100) currentProgress = 100
|
|
1037
|
-
onProgress({
|
|
1038
|
-
firmwareSize: fileLen,
|
|
1039
|
-
packageMaxLen: maxPackageLen,
|
|
1040
|
-
currentNumber: packageIndex,
|
|
1041
|
-
currentProgress
|
|
1042
|
-
})
|
|
1043
|
-
|
|
1044
|
-
// 等100ms
|
|
1045
|
-
await sleep(100)
|
|
1046
|
-
|
|
1047
|
-
// 这里还需要做个判断,如果进入等待发送状态,需要等设备通知之后再继续发送
|
|
1048
|
-
if (otaRes.data.state === 4) {
|
|
1049
|
-
GizLog.debug(`设备请求等待OTA`)
|
|
1050
|
-
|
|
1051
|
-
const awaitRes = await this.handleWatchBleData<AwaitNoti>(deviceId, filterAwaitOtaContinueFunc, 60 * 1000);
|
|
1052
|
-
GizLog.debug(`设备回复等待结果`, awaitRes)
|
|
1053
|
-
|
|
1054
|
-
if (awaitRes.data.state === 1) {
|
|
1055
|
-
return {
|
|
1056
|
-
success: false,
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
// 发送完所有数据包, 等待监听
|
|
1063
|
-
GizLog.debug("发送完所有数据包,等待设备回复")
|
|
1064
|
-
|
|
1065
|
-
const filterOtaResFunc = (_, hexString) => {
|
|
1066
|
-
const bleBytesData = hexStrint2byte(hexString)
|
|
1067
|
-
const parseData = new ProtocolBase(bleBytesData);
|
|
1068
|
-
switch (parseData.cmd) {
|
|
1069
|
-
case '0050': {
|
|
1070
|
-
const data = new OtaComplete(bleBytesData)
|
|
1071
|
-
return data
|
|
1072
|
-
}
|
|
1073
|
-
}
|
|
1074
|
-
return null;
|
|
1075
|
-
}
|
|
1076
|
-
const awaitRes = await this.handleWatchBleData<OtaComplete>(deviceId, filterOtaResFunc, 10 * 1000);
|
|
1077
|
-
|
|
1078
|
-
if (awaitRes.success && awaitRes.data.state === 0) {
|
|
1079
|
-
GizLog.debug("设备回复收到固件,准备重启设备")
|
|
1080
|
-
onEvent({
|
|
1081
|
-
code: "GizOTAEventReboot"
|
|
1082
|
-
})
|
|
1083
|
-
// 等待5s后去尝试连接设备
|
|
1084
|
-
this.disConnectDevice(deviceId)
|
|
1085
|
-
await sleep(10 * 1000)
|
|
1086
|
-
const connectRes = await this.connectDevice(device)
|
|
1087
|
-
console.log('connectRes', connectRes)
|
|
1088
|
-
if (!connectRes.success) {
|
|
1089
|
-
GizLog.debug("设备重启后,无法连接")
|
|
1090
|
-
|
|
1091
|
-
return {
|
|
1092
|
-
success: false,
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
// 查询设备版本号
|
|
1097
|
-
const versionRes = await this.getDeviceInfo({deviceId});
|
|
1098
|
-
if (!versionRes.success) {
|
|
1099
|
-
GizLog.debug("获取设备版本号失败")
|
|
1100
|
-
return {
|
|
1101
|
-
success: false,
|
|
1102
|
-
}
|
|
1103
|
-
}
|
|
1104
|
-
let newSoftVersion = ''
|
|
1105
|
-
if (type === 'MCU') {
|
|
1106
|
-
newSoftVersion = versionRes.data.mcuSoftVersion
|
|
1107
|
-
} else {
|
|
1108
|
-
newSoftVersion = versionRes.data.moduleSoftVersion
|
|
1109
|
-
}
|
|
1110
|
-
|
|
1111
|
-
onEvent({
|
|
1112
|
-
code: "GizOTAEventFinish"
|
|
1113
|
-
})
|
|
1114
|
-
|
|
1115
|
-
if (newSoftVersion === softVer) {
|
|
1116
|
-
// 版本是正确的
|
|
1117
|
-
return {
|
|
1118
|
-
success: true,
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
|
|
1122
|
-
GizLog.debug("设备上报版本号与更新的固件版本号不匹配,更新失败")
|
|
1123
|
-
// 版本对不上
|
|
1124
|
-
return {
|
|
1125
|
-
success: false,
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
} else {
|
|
1129
|
-
// 错误
|
|
1130
|
-
return {
|
|
1131
|
-
success: false,
|
|
1132
|
-
}
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
} else {
|
|
1137
|
-
// pre ota 失败,提示错误
|
|
1138
|
-
return {
|
|
1139
|
-
success: false,
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
}
|
|
1143
|
-
|
|
1144
|
-
// 设备拒绝更新
|
|
1145
|
-
return {
|
|
1146
|
-
success: false,
|
|
1147
|
-
err: errorCode.GIZ_SDK_OTA_DEVICE_BUSY_IN_UPGRADE,
|
|
1148
|
-
}
|
|
1149
|
-
}
|
|
1150
|
-
return res;
|
|
1151
|
-
}
|
|
1152
|
-
|
|
1153
|
-
public getDeviceInfo = async({deviceId}: {deviceId: string}) => {
|
|
1154
|
-
// 发送查询指令
|
|
1155
|
-
const value = DeviceInfo.pack();
|
|
1156
|
-
GizLog.debug('DeviceInfo.pack', value)
|
|
1157
|
-
|
|
1158
|
-
await this.write(deviceId, numberArray2Uint8Array(value).buffer);
|
|
1159
|
-
|
|
1160
|
-
const filterFunc = (_, hexString) => {
|
|
1161
|
-
const bleBytesData = hexStrint2byte(hexString)
|
|
1162
|
-
const parseData = new ProtocolBase(bleBytesData);
|
|
1163
|
-
switch (parseData.cmd) {
|
|
1164
|
-
case '0014': {
|
|
1165
|
-
try {
|
|
1166
|
-
const deviceInfoData = new DeviceInfo(bleBytesData);
|
|
1167
|
-
GizLog.debug('parse DeviceInfo', deviceInfoData)
|
|
1168
|
-
|
|
1169
|
-
return deviceInfoData;
|
|
1170
|
-
} catch (error) {
|
|
1171
|
-
GizLog.debug('parse DeviceInfo error', error)
|
|
1172
|
-
}
|
|
1173
|
-
}
|
|
1174
|
-
}
|
|
1175
|
-
return null;
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
const versionRes = await this.handleWatchBleData<DeviceInfo>(deviceId, filterFunc);
|
|
1179
|
-
return versionRes
|
|
1180
|
-
}
|
|
1181
|
-
|
|
1182
|
-
public checkUpdate =async ({device, type}: CheckOTAParams) => {
|
|
1183
|
-
/**
|
|
1184
|
-
* 获取设备硬件版本号
|
|
1185
|
-
* 查询云端接口
|
|
1186
|
-
*/
|
|
1187
|
-
|
|
1188
|
-
const versionRes = await this.getDeviceInfo({deviceId: device.bleDeviceID});
|
|
1189
|
-
|
|
1190
|
-
if (versionRes.success) {
|
|
1191
|
-
let softVersion = '';
|
|
1192
|
-
let hardVersion = '';
|
|
1193
|
-
if (type === 'MCU') {
|
|
1194
|
-
softVersion = versionRes.data.mcuSoftVersion;
|
|
1195
|
-
hardVersion = versionRes.data.mcuHardVersion;
|
|
1196
|
-
} else {
|
|
1197
|
-
softVersion = versionRes.data.moduleSoftVersion;
|
|
1198
|
-
hardVersion = versionRes.data.moduleHardVersion;
|
|
1199
|
-
}
|
|
1200
|
-
const updateRes = await checkUpdate({did: device.did, type, softVersion, hardVersion})
|
|
1201
|
-
return updateRes;
|
|
1202
|
-
}
|
|
1203
|
-
return {
|
|
1204
|
-
success: false,
|
|
1205
|
-
err: versionRes.err
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
}
|
|
1209
|
-
public watchBleDevice = async (
|
|
1210
|
-
deviceId,
|
|
1211
|
-
func
|
|
1212
|
-
) => {
|
|
1213
|
-
const { serviceId, characteristicId, errMsg } =
|
|
1214
|
-
this.getTargetDevice(deviceId);
|
|
1215
|
-
if (errMsg) return { success: false, message: errMsg };
|
|
1216
|
-
await wx
|
|
1217
|
-
.notifyBLECharacteristicValueChange({
|
|
1218
|
-
deviceId,
|
|
1219
|
-
serviceId: serviceId!,
|
|
1220
|
-
characteristicId: characteristicId!,
|
|
1221
|
-
state: true,
|
|
1222
|
-
type: 'notification',
|
|
1223
|
-
})
|
|
1224
|
-
.catch((err) => this.notifyError(err));
|
|
1225
|
-
|
|
1226
|
-
GizLog.log(`监听设备 ${deviceId} 成功!`);
|
|
1227
|
-
this.addEventListener('GizBleDeviceData', func);
|
|
1228
|
-
return {
|
|
1229
|
-
message: 'ok',
|
|
1230
|
-
success: true,
|
|
1231
|
-
};
|
|
1232
|
-
};
|
|
1233
|
-
|
|
1234
|
-
public writeBoradcast = async (bleDeviceID: string, value: number[]) => {
|
|
1235
|
-
const scanDevice = this.scanList.find(item => item.bleDeviceID === bleDeviceID);
|
|
1236
|
-
if (!scanDevice) {
|
|
1237
|
-
return {
|
|
1238
|
-
success: false,
|
|
1239
|
-
message: 'not found device',
|
|
1240
|
-
};
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
|
-
if (this.boradcastTimer) {
|
|
1244
|
-
clearTimeout(this.boradcastTimer);
|
|
1245
|
-
}
|
|
1246
|
-
try {
|
|
1247
|
-
this.peripheralServer.stopAdvertising({});
|
|
1248
|
-
const serviceUuids = convertToServiceUuids({
|
|
1249
|
-
mac: scanDevice.mac,
|
|
1250
|
-
index: 0,
|
|
1251
|
-
data: value,
|
|
1252
|
-
len: value.length
|
|
1253
|
-
});
|
|
1254
|
-
this.peripheralServer.startAdvertising({
|
|
1255
|
-
advertiseRequest: {
|
|
1256
|
-
connectable: true,
|
|
1257
|
-
serviceUuids,
|
|
1258
|
-
},
|
|
1259
|
-
powerLevel: 'high',
|
|
1260
|
-
success: (data) => {
|
|
1261
|
-
GizLog.debug('GIZ_SDK: startAdvertising success', data)
|
|
1262
|
-
},
|
|
1263
|
-
fail: (err) => {
|
|
1264
|
-
GizLog.error('GIZ_SDK: startAdvertising error', new Error(JSON.stringify(err)))
|
|
1265
|
-
},
|
|
1266
|
-
});
|
|
1267
|
-
GizLog.debug("GIZ_SDK: start adv", serviceUuids)
|
|
1268
|
-
|
|
1269
|
-
this.boradcastTimer = setTimeout(() => {
|
|
1270
|
-
this.peripheralServer.stopAdvertising({});
|
|
1271
|
-
this.boradcastTimer = null;
|
|
1272
|
-
}, 1000);
|
|
1273
|
-
return {
|
|
1274
|
-
success: true,
|
|
1275
|
-
}
|
|
1276
|
-
} catch (error) {
|
|
1277
|
-
GizLog.log('GIZ_SDK:', error)
|
|
1278
|
-
clearTimeout(this.boradcastTimer);
|
|
1279
|
-
return {
|
|
1280
|
-
success: false,
|
|
1281
|
-
message: 'send cmd error',
|
|
1282
|
-
};
|
|
1283
|
-
}
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
write = async (bleDeviceID, value) => {
|
|
1290
|
-
const writeMethod = async () => {
|
|
1291
|
-
GizLog.log('app => dev:', ab2hex(value));
|
|
1292
|
-
const targetDevice = this.getTargetDevice(bleDeviceID);
|
|
1293
|
-
if (targetDevice.serviceId && targetDevice.characteristicId) {
|
|
1294
|
-
const unpackRes = await unpackWriteBLECharacteristicValue(
|
|
1295
|
-
bleDeviceID,
|
|
1296
|
-
targetDevice.serviceId,
|
|
1297
|
-
targetDevice.characteristicId,
|
|
1298
|
-
value
|
|
1299
|
-
);
|
|
1300
|
-
return unpackRes;
|
|
1301
|
-
}
|
|
1302
|
-
return {
|
|
1303
|
-
success: false,
|
|
1304
|
-
message: 'send cmd error',
|
|
1305
|
-
};
|
|
1306
|
-
};
|
|
1307
|
-
|
|
1308
|
-
// 将当前方法添加到方法队列中,并等待前一个方法执行完成后再执行
|
|
1309
|
-
this.methodQueue = this.methodQueue.then(writeMethod);
|
|
1310
|
-
|
|
1311
|
-
return this.methodQueue;
|
|
1312
|
-
};
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
/**
|
|
1316
|
-
* @description 断开设备连接
|
|
1317
|
-
*/
|
|
1318
|
-
public disConnectDevice = async (deviceId: string) => {
|
|
1319
|
-
this.removeDevById(deviceId);
|
|
1320
|
-
const { errMsg } = await wx.closeBLEConnection({
|
|
1321
|
-
deviceId,
|
|
1322
|
-
});
|
|
1323
|
-
|
|
1324
|
-
const target = this.scanList.find(item => item.bleDeviceID === deviceId);
|
|
1325
|
-
if (target) {
|
|
1326
|
-
target.ctime = Date.now();
|
|
1327
|
-
this.updateDevice(target);
|
|
1328
|
-
}
|
|
1329
|
-
|
|
1330
|
-
if (errMsg === 'closeBLEConnection:ok') {
|
|
1331
|
-
return {
|
|
1332
|
-
err: errMsg,
|
|
1333
|
-
success: true,
|
|
1334
|
-
};
|
|
1335
|
-
}
|
|
1336
|
-
return {
|
|
1337
|
-
err: errMsg,
|
|
1338
|
-
success: false,
|
|
1339
|
-
};
|
|
1340
|
-
};
|
|
1341
|
-
|
|
1342
|
-
private removeDevById = (deviceId: string) => {
|
|
1343
|
-
const targetIdx = this.connectedList.findIndex(
|
|
1344
|
-
(connectedDev) => connectedDev.deviceId === deviceId
|
|
1345
|
-
);
|
|
1346
|
-
if (targetIdx !== -1) {
|
|
1347
|
-
this.connectedList.splice(targetIdx, 1);
|
|
1348
|
-
}
|
|
1349
|
-
};
|
|
1350
|
-
|
|
1351
|
-
private getTargetDevice = (deviceId: string) => {
|
|
1352
|
-
const targetDevice = this.connectedList.find(
|
|
1353
|
-
(device) => deviceId === device.deviceId
|
|
1354
|
-
);
|
|
1355
|
-
if (!targetDevice)
|
|
1356
|
-
return {
|
|
1357
|
-
errMsg: 'Unable get this device,maybe you forgot to connect it.',
|
|
1358
|
-
};
|
|
1359
|
-
|
|
1360
|
-
return targetDevice;
|
|
1361
|
-
};
|
|
1362
|
-
|
|
1363
|
-
public destory = () => {
|
|
1364
|
-
this.listenDevOfflineTimer && clearInterval(this.listenDevOfflineTimer);
|
|
1365
|
-
wx.offAppShow(this.onAppShow);
|
|
1366
|
-
wx.offAppHide(this.onAppHide);
|
|
1367
|
-
wx.offBluetoothAdapterStateChange(this.onBluetoothAdapterStateChange)
|
|
1368
|
-
}
|
|
1369
|
-
}
|