mini_program_gizwits_sdk 3.4.9-configEvent.7 → 3.4.9-configEvent.9

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.
@@ -112,7 +112,7 @@ export declare class BleHandle extends EventListener<TBleHandleEvent> {
112
112
  private updateCtime;
113
113
  private checkDeviceIsChange;
114
114
  private handleBluetoothDeviceFound;
115
- startScan: (onScanDevice: OnScanDevice, delay?: number, services?: string[]) => Promise<IStartScanResult>;
115
+ startScan: (delay?: number, services?: string[]) => Promise<IStartScanResult>;
116
116
  stopScan: () => Promise<any>;
117
117
  disableScan: () => Promise<WechatMiniprogram.BluetoothError>;
118
118
  enableScan: () => void;
@@ -1,6 +1,13 @@
1
1
  import { IRandomCodesResult, IResult } from "../sdk";
2
2
  import { BleHandle } from "../handler/ble";
3
3
  import ConfigBase, { ConfigEventData } from "./ConfigBase";
4
+ type EnableAndGetBluetoothDevicesRes = Promise<{
5
+ success: false;
6
+ err: IError;
7
+ } | {
8
+ success: true;
9
+ bleDevices?: IDevice[];
10
+ }>;
4
11
  interface IArgs {
5
12
  bleDeviceId: string;
6
13
  arrayBuffer: ArrayBuffer;
@@ -26,20 +33,14 @@ declare class BLEConfig extends ConfigBase {
26
33
  constructor(ssid: string, bssid: string, password: string, specialProductKeys: string[], specialProductKeySecrets: string[], bleHandle: BleHandle);
27
34
  sendBLEConfigCmd: ({ bleDeviceId, arrayBuffer, serviceUUIDSuffix, characteristicUUIDSuffix, bleHandle, eventCallBack }: IArgs) => Promise<boolean | void>;
28
35
  destroy: () => void;
29
- isValidBleDevice: (bleDevice: WechatMiniprogram.BlueToothDevice, softAPSSIDPrefix?: string) => boolean;
36
+ isValidBleDevice: (device: IDevice, softAPSSIDPrefix?: string) => boolean;
30
37
  enableBluetoothDevicesDescovery: () => Promise<{
31
38
  success: false;
32
39
  err: IError;
33
40
  } | {
34
41
  success: true;
35
42
  }>;
36
- enableAndGetBluetoothDevices: (softAPSSIDPrefix?: string) => Promise<{
37
- success: false;
38
- err: IError;
39
- } | {
40
- success: true;
41
- bleDevices?: WechatMiniprogram.BlueToothDevice[];
42
- }>;
43
+ enableAndGetBluetoothDevices: (softAPSSIDPrefix?: string) => EnableAndGetBluetoothDevicesRes;
43
44
  setDeviceOnboardingDeploy: ({ timeout, isBind, softAPSSIDPrefix, eventCallBack, }: ISetDeviceOnboardingDeployProps) => Promise<IResult<IDevice[]>>;
44
45
  configBLEDevice: ({ ssid, password, softAPSSIDPrefix, }: configBLEDeviceParams) => Promise<IResult<IRandomCodesResult[]>>;
45
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mini_program_gizwits_sdk",
3
- "version": "3.4.9-configEvent.7",
3
+ "version": "3.4.9-configEvent.9",
4
4
  "description": "增加配网回调",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
@@ -419,6 +419,10 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
419
419
  }
420
420
  const res = await this.getBluetoothAdapterState();
421
421
  this.isInitSuccess = true
422
+
423
+ wx.onBluetoothDeviceFound((resData) => {
424
+ this.handleBluetoothDeviceFound(resData);
425
+ });
422
426
  return res;
423
427
  }
424
428
 
@@ -501,7 +505,6 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
501
505
 
502
506
  private handleBluetoothDeviceFound = (
503
507
  resData: WechatMiniprogram.OnBluetoothDeviceFoundListenerResult,
504
- onScanDevice: OnScanDevice
505
508
  ) => {
506
509
  /**
507
510
  * TODO
@@ -565,7 +568,7 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
565
568
  this.updateDevice(newDevice);
566
569
  // GizLog.debug("GIZ_SDK: same device", newDevice, isChange);
567
570
  if (isChange) {
568
- onScanDevice(this.scanList);
571
+ this.notifyDevices();
569
572
  }
570
573
  }
571
574
  } else {
@@ -579,7 +582,7 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
579
582
  const newDevice = BTDevice2GDevice(device, this.pks);
580
583
  if (newDevice.productKey && newDevice.mac) {
581
584
  this.scanList.push(newDevice);
582
- onScanDevice(this.scanList);
585
+ this.notifyDevices();
583
586
  }
584
587
  }
585
588
  }
@@ -587,7 +590,6 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
587
590
  };
588
591
 
589
592
  public startScan = async (
590
- onScanDevice: OnScanDevice,
591
593
  delay: number = 5000,
592
594
  services?: string[]
593
595
  ) => {
@@ -597,10 +599,6 @@ export class BleHandle extends EventListener<TBleHandleEvent> {
597
599
  }
598
600
  }
599
601
  return new Promise<IStartScanResult>(async (res) => {
600
- wx.onBluetoothDeviceFound((resData) => {
601
- this.handleBluetoothDeviceFound(resData, onScanDevice);
602
- });
603
-
604
602
  GizLog.debug("GIZ_SDK: start scan", delay);
605
603
  await wx.startBluetoothDevicesDiscovery({
606
604
  // 如果有传的话,就按传入的方式来搜索
package/src/sdk.ts CHANGED
@@ -680,13 +680,9 @@ class GizwitsMiniSDK {
680
680
  // this.bleDevices = deviceList;
681
681
  // }, -1);
682
682
  // }
683
- this.bleHandle.startScan((deviceList) => {
684
- this.bleDevices = deviceList;
685
- }, -1, services);
683
+ this.bleHandle.startScan( -1, services);
686
684
  }, autoTime);
687
- this.bleHandle.startScan((deviceList) => {
688
- this.bleDevices = deviceList;
689
- }, -1, services);
685
+ this.bleHandle.startScan(-1, services);
690
686
  };
691
687
 
692
688
  /**
@@ -788,9 +784,7 @@ class GizwitsMiniSDK {
788
784
  services?: string[]
789
785
  ): Promise<ISDKResult<IDevice[]>> => {
790
786
  this.bleHandle.scanList = [];
791
- const data = await this.bleHandle.startScan((deviceList) => {
792
- this.bleDevices = deviceList;
793
- }, delay, services);
787
+ const data = await this.bleHandle.startScan(delay, services);
794
788
  return {
795
789
  success: true,
796
790
  data: data.scanList,
@@ -12,6 +12,11 @@ import {
12
12
  } from '../wechatApi';
13
13
  import ConfigBase, { ConfigEventData } from "./ConfigBase";
14
14
 
15
+ type EnableAndGetBluetoothDevicesRes = Promise<
16
+ | { success: false; err: IError }
17
+ | { success: true; bleDevices?: IDevice[] }
18
+ >;
19
+
15
20
  interface IArgs {
16
21
  bleDeviceId: string;
17
22
  arrayBuffer: ArrayBuffer;
@@ -237,31 +242,41 @@ class BLEConfig extends ConfigBase {
237
242
  destroy = () => {
238
243
  this.cleanTimeout();
239
244
  }
240
- isValidBleDevice = (
241
- bleDevice: WechatMiniprogram.BlueToothDevice,
242
- softAPSSIDPrefix?: string
243
- ) => {
244
- if (!bleDevice || !bleDevice.advertisData) {
245
- // 无效蓝牙设备或者蓝牙设备广播数据为空,返回失败
245
+ isValidBleDevice = (device:IDevice, softAPSSIDPrefix?: string) => {
246
+ if (device.bleWorkStatus === 0) return false;
247
+ if (!this.specialProductKeys.includes(device.productKey)) {
246
248
  return false;
247
249
  }
248
250
 
249
- const {productKey, mac, bleWorkStatus} = advertisData2PkAndMac(bleDevice.advertisData, this.specialProductKeys)
250
-
251
-
252
- if (!this.specialProductKeys.includes(productKey)) {
253
- // 不在PK列表
254
- return false;
255
- }
256
- // 判断广播包是否可以配网
257
- if (bleWorkStatus !== 1) return;
258
- GizLog.debug(`GIZ_SDK: isValidBleDevice mac: ${mac} pk: ${productKey} name: ${bleDevice.name}`)
259
- GizLog.debug(`GIZ_SDK: softAPSSIDPrefix: ${softAPSSIDPrefix} ismatch : ${bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix)}`)
251
+ GizLog.debug(`GIZ_SDK: isValidBleDevice mac: ${device.mac} pk: ${device.productKey} name: ${device.name}`)
252
+ GizLog.debug(`GIZ_SDK: softAPSSIDPrefix: ${softAPSSIDPrefix} ismatch : ${device.name && device.name.startsWith(softAPSSIDPrefix)}`)
260
253
  return (
261
254
  !softAPSSIDPrefix ||
262
- (bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix))
263
- );
264
- };
255
+ (device.name && device.name.startsWith(softAPSSIDPrefix))
256
+ )
257
+ }
258
+ // isValidBleDevice = (
259
+ // bleDevice: WechatMiniprogram.BlueToothDevice,
260
+ // softAPSSIDPrefix?: string
261
+ // ) => {
262
+ // if (!bleDevice || !bleDevice.advertisData) {
263
+ // // 无效蓝牙设备或者蓝牙设备广播数据为空,返回失败
264
+ // return false;
265
+ // }
266
+ // const {productKey, mac, bleWorkStatus} = advertisData2PkAndMac(bleDevice.advertisData, this.specialProductKeys)
267
+ // if (!this.specialProductKeys.includes(productKey)) {
268
+ // // 不在PK列表
269
+ // return false;
270
+ // }
271
+ // // 判断广播包是否可以配网
272
+ // if (bleWorkStatus !== 1) return;
273
+ // GizLog.debug(`GIZ_SDK: isValidBleDevice mac: ${mac} pk: ${productKey} name: ${bleDevice.name}`)
274
+ // GizLog.debug(`GIZ_SDK: softAPSSIDPrefix: ${softAPSSIDPrefix} ismatch : ${bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix)}`)
275
+ // return (
276
+ // !softAPSSIDPrefix ||
277
+ // (bleDevice.name && bleDevice.name.startsWith(softAPSSIDPrefix))
278
+ // );
279
+ // };
265
280
 
266
281
 
267
282
  enableBluetoothDevicesDescovery = async (): Promise<
@@ -291,19 +306,17 @@ class BLEConfig extends ConfigBase {
291
306
 
292
307
  enableAndGetBluetoothDevices = async (
293
308
  softAPSSIDPrefix?: string
294
- ): Promise<
295
- | { success: false; err: IError }
296
- | { success: true; bleDevices?: WechatMiniprogram.BlueToothDevice[] }
297
- > => {
309
+ ): EnableAndGetBluetoothDevicesRes => {
298
310
  const discoveryRes = await this.enableBluetoothDevicesDescovery();
299
311
  if (!discoveryRes.success) {
300
312
  return discoveryRes;
301
313
  }
302
314
  GizLog.debug('GIZ_SDK: start enableAndGetBluetoothDevices');
303
315
 
304
- const bleDevices: WechatMiniprogram.BlueToothDevice[] = (
305
- await getBluetoothDevices()
306
- ).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));
316
+ // const bleDevices: WechatMiniprogram.BlueToothDevice[] = (
317
+ // await getBluetoothDevices()
318
+ // ).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));
319
+ const bleDevices: IDevice[] = this.bleHandle.scanList.filter(d => this.isValidBleDevice(d, softAPSSIDPrefix))
307
320
  GizLog.debug('GIZ_SDK: getBluetoothDevices success', bleDevices);
308
321
  return {
309
322
  success: true,
@@ -394,26 +407,78 @@ class BLEConfig extends ConfigBase {
394
407
  return rej(enableAndGetRes);
395
408
  }
396
409
 
397
- let { bleDevices } = enableAndGetRes;
410
+ let bleDevices: IDevice[] = enableAndGetRes.bleDevices as IDevice[];
398
411
 
399
412
  GizLog.debug('GIZ_SDK: enableAndGetBluetoothDevices success, target devices: ', bleDevices);
400
413
 
401
- const handleFoundDevices = async ({
402
- devices,
403
- }: {
404
- devices: WechatMiniprogram.BlueToothDevice[];
405
- }) => {
406
- if(this.hasTimeoutHandler()) {
407
- Array.prototype.push.apply(
408
- bleDevices,
409
- devices.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix))
410
- )
411
- } else {
412
- wx.offBluetoothDeviceFound(()=>{});
414
+ // const handleFoundDevices = async ({
415
+ // devices,
416
+ // }: {
417
+ // devices: WechatMiniprogram.BlueToothDevice[];
418
+ // }) => {
419
+ // if(this.hasTimeoutHandler()) {
420
+ // Array.prototype.push.apply(
421
+ // bleDevices,
422
+ // devices.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix))
423
+ // )
424
+ // } else {
425
+ // wx.offBluetoothDeviceFound(()=>{});
426
+ // }
427
+ // };
428
+
429
+ // wx.onBluetoothDeviceFound(handleFoundDevices);
430
+ await startBluetoothDevicesDiscovery(true);
431
+
432
+ const appendPassword = this.appendExtendData(password)
433
+ const uint8Array = WifiConfig.pack(ssid,appendPassword);
434
+ const startConfigDevice = async () => {
435
+ GizLog.debug('GIZ_SDK: startConfigDevice');
436
+ if (!this.hasTimeoutHandler()) {
437
+ return;
413
438
  }
439
+ const bleDevice = bleDevices.shift() as IDevice;
440
+ if (bleDevice) {
441
+ this.eventCallBack?.({
442
+ event: "GIZ_DISCOVER_SUCCESS",
443
+ deviceID: bleDevice.bleDeviceID
444
+ })
445
+ GizLog.debug('GIZ_SDK: startConfigDevice, target device: ', bleDevice);
446
+ }
447
+
448
+ const success =
449
+ bleDevice &&
450
+ (await this.sendBLEConfigCmd({
451
+ bleDeviceId: bleDevice.bleDeviceID,
452
+ arrayBuffer: uint8Array.buffer,
453
+ bleHandle: this.bleHandle,
454
+ eventCallBack: this.eventCallBack
455
+ }));
456
+
457
+ if (!success) {
458
+ // 如果校验设备或者发送不成功,重试下一个设备
459
+ await sleep(500);
460
+ bleDevices = this.bleHandle.scanList.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));;
461
+ await startConfigDevice();
462
+ return;
463
+ }
464
+
465
+ // 如果有一个设备发送成功,则不再发现新设备
466
+ GizLog.debug("GIZ_SDK: offBluetoothDeviceFound ready search device")
467
+ // wx.offBluetoothDeviceFound(() => {});
414
468
  };
415
469
 
416
- wx.onBluetoothDeviceFound(handleFoundDevices);
470
+ // 开始大循环搜索
471
+ try {
472
+ // const devicesReturn = await this.searchDevice({ ssid, password });
473
+ this.searchDevice({ ssid, password }).then(devicesReturn => {
474
+ res(devicesReturn);
475
+ })
476
+ } catch (error) {
477
+ rej(error);
478
+ GizLog.error("GIZ_SDK: searchDevice error", new Error(JSON.stringify(error)))
479
+ }
480
+
481
+ await startConfigDevice();
417
482
 
418
483
  });
419
484
  };