@onekeyfe/hd-transport-react-native 1.2.8 → 1.2.10-alpha.1
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/BleManager.d.ts +2 -4
- package/dist/BleManager.d.ts.map +1 -1
- package/dist/bleNativeDisconnect.d.ts.map +1 -1
- package/dist/index.d.ts +44 -64
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +449 -615
- package/dist/subscribeBleOn.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/BleManager.ts +8 -32
- package/src/__tests__/bleNativeDisconnect.test.ts +22 -1
- package/src/__tests__/connectTimeout.test.ts +110 -0
- package/src/__tests__/enumerate.test.ts +207 -17
- package/src/__tests__/moduleInterop.test.ts +48 -0
- package/src/__tests__/nativeFrameWrite.test.ts +910 -0
- package/src/__tests__/protocolV2Link.test.ts +284 -81
- package/src/__tests__/subscribeBleOn.test.ts +114 -0
- package/src/bleNativeDisconnect.ts +2 -1
- package/src/index.ts +535 -350
- package/src/subscribeBleOn.ts +30 -11
- package/dist/bleEncryption.d.ts +0 -17
- package/dist/bleEncryption.d.ts.map +0 -1
- package/dist/bleIosStaleBond.d.ts +0 -16
- package/dist/bleIosStaleBond.d.ts.map +0 -1
- package/dist/bleKeyMissing.d.ts +0 -7
- package/dist/bleKeyMissing.d.ts.map +0 -1
- package/dist/bleStaleBond.d.ts +0 -5
- package/dist/bleStaleBond.d.ts.map +0 -1
- package/src/__tests__/androidEncryption.test.ts +0 -482
- package/src/__tests__/androidKeyMissing.test.ts +0 -307
- package/src/__tests__/bleStaleBond.test.ts +0 -47
- package/src/__tests__/iosPeerTermination.test.ts +0 -318
- package/src/bleEncryption.ts +0 -267
- package/src/bleIosStaleBond.ts +0 -92
- package/src/bleKeyMissing.ts +0 -106
- package/src/bleStaleBond.ts +0 -64
|
@@ -12,6 +12,7 @@ import ReactNativeBleTransport, {
|
|
|
12
12
|
BLE_WRITE_PACKET_TIMEOUT_MS,
|
|
13
13
|
configureProtocolV2BleTuning,
|
|
14
14
|
getFirmwareUploadWriteRetryType,
|
|
15
|
+
getProtocolV2BleTuning,
|
|
15
16
|
resetProtocolV2BleTuning,
|
|
16
17
|
} from '../index';
|
|
17
18
|
|
|
@@ -45,21 +46,6 @@ jest.mock('../BleManager', () => ({
|
|
|
45
46
|
pairDevice: jest.fn(() => Promise.resolve({ bonded: true, bonding: false })),
|
|
46
47
|
}));
|
|
47
48
|
|
|
48
|
-
// The native facade is replaced wholesale above; key missing and link encryption are covered
|
|
49
|
-
// by their own suites.
|
|
50
|
-
jest.mock('../bleKeyMissing', () => ({
|
|
51
|
-
isBleKeyMissingSupported: jest.fn(() => false),
|
|
52
|
-
startBleKeyMissingTracking: jest.fn(() => false),
|
|
53
|
-
stopBleKeyMissingTracking: jest.fn(),
|
|
54
|
-
waitForBleKeyMissing: jest.fn(() => Promise.resolve(false)),
|
|
55
|
-
}));
|
|
56
|
-
jest.mock('../bleEncryption', () => ({
|
|
57
|
-
markBleLinkEncrypted: jest.fn(),
|
|
58
|
-
startBleEncryptionTracking: jest.fn(() => false),
|
|
59
|
-
stopBleEncryptionTracking: jest.fn(),
|
|
60
|
-
waitForAndroidLinkEncryption: jest.fn(() => Promise.resolve('unresolved')),
|
|
61
|
-
}));
|
|
62
|
-
|
|
63
49
|
jest.mock('../subscribeBleOn', () => ({
|
|
64
50
|
subscribeBleOn: jest.fn(() => Promise.resolve()),
|
|
65
51
|
}));
|
|
@@ -221,6 +207,7 @@ const createHarness = ({
|
|
|
221
207
|
bleManager,
|
|
222
208
|
sentSeqs,
|
|
223
209
|
writeCharacteristic,
|
|
210
|
+
notifyCharacteristic,
|
|
224
211
|
setShouldRespond(value: boolean) {
|
|
225
212
|
shouldRespond = value;
|
|
226
213
|
},
|
|
@@ -318,6 +305,45 @@ const createV1Harness = ({
|
|
|
318
305
|
};
|
|
319
306
|
|
|
320
307
|
describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
308
|
+
test.each(['ios', 'android'] as const)(
|
|
309
|
+
'identifies MTU logs for concurrent devices and reconnects on %s',
|
|
310
|
+
async platform => {
|
|
311
|
+
setPlatformOS(platform);
|
|
312
|
+
const { transport, uuid, device, bleManager, logger } = createHarness();
|
|
313
|
+
const otherUuid = 'rn-pro2-other-id';
|
|
314
|
+
const otherDevice = { ...device, id: otherUuid };
|
|
315
|
+
|
|
316
|
+
try {
|
|
317
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
318
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
319
|
+
bleManager.devices.mockResolvedValue([otherDevice]);
|
|
320
|
+
await transport.acquire({ uuid: otherUuid, expectedProtocol: 'V2' });
|
|
321
|
+
await transport.release(uuid, true);
|
|
322
|
+
bleManager.devices.mockResolvedValue([device]);
|
|
323
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
324
|
+
|
|
325
|
+
const mtuLogs = logger.debug.mock.calls
|
|
326
|
+
.filter(([message]) => message === '[ReactNativeBleTransport] BLE MTU ready')
|
|
327
|
+
.map(([, params]) => params as { connectionId: number; actual: number });
|
|
328
|
+
expect(mtuLogs).toHaveLength(3);
|
|
329
|
+
expect(new Set(mtuLogs.map(({ connectionId }) => connectionId)).size).toBe(3);
|
|
330
|
+
for (const params of mtuLogs) {
|
|
331
|
+
expect(params).toEqual({
|
|
332
|
+
platform,
|
|
333
|
+
requested: expect.any(Number),
|
|
334
|
+
actual: device.mtu,
|
|
335
|
+
connectionId: expect.any(Number),
|
|
336
|
+
});
|
|
337
|
+
expect(Number.isSafeInteger(params.connectionId)).toBe(true);
|
|
338
|
+
expect(params.connectionId).toBeGreaterThan(0);
|
|
339
|
+
}
|
|
340
|
+
} finally {
|
|
341
|
+
await transport.release(uuid, true);
|
|
342
|
+
await transport.release(otherUuid, true);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
);
|
|
346
|
+
|
|
321
347
|
test('does not classify disconnects as retryable firmware writes', () => {
|
|
322
348
|
expect(
|
|
323
349
|
getFirmwareUploadWriteRetryType({
|
|
@@ -378,9 +404,16 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
378
404
|
expect(new ReactNativeBleTransport({}).scanTimeout).toBe(3000);
|
|
379
405
|
});
|
|
380
406
|
|
|
381
|
-
test.each([
|
|
382
|
-
|
|
383
|
-
|
|
407
|
+
test.each([
|
|
408
|
+
{ protocol: 'V1', initiated: true },
|
|
409
|
+
{ protocol: 'V1', initiated: false },
|
|
410
|
+
{ protocol: 'V1', initiated: undefined },
|
|
411
|
+
{ protocol: 'V2', initiated: true },
|
|
412
|
+
{ protocol: 'V2', initiated: false },
|
|
413
|
+
{ protocol: 'V2', initiated: undefined },
|
|
414
|
+
] as const)(
|
|
415
|
+
'waits for Android bonding before connecting $protocol GATT, initiated=$initiated',
|
|
416
|
+
async ({ protocol, initiated }) => {
|
|
384
417
|
setPlatformOS('android');
|
|
385
418
|
const { transport, uuid, device } = protocol === 'V1' ? createV1Harness() : createHarness();
|
|
386
419
|
const operationOrder: string[] = [];
|
|
@@ -395,7 +428,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
395
428
|
});
|
|
396
429
|
pairDeviceMock.mockImplementationOnce(() => {
|
|
397
430
|
operationOrder.push('bond');
|
|
398
|
-
return Promise.resolve({ bonded: false, bonding: true });
|
|
431
|
+
return Promise.resolve({ bonded: false, bonding: true, initiated });
|
|
399
432
|
});
|
|
400
433
|
bondStateMock.mockImplementationOnce(() => bonding.promise);
|
|
401
434
|
|
|
@@ -405,6 +438,9 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
405
438
|
});
|
|
406
439
|
expect(operationOrder).toEqual(['bond']);
|
|
407
440
|
expect(device.connect).not.toHaveBeenCalled();
|
|
441
|
+
expect(bondStateMock).toHaveBeenLastCalledWith(uuid, expect.any(AbortSignal), {
|
|
442
|
+
systemInitiated: initiated === false,
|
|
443
|
+
});
|
|
408
444
|
|
|
409
445
|
bonding.resolve();
|
|
410
446
|
await expect(acquiring).resolves.toEqual({
|
|
@@ -447,6 +483,42 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
447
483
|
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
448
484
|
});
|
|
449
485
|
|
|
486
|
+
test.each([
|
|
487
|
+
['ios', 'V1'],
|
|
488
|
+
['ios', 'V2'],
|
|
489
|
+
['android', 'V1'],
|
|
490
|
+
['android', 'V2'],
|
|
491
|
+
] as const)(
|
|
492
|
+
'uses connection or disconnect errors instead of a legacy bond error for ATT 22 on %s %s',
|
|
493
|
+
async (platform, expectedProtocol) => {
|
|
494
|
+
setPlatformOS(platform);
|
|
495
|
+
const BleErrorMock = jest.requireMock('react-native-ble-plx').BleError as new (
|
|
496
|
+
message: string
|
|
497
|
+
) => Error;
|
|
498
|
+
|
|
499
|
+
for (const [nativeCode, expectedCode] of [
|
|
500
|
+
[205, HardwareErrorCode.BleConnectedError],
|
|
501
|
+
[201, HardwareErrorCode.BleDeviceDisconnected],
|
|
502
|
+
]) {
|
|
503
|
+
const { transport, uuid, device } = createHarness();
|
|
504
|
+
const nativeError = Object.assign(new BleErrorMock('GATT connect failed'), {
|
|
505
|
+
errorCode: nativeCode,
|
|
506
|
+
attErrorCode: 22,
|
|
507
|
+
reason: 'GATT connect failed',
|
|
508
|
+
});
|
|
509
|
+
device.isConnected.mockResolvedValueOnce(false);
|
|
510
|
+
device.connect.mockRejectedValueOnce(nativeError);
|
|
511
|
+
|
|
512
|
+
await expect(transport.acquire({ uuid, expectedProtocol })).rejects.toMatchObject({
|
|
513
|
+
errorCode: expectedCode,
|
|
514
|
+
message: expect.stringContaining('GATT connect failed'),
|
|
515
|
+
});
|
|
516
|
+
expect(device.connect).toHaveBeenCalledTimes(1);
|
|
517
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
);
|
|
521
|
+
|
|
450
522
|
test('does not connect Android GATT when the system cannot start bonding', async () => {
|
|
451
523
|
setPlatformOS('android');
|
|
452
524
|
const { transport, uuid, device, bleManager } = createHarness();
|
|
@@ -478,27 +550,11 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
478
550
|
});
|
|
479
551
|
|
|
480
552
|
test.each([
|
|
481
|
-
[
|
|
482
|
-
|
|
483
|
-
'OneKey Neo',
|
|
484
|
-
{
|
|
485
|
-
iosErrorCode: 14,
|
|
486
|
-
reason: 'Peer removed pairing information',
|
|
487
|
-
},
|
|
488
|
-
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
489
|
-
],
|
|
490
|
-
[
|
|
491
|
-
'android',
|
|
492
|
-
'OneKey Pro 2',
|
|
493
|
-
{
|
|
494
|
-
androidErrorCode: 5,
|
|
495
|
-
reason: 'Connection state changed with status 5',
|
|
496
|
-
},
|
|
497
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
498
|
-
],
|
|
553
|
+
['ios', 'OneKey Neo', { iosErrorCode: 14, reason: 'Native connection failed' }],
|
|
554
|
+
['android', 'OneKey Pro 2', { androidErrorCode: 5, reason: 'Native connection failed' }],
|
|
499
555
|
] as const)(
|
|
500
|
-
'
|
|
501
|
-
async (platform, deviceName, nativeError
|
|
556
|
+
'keeps a %s %s native connect failure generic before protocol detection',
|
|
557
|
+
async (platform, deviceName, nativeError) => {
|
|
502
558
|
setPlatformOS(platform);
|
|
503
559
|
const { transport, uuid, device } = createHarness({ deviceName });
|
|
504
560
|
const BleErrorMock = jest.requireMock('react-native-ble-plx').BleError as new (
|
|
@@ -512,7 +568,9 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
512
568
|
.fn()
|
|
513
569
|
.mockRejectedValue(Object.assign(new BleErrorMock(nativeError.reason), nativeError));
|
|
514
570
|
|
|
515
|
-
await expect(transport.acquire({ uuid })).rejects.toMatchObject({
|
|
571
|
+
await expect(transport.acquire({ uuid })).rejects.toMatchObject({
|
|
572
|
+
errorCode: HardwareErrorCode.BleConnectedError,
|
|
573
|
+
});
|
|
516
574
|
expect(pairDeviceMock).toHaveBeenCalledTimes(platform === 'android' ? 1 : 0);
|
|
517
575
|
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
518
576
|
}
|
|
@@ -608,7 +666,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
608
666
|
await transport.release(uuid, true);
|
|
609
667
|
});
|
|
610
668
|
|
|
611
|
-
test('
|
|
669
|
+
test('maps native Protocol V1 write failures to the generic write error', async () => {
|
|
612
670
|
const { transport, uuid, writeCharacteristic } = createV1Harness();
|
|
613
671
|
const nativeError = Object.assign(new Error('Encryption is insufficient'), {
|
|
614
672
|
attErrorCode: 15,
|
|
@@ -765,7 +823,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
765
823
|
|
|
766
824
|
const acquiring = transport.acquire({ uuid });
|
|
767
825
|
const failure = expect(acquiring).rejects.toMatchObject({
|
|
768
|
-
errorCode: HardwareErrorCode.
|
|
826
|
+
errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
|
|
769
827
|
});
|
|
770
828
|
const reconnecting = transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
771
829
|
|
|
@@ -804,7 +862,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
804
862
|
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
805
863
|
|
|
806
864
|
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
807
|
-
errorCode: HardwareErrorCode.
|
|
865
|
+
errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
|
|
866
|
+
params: { expectedProtocol: 'V2' },
|
|
808
867
|
});
|
|
809
868
|
|
|
810
869
|
expect(device.cancelConnection).toHaveBeenCalledTimes(1);
|
|
@@ -820,10 +879,12 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
820
879
|
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
821
880
|
|
|
822
881
|
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
823
|
-
errorCode: HardwareErrorCode.
|
|
882
|
+
errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
|
|
883
|
+
params: { expectedProtocol: 'V2' },
|
|
824
884
|
});
|
|
825
885
|
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
826
|
-
errorCode: HardwareErrorCode.
|
|
886
|
+
errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
|
|
887
|
+
params: { expectedProtocol: 'V2' },
|
|
827
888
|
});
|
|
828
889
|
|
|
829
890
|
expect(device.cancelConnection).toHaveBeenCalledTimes(2);
|
|
@@ -831,35 +892,6 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
831
892
|
}
|
|
832
893
|
);
|
|
833
894
|
|
|
834
|
-
test.each([
|
|
835
|
-
[
|
|
836
|
-
'Encryption is insufficient',
|
|
837
|
-
{ reason: 'Encryption is insufficient', attErrorCode: 15 },
|
|
838
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
839
|
-
],
|
|
840
|
-
[
|
|
841
|
-
'Peer removed pairing information',
|
|
842
|
-
{ reason: 'Peer removed pairing information', iosErrorCode: 14 },
|
|
843
|
-
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
844
|
-
],
|
|
845
|
-
] as const)(
|
|
846
|
-
'fails Protocol V2 acquire immediately on %s instead of waiting for Ping',
|
|
847
|
-
async (_label, nativeError, errorCode) => {
|
|
848
|
-
const { transport, uuid, device } = createHarness({
|
|
849
|
-
monitorError: Object.assign(new Error(nativeError.reason), nativeError),
|
|
850
|
-
});
|
|
851
|
-
const probe = jest.spyOn(transport as any, 'probeProtocolV2');
|
|
852
|
-
|
|
853
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
854
|
-
errorCode,
|
|
855
|
-
});
|
|
856
|
-
|
|
857
|
-
expect(probe).not.toHaveBeenCalled();
|
|
858
|
-
expect(device.cancelConnection).toHaveBeenCalled();
|
|
859
|
-
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
860
|
-
}
|
|
861
|
-
);
|
|
862
|
-
|
|
863
895
|
test.each(['ios', 'android'] as const)(
|
|
864
896
|
'disconnects after a confirmed Protocol V2 probe miss on %s without reporting a bond error',
|
|
865
897
|
async platform => {
|
|
@@ -870,7 +902,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
870
902
|
jest.spyOn(transport as any, 'probeProtocolV2').mockResolvedValue(false);
|
|
871
903
|
|
|
872
904
|
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
873
|
-
errorCode: HardwareErrorCode.
|
|
905
|
+
errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
|
|
906
|
+
params: { expectedProtocol: 'V2' },
|
|
874
907
|
});
|
|
875
908
|
|
|
876
909
|
expect(device.cancelConnection).toHaveBeenCalledTimes(1);
|
|
@@ -1029,7 +1062,6 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1029
1062
|
for (const errorCode of [
|
|
1030
1063
|
HardwareErrorCode.BleDeviceNotBonded,
|
|
1031
1064
|
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
1032
|
-
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1033
1065
|
HardwareErrorCode.BleDeviceDisconnected,
|
|
1034
1066
|
HardwareErrorCode.BleCharacteristicNotifyError,
|
|
1035
1067
|
HardwareErrorCode.BleCharacteristicNotifyChangeFailure,
|
|
@@ -1210,20 +1242,20 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1210
1242
|
await transport.release(uuid, true);
|
|
1211
1243
|
}, 10_000);
|
|
1212
1244
|
|
|
1213
|
-
test('
|
|
1245
|
+
test('keeps an iOS MTU-timeout reconnect failure generic', async () => {
|
|
1214
1246
|
const { BleError: BleErrorMock } = jest.requireMock('react-native-ble-plx');
|
|
1215
1247
|
const { transport, uuid, device } = createHarness();
|
|
1216
1248
|
device.mtu = 23;
|
|
1217
1249
|
device.requestMTU.mockImplementationOnce(() => new Promise(() => {}));
|
|
1218
1250
|
device.connect.mockRejectedValueOnce(
|
|
1219
|
-
Object.assign(new BleErrorMock('
|
|
1251
|
+
Object.assign(new BleErrorMock('Native connection failed'), {
|
|
1220
1252
|
errorCode: 200,
|
|
1221
1253
|
iosErrorCode: 14,
|
|
1222
1254
|
})
|
|
1223
1255
|
);
|
|
1224
1256
|
|
|
1225
1257
|
await expect(transport.acquire({ uuid })).rejects.toMatchObject({
|
|
1226
|
-
errorCode: HardwareErrorCode.
|
|
1258
|
+
errorCode: HardwareErrorCode.BleConnectedError,
|
|
1227
1259
|
});
|
|
1228
1260
|
await transport.release(uuid, true).catch(() => undefined);
|
|
1229
1261
|
}, 10_000);
|
|
@@ -1706,7 +1738,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1706
1738
|
|
|
1707
1739
|
await expect(transport.acquire({ uuid })).rejects.toBeDefined();
|
|
1708
1740
|
await expect(transport.acquire({ uuid })).rejects.toMatchObject({
|
|
1709
|
-
errorCode: HardwareErrorCode.
|
|
1741
|
+
errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
|
|
1710
1742
|
});
|
|
1711
1743
|
await transport.disconnect(uuid);
|
|
1712
1744
|
await transport.stop();
|
|
@@ -1814,7 +1846,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1814
1846
|
jest.spyOn(transport as any, 'probeProtocolV1').mockResolvedValue(false);
|
|
1815
1847
|
|
|
1816
1848
|
await expect(transport.acquire({ uuid, protocolHint: 'V2' })).rejects.toMatchObject({
|
|
1817
|
-
errorCode: HardwareErrorCode.
|
|
1849
|
+
errorCode: HardwareErrorCode.TransportProtocolProbeFailed,
|
|
1818
1850
|
});
|
|
1819
1851
|
|
|
1820
1852
|
expect(device.onDisconnected).not.toHaveBeenCalled();
|
|
@@ -1855,6 +1887,80 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1855
1887
|
await transport.release(uuid, true);
|
|
1856
1888
|
});
|
|
1857
1889
|
|
|
1890
|
+
test.each(['ios', 'android'] as const)(
|
|
1891
|
+
'reuses the connection, monitor and V2 sequence after a kept-alive release on %s',
|
|
1892
|
+
async platform => {
|
|
1893
|
+
setPlatformOS(platform);
|
|
1894
|
+
const { transport, uuid, device, bleManager, notifyCharacteristic, sentSeqs } =
|
|
1895
|
+
createHarness();
|
|
1896
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
1897
|
+
await transport.call(uuid, 'Ping', { message: 'before-release' });
|
|
1898
|
+
const connectCount = device.connect.mock.calls.length;
|
|
1899
|
+
const cancelTransactionCount = bleManager.cancelTransaction.mock.calls.length;
|
|
1900
|
+
const subscription = notifyCharacteristic.monitor.mock.results[0].value;
|
|
1901
|
+
const invalidateLink = jest.spyOn((transport as any).protocolV2Links, 'invalidateLink');
|
|
1902
|
+
|
|
1903
|
+
try {
|
|
1904
|
+
await transport.release(uuid, false, true);
|
|
1905
|
+
expect(transport.getProtocolType(uuid)).toBe('V2');
|
|
1906
|
+
expect(subscription.remove).not.toHaveBeenCalled();
|
|
1907
|
+
expect(invalidateLink).not.toHaveBeenCalled();
|
|
1908
|
+
expect(bleManager.cancelTransaction).toHaveBeenCalledTimes(cancelTransactionCount);
|
|
1909
|
+
expect(bleManager.cancelDeviceConnection).not.toHaveBeenCalled();
|
|
1910
|
+
|
|
1911
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
1912
|
+
await expect(
|
|
1913
|
+
transport.call(uuid, 'Ping', { message: 'after-release' })
|
|
1914
|
+
).resolves.toMatchObject({
|
|
1915
|
+
type: 'Success',
|
|
1916
|
+
});
|
|
1917
|
+
expect(device.connect).toHaveBeenCalledTimes(connectCount);
|
|
1918
|
+
expect(notifyCharacteristic.monitor).toHaveBeenCalledTimes(1);
|
|
1919
|
+
expect(device.onDisconnected).toHaveBeenCalledTimes(1);
|
|
1920
|
+
expect(sentSeqs).toEqual([1, 2, 3]);
|
|
1921
|
+
} finally {
|
|
1922
|
+
await transport.disconnect(uuid);
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
);
|
|
1926
|
+
|
|
1927
|
+
test.each(['ios', 'android'] as const)(
|
|
1928
|
+
'keeps the legacy release path without retention and for V1 on %s',
|
|
1929
|
+
async platform => {
|
|
1930
|
+
setPlatformOS(platform);
|
|
1931
|
+
for (const protocol of ['V1', 'V2'] as const) {
|
|
1932
|
+
const { transport, uuid } = protocol === 'V1' ? createV1Harness() : createHarness();
|
|
1933
|
+
await transport.acquire({ uuid, expectedProtocol: protocol });
|
|
1934
|
+
const releaseNative = jest.spyOn(transport as any, 'releaseNative');
|
|
1935
|
+
await transport.release(uuid, false, protocol === 'V1');
|
|
1936
|
+
expect(releaseNative).toHaveBeenCalledWith(uuid, false);
|
|
1937
|
+
await transport.release(uuid, true);
|
|
1938
|
+
}
|
|
1939
|
+
}
|
|
1940
|
+
);
|
|
1941
|
+
|
|
1942
|
+
test.each([
|
|
1943
|
+
['ios', 'disconnect'],
|
|
1944
|
+
['android', 'disconnect'],
|
|
1945
|
+
['ios', 'stop'],
|
|
1946
|
+
['android', 'stop'],
|
|
1947
|
+
['ios', 'onclose'],
|
|
1948
|
+
['android', 'onclose'],
|
|
1949
|
+
] as const)('cleans up a retained link on %s / %s', async (platform, action) => {
|
|
1950
|
+
setPlatformOS(platform);
|
|
1951
|
+
const { transport, uuid, notifyCharacteristic } = createHarness();
|
|
1952
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
1953
|
+
await transport.release(uuid, false, true);
|
|
1954
|
+
const subscription = notifyCharacteristic.monitor.mock.results[0].value;
|
|
1955
|
+
|
|
1956
|
+
if (action === 'onclose') await transport.release(uuid, true, true);
|
|
1957
|
+
else if (action === 'stop') await transport.stop();
|
|
1958
|
+
else await transport.disconnect(uuid);
|
|
1959
|
+
|
|
1960
|
+
expect(subscription.remove).toHaveBeenCalledTimes(1);
|
|
1961
|
+
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
1962
|
+
});
|
|
1963
|
+
|
|
1858
1964
|
test('rejects the active Protocol V2 reader when the current monitor errors', async () => {
|
|
1859
1965
|
const harness = createHarness();
|
|
1860
1966
|
const { transport, uuid, sentSeqs } = harness;
|
|
@@ -1898,6 +2004,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1898
2004
|
return Promise.resolve();
|
|
1899
2005
|
});
|
|
1900
2006
|
|
|
2007
|
+
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
2008
|
+
await transport.release(uuid, false, true);
|
|
1901
2009
|
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
1902
2010
|
harness.setShouldRespond(false);
|
|
1903
2011
|
await expect(transport.call(uuid, 'Ping', {}, { timeoutMs: 10 })).rejects.toMatchObject({
|
|
@@ -2060,6 +2168,101 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
2060
2168
|
expect(device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
|
|
2061
2169
|
});
|
|
2062
2170
|
|
|
2171
|
+
describe('Android connection priority by API level', () => {
|
|
2172
|
+
const setPlatformVersion = (version: number | undefined) => {
|
|
2173
|
+
const reactNative: { Platform: { Version?: number } } = jest.requireMock('react-native');
|
|
2174
|
+
reactNative.Platform.Version = version;
|
|
2175
|
+
};
|
|
2176
|
+
|
|
2177
|
+
afterEach(() => {
|
|
2178
|
+
setPlatformVersion(undefined);
|
|
2179
|
+
});
|
|
2180
|
+
|
|
2181
|
+
const runHighVolumeCalls = async (harness: ReturnType<typeof createHarness>) => {
|
|
2182
|
+
await harness.transport.acquire({ uuid: harness.uuid, expectedProtocol: 'V2' });
|
|
2183
|
+
await harness.transport.call(harness.uuid, 'FileWrite', {});
|
|
2184
|
+
await harness.transport.call(harness.uuid, 'FileWrite', {});
|
|
2185
|
+
};
|
|
2186
|
+
|
|
2187
|
+
const createAndroidHarness = (apiLevel: number) => {
|
|
2188
|
+
setPlatformOS('android');
|
|
2189
|
+
setPlatformVersion(apiLevel);
|
|
2190
|
+
const harness = createHarness();
|
|
2191
|
+
harness.device.mtu = 517;
|
|
2192
|
+
return harness;
|
|
2193
|
+
};
|
|
2194
|
+
|
|
2195
|
+
test('leaves the priority to the peripheral on Android 17 by default', async () => {
|
|
2196
|
+
const harness = createAndroidHarness(37);
|
|
2197
|
+
|
|
2198
|
+
await runHighVolumeCalls(harness);
|
|
2199
|
+
await harness.transport.release(harness.uuid, true);
|
|
2200
|
+
|
|
2201
|
+
expect(harness.device.requestConnectionPriority).not.toHaveBeenCalled();
|
|
2202
|
+
expect(
|
|
2203
|
+
harness.logger.debug.mock.calls.filter(
|
|
2204
|
+
([message]) =>
|
|
2205
|
+
message ===
|
|
2206
|
+
'[ReactNativeBleTransport] Android BLE connection priority left to peripheral'
|
|
2207
|
+
)
|
|
2208
|
+
).toHaveLength(1);
|
|
2209
|
+
});
|
|
2210
|
+
|
|
2211
|
+
test('keeps requesting HIGH below Android 17 by default', async () => {
|
|
2212
|
+
const harness = createAndroidHarness(36);
|
|
2213
|
+
|
|
2214
|
+
await runHighVolumeCalls(harness);
|
|
2215
|
+
expect(harness.device.requestConnectionPriority).toHaveBeenCalledTimes(1);
|
|
2216
|
+
expect(harness.device.requestConnectionPriority).toHaveBeenCalledWith(1);
|
|
2217
|
+
|
|
2218
|
+
await harness.transport.release(harness.uuid, true);
|
|
2219
|
+
expect(harness.device.requestConnectionPriority).toHaveBeenLastCalledWith(0);
|
|
2220
|
+
});
|
|
2221
|
+
|
|
2222
|
+
test.each([
|
|
2223
|
+
[37, true],
|
|
2224
|
+
[36, false],
|
|
2225
|
+
] as const)('applies the tuning switch on API %s when set to %s', async (apiLevel, setting) => {
|
|
2226
|
+
configureProtocolV2BleTuning({ androidHighConnectionPriority: setting });
|
|
2227
|
+
const harness = createAndroidHarness(apiLevel);
|
|
2228
|
+
|
|
2229
|
+
await runHighVolumeCalls(harness);
|
|
2230
|
+
|
|
2231
|
+
if (setting) {
|
|
2232
|
+
expect(harness.device.requestConnectionPriority).toHaveBeenCalledWith(1);
|
|
2233
|
+
} else {
|
|
2234
|
+
expect(harness.device.requestConnectionPriority).not.toHaveBeenCalled();
|
|
2235
|
+
}
|
|
2236
|
+
await harness.transport.release(harness.uuid, true);
|
|
2237
|
+
});
|
|
2238
|
+
|
|
2239
|
+
test('keeps the switch across unrelated tuning and ignores invalid values', () => {
|
|
2240
|
+
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe('auto');
|
|
2241
|
+
configureProtocolV2BleTuning({ androidHighConnectionPriority: false });
|
|
2242
|
+
configureProtocolV2BleTuning({ androidPacketLength: 200 });
|
|
2243
|
+
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe(false);
|
|
2244
|
+
configureProtocolV2BleTuning({ androidHighConnectionPriority: 'on' as never });
|
|
2245
|
+
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe(false);
|
|
2246
|
+
configureProtocolV2BleTuning({ androidHighConnectionPriority: 'auto' });
|
|
2247
|
+
expect(getProtocolV2BleTuning().androidHighConnectionPriority).toBe('auto');
|
|
2248
|
+
});
|
|
2249
|
+
|
|
2250
|
+
test('requests HIGH again on the next link after a manual disconnect', async () => {
|
|
2251
|
+
const harness = createAndroidHarness(36);
|
|
2252
|
+
|
|
2253
|
+
await runHighVolumeCalls(harness);
|
|
2254
|
+
expect(harness.device.requestConnectionPriority).toHaveBeenCalledTimes(1);
|
|
2255
|
+
|
|
2256
|
+
await harness.transport.disconnect(harness.uuid);
|
|
2257
|
+
await runHighVolumeCalls(harness);
|
|
2258
|
+
|
|
2259
|
+
expect(
|
|
2260
|
+
harness.device.requestConnectionPriority.mock.calls.filter(([priority]) => priority === 1)
|
|
2261
|
+
).toHaveLength(2);
|
|
2262
|
+
await harness.transport.release(harness.uuid, true);
|
|
2263
|
+
});
|
|
2264
|
+
});
|
|
2265
|
+
|
|
2063
2266
|
test('uses withResponse for an iOS Protocol V2 firmware file write when requested', async () => {
|
|
2064
2267
|
const { transport, uuid, writeCharacteristic } = createHarness();
|
|
2065
2268
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
+
|
|
3
|
+
import { subscribeBleOn } from '../subscribeBleOn';
|
|
4
|
+
|
|
5
|
+
import type { BlePlxManager } from '../types';
|
|
6
|
+
|
|
7
|
+
type BleState = Parameters<Parameters<BlePlxManager['onStateChange']>[0]>[0];
|
|
8
|
+
|
|
9
|
+
const createBleManager = () => {
|
|
10
|
+
let stateListener: ((state: BleState) => void) | undefined;
|
|
11
|
+
const remove = jest.fn();
|
|
12
|
+
const bleManager = {
|
|
13
|
+
onStateChange: jest.fn(listener => {
|
|
14
|
+
stateListener = listener;
|
|
15
|
+
return { remove };
|
|
16
|
+
}),
|
|
17
|
+
} as unknown as BlePlxManager;
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
bleManager,
|
|
21
|
+
emitState: (state: BleState) => stateListener?.(state),
|
|
22
|
+
remove,
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe('subscribeBleOn', () => {
|
|
27
|
+
beforeAll(() => {
|
|
28
|
+
jest.useFakeTimers({ doNotFake: ['performance'] });
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
afterAll(() => {
|
|
32
|
+
jest.useRealTimers();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
jest.clearAllTimers();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('resolves when Bluetooth is powered on', async () => {
|
|
40
|
+
const { bleManager, emitState, remove } = createBleManager();
|
|
41
|
+
const result = subscribeBleOn(bleManager);
|
|
42
|
+
|
|
43
|
+
emitState('PoweredOn');
|
|
44
|
+
|
|
45
|
+
await expect(result).resolves.toBeUndefined();
|
|
46
|
+
expect(remove).toHaveBeenCalledTimes(1);
|
|
47
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('cleans up when the current state is emitted synchronously', async () => {
|
|
51
|
+
const remove = jest.fn();
|
|
52
|
+
const bleManager = {
|
|
53
|
+
onStateChange: jest.fn(listener => {
|
|
54
|
+
listener('PoweredOn');
|
|
55
|
+
return { remove };
|
|
56
|
+
}),
|
|
57
|
+
} as unknown as BlePlxManager;
|
|
58
|
+
|
|
59
|
+
await expect(subscribeBleOn(bleManager)).resolves.toBeUndefined();
|
|
60
|
+
expect(remove).toHaveBeenCalledTimes(1);
|
|
61
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test.each([
|
|
65
|
+
['PoweredOff', HardwareErrorCode.BlePoweredOff],
|
|
66
|
+
['Unsupported', HardwareErrorCode.BleUnsupported],
|
|
67
|
+
['Unauthorized', HardwareErrorCode.BleLocationError],
|
|
68
|
+
] as const)('maps %s to hardware error %s', async (state, errorCode) => {
|
|
69
|
+
const { bleManager, emitState, remove } = createBleManager();
|
|
70
|
+
const result = subscribeBleOn(bleManager, 2000);
|
|
71
|
+
const rejection = expect(result).rejects.toMatchObject({ errorCode });
|
|
72
|
+
|
|
73
|
+
emitState(state);
|
|
74
|
+
jest.advanceTimersByTime(1999);
|
|
75
|
+
expect(remove).not.toHaveBeenCalled();
|
|
76
|
+
jest.advanceTimersByTime(1);
|
|
77
|
+
|
|
78
|
+
await rejection;
|
|
79
|
+
expect(remove).toHaveBeenCalledTimes(1);
|
|
80
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('resolves if Bluetooth powers on during the readiness window', async () => {
|
|
84
|
+
const { bleManager, emitState, remove } = createBleManager();
|
|
85
|
+
const result = subscribeBleOn(bleManager);
|
|
86
|
+
|
|
87
|
+
emitState('PoweredOff');
|
|
88
|
+
jest.advanceTimersByTime(1999);
|
|
89
|
+
expect(remove).not.toHaveBeenCalled();
|
|
90
|
+
emitState('PoweredOn');
|
|
91
|
+
|
|
92
|
+
await expect(result).resolves.toBeUndefined();
|
|
93
|
+
expect(remove).toHaveBeenCalledTimes(1);
|
|
94
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test.each(['Unknown', 'Resetting'] as const)(
|
|
98
|
+
'reports %s as a scan error if no stable state arrives before timeout',
|
|
99
|
+
async state => {
|
|
100
|
+
const { bleManager, emitState, remove } = createBleManager();
|
|
101
|
+
const result = subscribeBleOn(bleManager, 1000);
|
|
102
|
+
const rejection = expect(result).rejects.toMatchObject({
|
|
103
|
+
errorCode: HardwareErrorCode.BleScanError,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
emitState('PoweredOff');
|
|
107
|
+
emitState(state);
|
|
108
|
+
jest.advanceTimersByTime(1000);
|
|
109
|
+
|
|
110
|
+
await rejection;
|
|
111
|
+
expect(remove).toHaveBeenCalledTimes(1);
|
|
112
|
+
}
|
|
113
|
+
);
|
|
114
|
+
});
|
|
@@ -23,7 +23,8 @@ export const isNativeBleDisconnectError = (error: unknown): boolean => {
|
|
|
23
23
|
const nativeError = error as NativeBleErrorFields;
|
|
24
24
|
return (
|
|
25
25
|
nativeError.errorCode === BLE_PLX_DEVICE_DISCONNECTED ||
|
|
26
|
-
nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED
|
|
26
|
+
nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED ||
|
|
27
|
+
nativeErrorText(nativeError).toLowerCase().includes('was disconnected')
|
|
27
28
|
);
|
|
28
29
|
};
|
|
29
30
|
|