mini_program_gizwits_sdk 3.4.9-configEvent.8 → 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.
- package/dist/index.js +2 -2
- package/dist/src/handler/ble.d.ts +1 -1
- package/dist/src/wifiConfig/ble.d.ts +9 -8
- package/package.json +1 -1
- package/src/handler/ble.ts +6 -8
- package/src/sdk.ts +3 -9
- package/src/wifiConfig/ble.ts +60 -49
|
@@ -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: (
|
|
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: (
|
|
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) =>
|
|
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
package/src/handler/ble.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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(
|
|
684
|
-
this.bleDevices = deviceList;
|
|
685
|
-
}, -1, services);
|
|
683
|
+
this.bleHandle.startScan( -1, services);
|
|
686
684
|
}, autoTime);
|
|
687
|
-
this.bleHandle.startScan(
|
|
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(
|
|
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,
|
package/src/wifiConfig/ble.ts
CHANGED
|
@@ -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
|
-
|
|
242
|
-
|
|
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
|
-
|
|
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
|
-
(
|
|
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,10 +306,7 @@ class BLEConfig extends ConfigBase {
|
|
|
291
306
|
|
|
292
307
|
enableAndGetBluetoothDevices = async (
|
|
293
308
|
softAPSSIDPrefix?: string
|
|
294
|
-
):
|
|
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;
|
|
@@ -304,7 +316,7 @@ class BLEConfig extends ConfigBase {
|
|
|
304
316
|
// const bleDevices: WechatMiniprogram.BlueToothDevice[] = (
|
|
305
317
|
// await getBluetoothDevices()
|
|
306
318
|
// ).filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));
|
|
307
|
-
const bleDevices:
|
|
319
|
+
const bleDevices: IDevice[] = this.bleHandle.scanList.filter(d => this.isValidBleDevice(d, softAPSSIDPrefix))
|
|
308
320
|
GizLog.debug('GIZ_SDK: getBluetoothDevices success', bleDevices);
|
|
309
321
|
return {
|
|
310
322
|
success: true,
|
|
@@ -395,27 +407,27 @@ class BLEConfig extends ConfigBase {
|
|
|
395
407
|
return rej(enableAndGetRes);
|
|
396
408
|
}
|
|
397
409
|
|
|
398
|
-
let
|
|
410
|
+
let bleDevices: IDevice[] = enableAndGetRes.bleDevices as IDevice[];
|
|
399
411
|
|
|
400
412
|
GizLog.debug('GIZ_SDK: enableAndGetBluetoothDevices success, target devices: ', bleDevices);
|
|
401
413
|
|
|
402
|
-
const handleFoundDevices = async ({
|
|
403
|
-
|
|
404
|
-
}: {
|
|
405
|
-
|
|
406
|
-
}) => {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
wx.onBluetoothDeviceFound(handleFoundDevices);
|
|
418
|
-
await startBluetoothDevicesDiscovery();
|
|
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);
|
|
419
431
|
|
|
420
432
|
const appendPassword = this.appendExtendData(password)
|
|
421
433
|
const uint8Array = WifiConfig.pack(ssid,appendPassword);
|
|
@@ -424,11 +436,11 @@ class BLEConfig extends ConfigBase {
|
|
|
424
436
|
if (!this.hasTimeoutHandler()) {
|
|
425
437
|
return;
|
|
426
438
|
}
|
|
427
|
-
const bleDevice = bleDevices.shift();
|
|
439
|
+
const bleDevice = bleDevices.shift() as IDevice;
|
|
428
440
|
if (bleDevice) {
|
|
429
441
|
this.eventCallBack?.({
|
|
430
442
|
event: "GIZ_DISCOVER_SUCCESS",
|
|
431
|
-
deviceID: bleDevice.
|
|
443
|
+
deviceID: bleDevice.bleDeviceID
|
|
432
444
|
})
|
|
433
445
|
GizLog.debug('GIZ_SDK: startConfigDevice, target device: ', bleDevice);
|
|
434
446
|
}
|
|
@@ -436,7 +448,7 @@ class BLEConfig extends ConfigBase {
|
|
|
436
448
|
const success =
|
|
437
449
|
bleDevice &&
|
|
438
450
|
(await this.sendBLEConfigCmd({
|
|
439
|
-
bleDeviceId: bleDevice.
|
|
451
|
+
bleDeviceId: bleDevice.bleDeviceID,
|
|
440
452
|
arrayBuffer: uint8Array.buffer,
|
|
441
453
|
bleHandle: this.bleHandle,
|
|
442
454
|
eventCallBack: this.eventCallBack
|
|
@@ -445,15 +457,14 @@ class BLEConfig extends ConfigBase {
|
|
|
445
457
|
if (!success) {
|
|
446
458
|
// 如果校验设备或者发送不成功,重试下一个设备
|
|
447
459
|
await sleep(500);
|
|
448
|
-
|
|
449
|
-
// await startBluetoothDevicesDiscovery();
|
|
460
|
+
bleDevices = this.bleHandle.scanList.filter((d) => this.isValidBleDevice(d, softAPSSIDPrefix));;
|
|
450
461
|
await startConfigDevice();
|
|
451
462
|
return;
|
|
452
463
|
}
|
|
453
464
|
|
|
454
465
|
// 如果有一个设备发送成功,则不再发现新设备
|
|
455
466
|
GizLog.debug("GIZ_SDK: offBluetoothDeviceFound ready search device")
|
|
456
|
-
wx.offBluetoothDeviceFound(() => {});
|
|
467
|
+
// wx.offBluetoothDeviceFound(() => {});
|
|
457
468
|
};
|
|
458
469
|
|
|
459
470
|
// 开始大循环搜索
|