mini_program_gizwits_sdk 3.4.9-configEvent.6 → 3.4.9-configEvent.8

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.
@@ -1,4 +1,4 @@
1
- import { IRandomCodesResult, IResult } from "../sdk";
1
+ import { IRandomCodesResult, IResult, TimeoutHandle } from "../sdk";
2
2
  interface IRejectCallback {
3
3
  (result: IResult<unknown>): void;
4
4
  }
@@ -14,7 +14,7 @@ declare class ConfigBase {
14
14
  specialProductKeys: string[];
15
15
  disableSearchDevice: boolean;
16
16
  specialProductKeySecrets: string[];
17
- private timeoutHandler;
17
+ timeoutHandler: TimeoutHandle;
18
18
  eventCallBack: (event: ConfigEventData) => void;
19
19
  private setDeviceOnboardingDeployRej?;
20
20
  constructor(ssid: string, bssid: string, password: string, specialProductKeys: string[], specialProductKeySecrets: string[]);
@@ -9,7 +9,6 @@ interface IArgs {
9
9
  bleHandle: BleHandle;
10
10
  eventCallBack: (event: ConfigEventData) => void;
11
11
  }
12
- export declare function sendBLEConfigCmd({ bleDeviceId, arrayBuffer, serviceUUIDSuffix, characteristicUUIDSuffix, bleHandle, eventCallBack }: IArgs): Promise<boolean | void>;
13
12
  interface configBLEDeviceParams {
14
13
  ssid: string;
15
14
  password: string;
@@ -25,6 +24,7 @@ interface ISetDeviceOnboardingDeployProps {
25
24
  declare class BLEConfig extends ConfigBase {
26
25
  bleHandle: BleHandle;
27
26
  constructor(ssid: string, bssid: string, password: string, specialProductKeys: string[], specialProductKeySecrets: string[], bleHandle: BleHandle);
27
+ sendBLEConfigCmd: ({ bleDeviceId, arrayBuffer, serviceUUIDSuffix, characteristicUUIDSuffix, bleHandle, eventCallBack }: IArgs) => Promise<boolean | void>;
28
28
  destroy: () => void;
29
29
  isValidBleDevice: (bleDevice: WechatMiniprogram.BlueToothDevice, softAPSSIDPrefix?: string) => boolean;
30
30
  enableBluetoothDevicesDescovery: () => Promise<{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mini_program_gizwits_sdk",
3
- "version": "3.4.9-configEvent.6",
3
+ "version": "3.4.9-configEvent.8",
4
4
  "description": "增加配网回调",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -20,7 +20,7 @@ class ConfigBase {
20
20
  specialProductKeys: string[] = [];
21
21
  disableSearchDevice = false;
22
22
  specialProductKeySecrets: string[] = []
23
- private timeoutHandler: TimeoutHandle = null;
23
+ timeoutHandler: TimeoutHandle = null;
24
24
  eventCallBack: (event: ConfigEventData) => void;
25
25
 
26
26
  private setDeviceOnboardingDeployRej?: (value: unknown) => void = () => {}; // 保存promise的rej,用于临时中断
@@ -23,186 +23,6 @@ interface IArgs {
23
23
 
24
24
  const configAck = '0000000303000002';
25
25
 
26
- export function sendBLEConfigCmd({
27
- bleDeviceId,
28
- arrayBuffer,
29
- serviceUUIDSuffix = 'abf0',
30
- characteristicUUIDSuffix = 'abf7',
31
- bleHandle,
32
- eventCallBack
33
- }: IArgs) {
34
- let sendInterval = null;
35
- let closeTimeout = null;
36
-
37
- const checkInterrupt = () => {
38
- if (closeTimeout === null) {
39
- sendInterval && clearInterval(sendInterval);
40
- return true;
41
- }
42
- return false;
43
- }
44
-
45
- return new Promise<boolean>(async (resolve, reject) => {
46
- try {
47
- GizLog.debug(
48
- 'GIZ_SDK: ssid info: ', arrayBuffer
49
- );
50
- // 设置超时
51
- closeTimeout = setTimeout(() => {
52
- sendInterval && clearInterval(sendInterval);
53
- // 发送超时 返回失败
54
- closeTimeout = null;
55
- resolve(false);
56
- }, 5 * 1000)
57
- GizLog.debug(
58
- 'GIZ_SDK: createBLEConnection start: ', bleDeviceId
59
- );
60
- await bleHandle.disableScan()
61
-
62
- const connectData = await retryConnect(bleDeviceId, 10 * 1000);
63
- eventCallBack?.({
64
- event: "GIZ_CONNECT_SUCCESS",
65
- deviceID: bleDeviceId
66
- })
67
-
68
- bleHandle.enableScan();
69
-
70
- GizLog.debug(
71
- 'GIZ_SDK: createBLEConnection end, res:', connectData
72
- );
73
- const services = await getBLEDeviceServices(bleDeviceId);
74
- GizLog.debug(
75
- 'GIZ_SDK: getBLEDeviceServices end, res:', services
76
- );
77
- const service = services.find((s) =>
78
- s.uuid.split('-')[0].toLowerCase().endsWith(serviceUUIDSuffix)
79
- );
80
- if (!service) {
81
- // 获取蓝牙设备服务异常
82
- GizLog.debug(
83
- 'GIZ_SDK: get ble device services fail',
84
- bleDeviceId,
85
- services
86
- );
87
- resolve(false);
88
- return;
89
- }
90
-
91
- const characteristics = await getBLEDeviceCharacteristics(
92
- bleDeviceId,
93
- service.uuid
94
- );
95
- GizLog.debug(
96
- 'GIZ_SDK: getBLEDeviceCharacteristics end, res:', characteristics
97
- );
98
- const characteristic = characteristics.find((c) =>
99
- c.uuid.split('-')[0].toLowerCase().endsWith(characteristicUUIDSuffix)
100
- );
101
- if (!characteristic) {
102
- // 获取蓝牙设备特征值异常
103
- GizLog.debug(
104
- 'GIZ_SDK: get ble device characteristics fail',
105
- bleDeviceId,
106
- characteristics
107
- );
108
- resolve(false);
109
- return;
110
- }
111
-
112
- if (
113
- !characteristic.properties.notify &&
114
- !characteristic.properties.indicate
115
- ) {
116
- GizLog.debug(
117
- 'GIZ_SDK: the ble device characteristic not support notify or indicate',
118
- bleDeviceId,
119
- characteristic
120
- );
121
- // 该设备不支持 notify & indicate 操作
122
- resolve(false);
123
- return;
124
- }
125
-
126
- GizLog.debug(
127
- 'GIZ_SDK: check characteristic and service success',
128
- );
129
-
130
-
131
- await notifyBLECharacteristicValueChange(
132
- bleDeviceId,
133
- service.uuid,
134
- characteristic.uuid
135
- );
136
-
137
- eventCallBack?.({
138
- event: "GIZ_CONFIG_SEND_SUCCESS",
139
- deviceID: bleDeviceId
140
- })
141
-
142
- // 这里有个问题,模组好像不一定每次都会回
143
- const handleBLECharacteristicValueChange = (res: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult) => {
144
- const hexString = ab2hex(res.value);
145
- GizLog.debug('GIZ_SDK: 收到设备返回ack', hexString)
146
- eventCallBack({
147
- event: "GIZ_CONFIG_RECV_SUCCESS",
148
- deviceID: bleDeviceId
149
- })
150
- if (hexString === configAck) {
151
- // 发送成功
152
- bleHandle.removeEventListener("GizBleDeviceData", handleBLECharacteristicValueChange)
153
- resolve(true);
154
- }
155
- };
156
-
157
- bleHandle.addEventListener("GizBleDeviceData", handleBLECharacteristicValueChange)
158
-
159
- // 订阅特征值变化
160
- // wx.onBLECharacteristicValueChange(handleBLECharacteristicValueChange);
161
-
162
- GizLog.debug(
163
- 'GIZ_SDK: on notifyBLECharacteristicValueChange success',
164
- );
165
-
166
- if (checkInterrupt()) return;
167
-
168
- await unpackWriteBLECharacteristicValue(
169
- bleDeviceId,
170
- service.uuid,
171
- characteristic.uuid,
172
- arrayBuffer
173
- );
174
- sendInterval = setInterval(() => {
175
- if (checkInterrupt()) return;
176
- unpackWriteBLECharacteristicValue(
177
- bleDeviceId,
178
- service.uuid,
179
- characteristic.uuid,
180
- arrayBuffer
181
- );
182
- }, 2000)
183
-
184
- // if (res.errCode === 0) {
185
- // resolve(true);
186
- // } else{
187
- // resolve(false);
188
- // }
189
- GizLog.debug('GIZ_SDK: unpackWriteBLECharacteristicValue end');
190
- } catch (error) {
191
- reject(false);
192
- GizLog.debug('GIZ_SDK: sendBLEConfigCmd error', error);
193
- }
194
- })
195
- .catch((error) => {
196
- GizLog.debug('GIZ_SDK: sendBLEConfigCmd error', error);
197
- })
198
- .finally(() => {
199
- // 关闭连接
200
- closeTimeout && clearTimeout(closeTimeout);
201
- sendInterval && clearInterval(sendInterval);
202
- wx.closeBLEConnection({ deviceId: bleDeviceId });
203
- });
204
- }
205
-
206
26
  interface configBLEDeviceParams {
207
27
  ssid: string;
208
28
  password: string;
@@ -231,6 +51,189 @@ class BLEConfig extends ConfigBase {
231
51
  super(ssid, bssid, password,specialProductKeys,specialProductKeySecrets)
232
52
  this.bleHandle = bleHandle;
233
53
  }
54
+
55
+ sendBLEConfigCmd = ({
56
+ bleDeviceId,
57
+ arrayBuffer,
58
+ serviceUUIDSuffix = 'abf0',
59
+ characteristicUUIDSuffix = 'abf7',
60
+ bleHandle,
61
+ eventCallBack
62
+ }: IArgs) => {
63
+ let sendInterval = null;
64
+ let closeTimeout = null;
65
+
66
+ const checkInterrupt = () => {
67
+ if (closeTimeout === null) {
68
+ sendInterval && clearInterval(sendInterval);
69
+ return true;
70
+ }
71
+ return !this.hasTimeoutHandler();
72
+ }
73
+
74
+ return new Promise<boolean>(async (resolve, reject) => {
75
+ try {
76
+ GizLog.debug(
77
+ 'GIZ_SDK: ssid info: ', arrayBuffer
78
+ );
79
+ // 设置超时
80
+ closeTimeout = setTimeout(() => {
81
+ sendInterval && clearInterval(sendInterval);
82
+ // 发送超时 返回失败
83
+ closeTimeout = null;
84
+ resolve(false);
85
+ }, 5 * 1000)
86
+ GizLog.debug(
87
+ 'GIZ_SDK: createBLEConnection start: ', bleDeviceId
88
+ );
89
+ await bleHandle.disableScan()
90
+
91
+ const connectData = await retryConnect(bleDeviceId, 10 * 1000);
92
+
93
+ eventCallBack?.({
94
+ event: "GIZ_CONNECT_SUCCESS",
95
+ deviceID: bleDeviceId
96
+ })
97
+
98
+ bleHandle.enableScan();
99
+
100
+ GizLog.debug(
101
+ 'GIZ_SDK: createBLEConnection end, res:', connectData
102
+ );
103
+ const services = await getBLEDeviceServices(bleDeviceId);
104
+ GizLog.debug(
105
+ 'GIZ_SDK: getBLEDeviceServices end, res:', services
106
+ );
107
+ const service = services.find((s) =>
108
+ s.uuid.split('-')[0].toLowerCase().endsWith(serviceUUIDSuffix)
109
+ );
110
+ if (!service) {
111
+ // 获取蓝牙设备服务异常
112
+ GizLog.debug(
113
+ 'GIZ_SDK: get ble device services fail',
114
+ bleDeviceId,
115
+ services
116
+ );
117
+ resolve(false);
118
+ return;
119
+ }
120
+
121
+ const characteristics = await getBLEDeviceCharacteristics(
122
+ bleDeviceId,
123
+ service.uuid
124
+ );
125
+ GizLog.debug(
126
+ 'GIZ_SDK: getBLEDeviceCharacteristics end, res:', characteristics
127
+ );
128
+ const characteristic = characteristics.find((c) =>
129
+ c.uuid.split('-')[0].toLowerCase().endsWith(characteristicUUIDSuffix)
130
+ );
131
+ if (!characteristic) {
132
+ // 获取蓝牙设备特征值异常
133
+ GizLog.debug(
134
+ 'GIZ_SDK: get ble device characteristics fail',
135
+ bleDeviceId,
136
+ characteristics
137
+ );
138
+ resolve(false);
139
+ return;
140
+ }
141
+
142
+ if (
143
+ !characteristic.properties.notify &&
144
+ !characteristic.properties.indicate
145
+ ) {
146
+ GizLog.debug(
147
+ 'GIZ_SDK: the ble device characteristic not support notify or indicate',
148
+ bleDeviceId,
149
+ characteristic
150
+ );
151
+ // 该设备不支持 notify & indicate 操作
152
+ resolve(false);
153
+ return;
154
+ }
155
+
156
+ GizLog.debug(
157
+ 'GIZ_SDK: check characteristic and service success',
158
+ );
159
+
160
+
161
+ await notifyBLECharacteristicValueChange(
162
+ bleDeviceId,
163
+ service.uuid,
164
+ characteristic.uuid
165
+ );
166
+
167
+ eventCallBack?.({
168
+ event: "GIZ_CONFIG_SEND_SUCCESS",
169
+ deviceID: bleDeviceId
170
+ })
171
+
172
+ // 这里有个问题,模组好像不一定每次都会回
173
+ const handleBLECharacteristicValueChange = (res: WechatMiniprogram.OnBLECharacteristicValueChangeListenerResult) => {
174
+ const hexString = ab2hex(res.value);
175
+ GizLog.debug('GIZ_SDK: 收到设备返回ack', hexString)
176
+ eventCallBack({
177
+ event: "GIZ_CONFIG_RECV_SUCCESS",
178
+ deviceID: bleDeviceId
179
+ })
180
+ if (hexString === configAck) {
181
+ // 发送成功
182
+ bleHandle.removeEventListener("GizBleDeviceData", handleBLECharacteristicValueChange)
183
+ resolve(true);
184
+ }
185
+ };
186
+
187
+ bleHandle.addEventListener("GizBleDeviceData", handleBLECharacteristicValueChange)
188
+
189
+ // 订阅特征值变化
190
+ // wx.onBLECharacteristicValueChange(handleBLECharacteristicValueChange);
191
+
192
+ GizLog.debug(
193
+ 'GIZ_SDK: on notifyBLECharacteristicValueChange success',
194
+ );
195
+
196
+ if (checkInterrupt()) return;
197
+
198
+ await unpackWriteBLECharacteristicValue(
199
+ bleDeviceId,
200
+ service.uuid,
201
+ characteristic.uuid,
202
+ arrayBuffer
203
+ );
204
+ sendInterval = setInterval(() => {
205
+ if (checkInterrupt()) return;
206
+ unpackWriteBLECharacteristicValue(
207
+ bleDeviceId,
208
+ service.uuid,
209
+ characteristic.uuid,
210
+ arrayBuffer
211
+ );
212
+ }, 2000)
213
+
214
+ // if (res.errCode === 0) {
215
+ // resolve(true);
216
+ // } else{
217
+ // resolve(false);
218
+ // }
219
+ GizLog.debug('GIZ_SDK: unpackWriteBLECharacteristicValue end');
220
+ } catch (error) {
221
+ reject(false);
222
+ GizLog.debug('GIZ_SDK: sendBLEConfigCmd error', error);
223
+ }
224
+ })
225
+ .catch((error) => {
226
+ GizLog.debug('GIZ_SDK: sendBLEConfigCmd error', error);
227
+ })
228
+ .finally(() => {
229
+ // 关闭连接
230
+ closeTimeout && clearTimeout(closeTimeout);
231
+ sendInterval && clearInterval(sendInterval);
232
+ wx.closeBLEConnection({ deviceId: bleDeviceId });
233
+ });
234
+ }
235
+
236
+
234
237
  destroy = () => {
235
238
  this.cleanTimeout();
236
239
  }
@@ -298,9 +301,10 @@ class BLEConfig extends ConfigBase {
298
301
  }
299
302
  GizLog.debug('GIZ_SDK: start enableAndGetBluetoothDevices');
300
303
 
301
- const bleDevices: WechatMiniprogram.BlueToothDevice[] = (
302
- await getBluetoothDevices()
303
- ).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));
304
+ // const bleDevices: WechatMiniprogram.BlueToothDevice[] = (
305
+ // await getBluetoothDevices()
306
+ // ).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));
307
+ const bleDevices: WechatMiniprogram.BlueToothDevice[] = []
304
308
  GizLog.debug('GIZ_SDK: getBluetoothDevices success', bleDevices);
305
309
  return {
306
310
  success: true,
@@ -395,20 +399,23 @@ class BLEConfig extends ConfigBase {
395
399
 
396
400
  GizLog.debug('GIZ_SDK: enableAndGetBluetoothDevices success, target devices: ', bleDevices);
397
401
 
398
- // const handleFoundDevices = async ({
399
- // devices,
400
- // }: {
401
- // devices: WechatMiniprogram.BlueToothDevice[];
402
- // }) => {
403
- // this.hasTimeoutHandler()
404
- // ? Array.prototype.push.apply(
405
- // bleDevices,
406
- // devices.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix))
407
- // )
408
- // : wx.offBluetoothDeviceFound(()=>{});
409
- // };
410
-
411
- // wx.onBluetoothDeviceFound(handleFoundDevices);
402
+ const handleFoundDevices = async ({
403
+ devices,
404
+ }: {
405
+ devices: WechatMiniprogram.BlueToothDevice[];
406
+ }) => {
407
+ if(this.hasTimeoutHandler()) {
408
+ Array.prototype.push.apply(
409
+ bleDevices,
410
+ devices.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix))
411
+ )
412
+ } else {
413
+ wx.offBluetoothDeviceFound(()=>{});
414
+ }
415
+ };
416
+
417
+ wx.onBluetoothDeviceFound(handleFoundDevices);
418
+ await startBluetoothDevicesDiscovery();
412
419
 
413
420
  const appendPassword = this.appendExtendData(password)
414
421
  const uint8Array = WifiConfig.pack(ssid,appendPassword);
@@ -428,7 +435,7 @@ class BLEConfig extends ConfigBase {
428
435
 
429
436
  const success =
430
437
  bleDevice &&
431
- (await sendBLEConfigCmd({
438
+ (await this.sendBLEConfigCmd({
432
439
  bleDeviceId: bleDevice.deviceId,
433
440
  arrayBuffer: uint8Array.buffer,
434
441
  bleHandle: this.bleHandle,
@@ -438,7 +445,7 @@ class BLEConfig extends ConfigBase {
438
445
  if (!success) {
439
446
  // 如果校验设备或者发送不成功,重试下一个设备
440
447
  await sleep(500);
441
- bleDevices = (await getBluetoothDevices()).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));;
448
+ // bleDevices = (await getBluetoothDevices()).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));;
442
449
  // await startBluetoothDevicesDiscovery();
443
450
  await startConfigDevice();
444
451
  return;
@@ -461,6 +468,7 @@ class BLEConfig extends ConfigBase {
461
468
  }
462
469
 
463
470
  await startConfigDevice();
471
+
464
472
  });
465
473
  };
466
474
  }