@onekeyfe/hd-transport-react-native 1.2.11-alpha.4 → 1.2.11-alpha.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.d.ts +5 -55
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +275 -467
- package/package.json +5 -5
- package/src/__tests__/connectTimeout.test.ts +0 -44
- package/src/__tests__/nativeFrameWrite.test.ts +0 -1
- package/src/__tests__/protocolV2Link.test.ts +3 -486
- package/src/index.ts +341 -632
- package/dist/bleIosBond.d.ts +0 -3
- package/dist/bleIosBond.d.ts.map +0 -1
- package/src/__tests__/bleIosBond.test.ts +0 -35
- package/src/__tests__/iosPresentDeviceConnect.test.ts +0 -228
- package/src/__tests__/releaseBondConcurrency.test.ts +0 -284
- package/src/bleIosBond.ts +0 -39
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "1.2.11-alpha.
|
|
3
|
+
"version": "1.2.11-alpha.6",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-core": "1.2.11-alpha.
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.11-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.11-alpha.
|
|
23
|
+
"@onekeyfe/hd-core": "1.2.11-alpha.6",
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.11-alpha.6",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.11-alpha.6",
|
|
26
26
|
"@onekeyfe/react-native-ble-utils": "0.1.9",
|
|
27
27
|
"react-native-ble-plx": "3.5.1"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "fe0e726cdb7de7d9889e26af1db227d9bed32369"
|
|
30
30
|
}
|
|
@@ -528,50 +528,6 @@ describe('BLE connect timeout', () => {
|
|
|
528
528
|
expect(BLE_CONNECT_TIMEOUT_MS).toBeLessThanOrEqual(12000);
|
|
529
529
|
});
|
|
530
530
|
|
|
531
|
-
test.each(
|
|
532
|
-
(['ios', 'android'] as const).flatMap(platform =>
|
|
533
|
-
(['manager', 'device'] as const).flatMap(source =>
|
|
534
|
-
(['V1', 'V2'] as const).map(expectedProtocol => ({ platform, source, expectedProtocol }))
|
|
535
|
-
)
|
|
536
|
-
)
|
|
537
|
-
)(
|
|
538
|
-
'cancel drains a late $platform $source connect before rejecting $expectedProtocol acquire',
|
|
539
|
-
async ({ platform, source, expectedProtocol }) => {
|
|
540
|
-
Object.assign(Platform, { OS: platform });
|
|
541
|
-
const { transport, device, bleManager, connect } = createHarness(() => nativeConnect.promise);
|
|
542
|
-
const nativeConnect = createDeferred<typeof device>();
|
|
543
|
-
const nativeDisconnect = createDeferred<void>();
|
|
544
|
-
if (source === 'manager') bleManager.devices.mockResolvedValue([]);
|
|
545
|
-
device.isConnected.mockResolvedValue(true);
|
|
546
|
-
if (source === 'device') device.isConnected.mockResolvedValueOnce(false);
|
|
547
|
-
bleManager.cancelDeviceConnection.mockImplementation(() => nativeDisconnect.promise);
|
|
548
|
-
let settled = false;
|
|
549
|
-
const acquiring = transport.acquire({ uuid: UUID, expectedProtocol }).catch(error => {
|
|
550
|
-
settled = true;
|
|
551
|
-
return error;
|
|
552
|
-
});
|
|
553
|
-
await flush();
|
|
554
|
-
await flush();
|
|
555
|
-
const pendingConnect = source === 'manager' ? bleManager.connectToDevice : connect;
|
|
556
|
-
expect(pendingConnect).toHaveBeenCalledTimes(1);
|
|
557
|
-
|
|
558
|
-
await transport.cancel(UUID);
|
|
559
|
-
nativeConnect.resolve(device);
|
|
560
|
-
await flush();
|
|
561
|
-
expect(settled).toBe(false);
|
|
562
|
-
expect(bleManager.cancelDeviceConnection).toHaveBeenCalledWith(UUID);
|
|
563
|
-
expect(device.discoverAllServicesAndCharacteristics).not.toHaveBeenCalled();
|
|
564
|
-
|
|
565
|
-
nativeDisconnect.resolve();
|
|
566
|
-
await advanceUntil(() => settled, 1000);
|
|
567
|
-
await expect(acquiring).resolves.toMatchObject({
|
|
568
|
-
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
569
|
-
});
|
|
570
|
-
expect(pendingConnect).toHaveBeenCalledTimes(1);
|
|
571
|
-
expect(jest.getTimerCount()).toBe(0);
|
|
572
|
-
}
|
|
573
|
-
);
|
|
574
|
-
|
|
575
531
|
test.each(
|
|
576
532
|
(['ios', 'android'] as const).flatMap(platform =>
|
|
577
533
|
(['connect', 'mtu', 'gatt'] as const).flatMap(stage =>
|
|
@@ -42,7 +42,6 @@ jest.mock('react-native-ble-plx', () => ({
|
|
|
42
42
|
}));
|
|
43
43
|
|
|
44
44
|
jest.mock('../BleManager', () => ({
|
|
45
|
-
getBondedDevices: jest.fn(() => Promise.resolve([])),
|
|
46
45
|
getConnectedDeviceIds: jest.fn(() => Promise.resolve([])),
|
|
47
46
|
onDeviceBondState: jest.fn(() => Promise.resolve()),
|
|
48
47
|
pairDevice: jest.fn(() => Promise.resolve({ bonded: true, bonding: false })),
|
|
@@ -41,7 +41,6 @@ jest.mock('react-native-ble-plx', () => ({
|
|
|
41
41
|
}));
|
|
42
42
|
|
|
43
43
|
jest.mock('../BleManager', () => ({
|
|
44
|
-
getBondedDevices: jest.fn(() => Promise.resolve([])),
|
|
45
44
|
getConnectedDeviceIds: jest.fn(() => Promise.resolve([])),
|
|
46
45
|
onDeviceBondState: jest.fn(() => Promise.resolve()),
|
|
47
46
|
pairDevice: jest.fn(() => Promise.resolve({ bonded: true, bonding: false })),
|
|
@@ -300,9 +299,6 @@ const createV1Harness = ({
|
|
|
300
299
|
device,
|
|
301
300
|
bleManager,
|
|
302
301
|
writeCharacteristic,
|
|
303
|
-
emitMonitorError(error: Error & { reason?: string; iosErrorCode?: number }) {
|
|
304
|
-
notifyCallback?.(error, null);
|
|
305
|
-
},
|
|
306
302
|
notifySubscriptionRemovers,
|
|
307
303
|
disconnectSubscriptionRemovers,
|
|
308
304
|
};
|
|
@@ -530,129 +526,16 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
530
526
|
|
|
531
527
|
device.connect = jest.fn().mockResolvedValue(device);
|
|
532
528
|
pairDeviceMock.mockClear();
|
|
533
|
-
pairDeviceMock
|
|
534
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
535
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
536
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false });
|
|
529
|
+
pairDeviceMock.mockResolvedValueOnce({ bonded: false, bonding: false });
|
|
537
530
|
|
|
538
531
|
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
539
532
|
errorCode: HardwareErrorCode.BleDeviceNotBonded,
|
|
540
533
|
});
|
|
541
|
-
// One initial attempt plus two bounded retries; the device was never bonded.
|
|
542
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(3);
|
|
543
534
|
expect(device.connect).not.toHaveBeenCalled();
|
|
544
535
|
expect(bleManager.devices).not.toHaveBeenCalled();
|
|
545
536
|
expect(bleManager.cancelDeviceConnection).toHaveBeenCalledWith(uuid);
|
|
546
537
|
});
|
|
547
538
|
|
|
548
|
-
test('skips createBond when the Android bonded list already has the device', async () => {
|
|
549
|
-
setPlatformOS('android');
|
|
550
|
-
const { transport, uuid } = createHarness();
|
|
551
|
-
const getBondedDevicesMock = jest.requireMock('../BleManager').getBondedDevices as jest.Mock;
|
|
552
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
553
|
-
// Same address with different case and separators must still match.
|
|
554
|
-
getBondedDevicesMock.mockResolvedValueOnce([
|
|
555
|
-
{
|
|
556
|
-
id: 'RN:PRO2:ID',
|
|
557
|
-
advertising: {},
|
|
558
|
-
bondState: { state: 'BOND_BONDED', preState: 'BOND_NONE' },
|
|
559
|
-
},
|
|
560
|
-
]);
|
|
561
|
-
pairDeviceMock.mockClear();
|
|
562
|
-
|
|
563
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
564
|
-
uuid,
|
|
565
|
-
protocolType: 'V2',
|
|
566
|
-
});
|
|
567
|
-
expect(pairDeviceMock).not.toHaveBeenCalled();
|
|
568
|
-
await transport.release(uuid, true);
|
|
569
|
-
});
|
|
570
|
-
|
|
571
|
-
test('falls back to pairDevice when the Android bonded list query fails', async () => {
|
|
572
|
-
setPlatformOS('android');
|
|
573
|
-
const { transport, uuid } = createHarness();
|
|
574
|
-
const getBondedDevicesMock = jest.requireMock('../BleManager').getBondedDevices as jest.Mock;
|
|
575
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
576
|
-
getBondedDevicesMock.mockRejectedValueOnce(new Error('native bonded list failed'));
|
|
577
|
-
pairDeviceMock.mockClear();
|
|
578
|
-
|
|
579
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
580
|
-
uuid,
|
|
581
|
-
protocolType: 'V2',
|
|
582
|
-
});
|
|
583
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
584
|
-
await transport.release(uuid, true);
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
test('retries createBond before connecting when the first attempt fails to start', async () => {
|
|
588
|
-
setPlatformOS('android');
|
|
589
|
-
const { transport, uuid, device } = createHarness();
|
|
590
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
591
|
-
pairDeviceMock.mockClear();
|
|
592
|
-
pairDeviceMock
|
|
593
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
594
|
-
.mockResolvedValueOnce({ bonded: true, bonding: false });
|
|
595
|
-
device.isConnected.mockResolvedValueOnce(false);
|
|
596
|
-
device.connect = jest.fn().mockResolvedValue(device);
|
|
597
|
-
|
|
598
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
599
|
-
uuid,
|
|
600
|
-
protocolType: 'V2',
|
|
601
|
-
});
|
|
602
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(2);
|
|
603
|
-
expect(device.connect).toHaveBeenCalled();
|
|
604
|
-
await transport.release(uuid, true);
|
|
605
|
-
});
|
|
606
|
-
|
|
607
|
-
test('reports a re-pairing error when a previously bonded device cannot restart bonding', async () => {
|
|
608
|
-
setPlatformOS('android');
|
|
609
|
-
const { transport, uuid } = createHarness();
|
|
610
|
-
const getBondedDevicesMock = jest.requireMock('../BleManager').getBondedDevices as jest.Mock;
|
|
611
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
612
|
-
|
|
613
|
-
// First pass: the device is already in the bonded list, which records it as known.
|
|
614
|
-
getBondedDevicesMock.mockResolvedValueOnce([
|
|
615
|
-
{ id: uuid, advertising: {}, bondState: { state: 'BOND_BONDED', preState: 'BOND_NONE' } },
|
|
616
|
-
]);
|
|
617
|
-
pairDeviceMock.mockClear();
|
|
618
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
619
|
-
uuid,
|
|
620
|
-
protocolType: 'V2',
|
|
621
|
-
});
|
|
622
|
-
expect(pairDeviceMock).not.toHaveBeenCalled();
|
|
623
|
-
await transport.release(uuid, true);
|
|
624
|
-
|
|
625
|
-
// Second pass: the host no longer holds the bond and createBond never starts.
|
|
626
|
-
getBondedDevicesMock.mockResolvedValue([]);
|
|
627
|
-
pairDeviceMock
|
|
628
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
629
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
630
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false });
|
|
631
|
-
|
|
632
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
633
|
-
errorCode: HardwareErrorCode.BleDeviceBondError,
|
|
634
|
-
params: { phase: 'bond', reason: 'peer_removed_pairing' },
|
|
635
|
-
});
|
|
636
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(3);
|
|
637
|
-
});
|
|
638
|
-
|
|
639
|
-
test('never queries the Android bonded list or pairs on iOS', async () => {
|
|
640
|
-
setPlatformOS('ios');
|
|
641
|
-
const { transport, uuid } = createHarness();
|
|
642
|
-
const getBondedDevicesMock = jest.requireMock('../BleManager').getBondedDevices as jest.Mock;
|
|
643
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
644
|
-
pairDeviceMock.mockClear();
|
|
645
|
-
getBondedDevicesMock.mockClear();
|
|
646
|
-
|
|
647
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
648
|
-
uuid,
|
|
649
|
-
protocolType: 'V2',
|
|
650
|
-
});
|
|
651
|
-
expect(getBondedDevicesMock).not.toHaveBeenCalled();
|
|
652
|
-
expect(pairDeviceMock).not.toHaveBeenCalled();
|
|
653
|
-
await transport.release(uuid, true);
|
|
654
|
-
});
|
|
655
|
-
|
|
656
539
|
test('checks that Android GATT is connected after bonding and reconnecting', async () => {
|
|
657
540
|
setPlatformOS('android');
|
|
658
541
|
const { transport, uuid, device, bleManager } = createHarness();
|
|
@@ -667,7 +550,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
667
550
|
});
|
|
668
551
|
|
|
669
552
|
test.each([
|
|
670
|
-
['ios', 'OneKey Neo', { iosErrorCode:
|
|
553
|
+
['ios', 'OneKey Neo', { iosErrorCode: 14, reason: 'Native connection failed' }],
|
|
671
554
|
['android', 'OneKey Pro 2', { androidErrorCode: 5, reason: 'Native connection failed' }],
|
|
672
555
|
] as const)(
|
|
673
556
|
'keeps a %s %s native connect failure generic before protocol detection',
|
|
@@ -709,293 +592,6 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
709
592
|
expect(device.isConnected).not.toHaveBeenCalled();
|
|
710
593
|
});
|
|
711
594
|
|
|
712
|
-
test('does not retry pairDevice after stop during Android bond delay', async () => {
|
|
713
|
-
setPlatformOS('android');
|
|
714
|
-
const { transport, uuid, device } = createHarness();
|
|
715
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
716
|
-
pairDeviceMock.mockClear();
|
|
717
|
-
pairDeviceMock.mockResolvedValueOnce({ bonded: false, bonding: false });
|
|
718
|
-
device.connect = jest.fn().mockResolvedValue(device);
|
|
719
|
-
|
|
720
|
-
const acquiring = transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
721
|
-
const rejection = expect(acquiring).rejects.toMatchObject({
|
|
722
|
-
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
723
|
-
});
|
|
724
|
-
await new Promise(resolve => {
|
|
725
|
-
setImmediate(resolve);
|
|
726
|
-
});
|
|
727
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
728
|
-
|
|
729
|
-
await transport.stop();
|
|
730
|
-
await rejection;
|
|
731
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
732
|
-
expect(device.connect).not.toHaveBeenCalled();
|
|
733
|
-
});
|
|
734
|
-
|
|
735
|
-
test('does not retry pairDevice after cancel during Android bond delay and allows the next acquire to bond', async () => {
|
|
736
|
-
setPlatformOS('android');
|
|
737
|
-
const { transport, uuid, device } = createHarness();
|
|
738
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
739
|
-
pairDeviceMock.mockClear();
|
|
740
|
-
pairDeviceMock
|
|
741
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
742
|
-
.mockResolvedValueOnce({ bonded: true, bonding: false });
|
|
743
|
-
device.connect = jest.fn().mockResolvedValue(device);
|
|
744
|
-
|
|
745
|
-
const acquiring = transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
746
|
-
const rejection = acquiring.catch(error => error);
|
|
747
|
-
await new Promise(resolve => {
|
|
748
|
-
setImmediate(resolve);
|
|
749
|
-
});
|
|
750
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
751
|
-
|
|
752
|
-
await transport.cancel();
|
|
753
|
-
const error = await rejection;
|
|
754
|
-
expect(error).toMatchObject({
|
|
755
|
-
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
756
|
-
});
|
|
757
|
-
expect([
|
|
758
|
-
HardwareErrorCode.BleDeviceNotBonded,
|
|
759
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
760
|
-
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
761
|
-
HardwareErrorCode.BleBondInvalid,
|
|
762
|
-
]).not.toContain(error.errorCode);
|
|
763
|
-
await new Promise(resolve => {
|
|
764
|
-
setTimeout(resolve, 350);
|
|
765
|
-
});
|
|
766
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
767
|
-
expect(device.connect).not.toHaveBeenCalled();
|
|
768
|
-
|
|
769
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
770
|
-
uuid,
|
|
771
|
-
protocolType: 'V2',
|
|
772
|
-
});
|
|
773
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(2);
|
|
774
|
-
await transport.release(uuid, true);
|
|
775
|
-
});
|
|
776
|
-
|
|
777
|
-
test('does not retry pairDevice after cancel while pairDevice is pending', async () => {
|
|
778
|
-
setPlatformOS('android');
|
|
779
|
-
const { transport, uuid, device } = createHarness();
|
|
780
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
781
|
-
const pendingBond = createDeferred<{ bonded: boolean; bonding: boolean }>();
|
|
782
|
-
pairDeviceMock.mockClear();
|
|
783
|
-
pairDeviceMock
|
|
784
|
-
.mockImplementationOnce(() => pendingBond.promise)
|
|
785
|
-
.mockResolvedValueOnce({ bonded: true, bonding: false });
|
|
786
|
-
device.connect = jest.fn().mockResolvedValue(device);
|
|
787
|
-
|
|
788
|
-
const acquiring = transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
789
|
-
const rejection = expect(acquiring).rejects.toMatchObject({
|
|
790
|
-
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
791
|
-
});
|
|
792
|
-
await new Promise(resolve => {
|
|
793
|
-
setImmediate(resolve);
|
|
794
|
-
});
|
|
795
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
796
|
-
|
|
797
|
-
const cancelling = transport.cancel();
|
|
798
|
-
pendingBond.resolve({ bonded: false, bonding: false });
|
|
799
|
-
await cancelling;
|
|
800
|
-
await rejection;
|
|
801
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
802
|
-
expect(device.connect).not.toHaveBeenCalled();
|
|
803
|
-
|
|
804
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
805
|
-
uuid,
|
|
806
|
-
protocolType: 'V2',
|
|
807
|
-
});
|
|
808
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(2);
|
|
809
|
-
await transport.release(uuid, true);
|
|
810
|
-
});
|
|
811
|
-
|
|
812
|
-
test('does not retry pairDevice after disconnect during Android bond delay', async () => {
|
|
813
|
-
setPlatformOS('android');
|
|
814
|
-
const { transport, uuid, device } = createHarness();
|
|
815
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
816
|
-
pairDeviceMock.mockClear();
|
|
817
|
-
pairDeviceMock
|
|
818
|
-
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
819
|
-
.mockResolvedValueOnce({ bonded: true, bonding: false });
|
|
820
|
-
device.connect = jest.fn().mockResolvedValue(device);
|
|
821
|
-
|
|
822
|
-
const acquiring = transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
823
|
-
const rejection = expect(acquiring).rejects.toMatchObject({
|
|
824
|
-
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
825
|
-
});
|
|
826
|
-
await new Promise(resolve => {
|
|
827
|
-
setImmediate(resolve);
|
|
828
|
-
});
|
|
829
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
830
|
-
|
|
831
|
-
await transport.disconnect(uuid);
|
|
832
|
-
await rejection;
|
|
833
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(1);
|
|
834
|
-
expect(device.connect).not.toHaveBeenCalled();
|
|
835
|
-
|
|
836
|
-
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).resolves.toEqual({
|
|
837
|
-
uuid,
|
|
838
|
-
protocolType: 'V2',
|
|
839
|
-
});
|
|
840
|
-
expect(pairDeviceMock).toHaveBeenCalledTimes(2);
|
|
841
|
-
await transport.release(uuid, true);
|
|
842
|
-
});
|
|
843
|
-
|
|
844
|
-
test('scopes explicit transport.cancel(uuid) so another device waiting on Android bonding succeeds', async () => {
|
|
845
|
-
setPlatformOS('android');
|
|
846
|
-
const uuidA = 'rn-pro2-a';
|
|
847
|
-
const uuidB = 'rn-pro2-b';
|
|
848
|
-
const { transport } = createHarness();
|
|
849
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
850
|
-
const onDeviceBondStateMock = jest.requireMock('../BleManager').onDeviceBondState as jest.Mock;
|
|
851
|
-
pairDeviceMock.mockClear();
|
|
852
|
-
onDeviceBondStateMock.mockClear();
|
|
853
|
-
|
|
854
|
-
const makeDevice = (uuid: string) => {
|
|
855
|
-
let notifyCallback:
|
|
856
|
-
| ((error: Error | null, characteristic: { value: string } | null) => void)
|
|
857
|
-
| undefined;
|
|
858
|
-
const notifyCharacteristic = {
|
|
859
|
-
uuid: '0003',
|
|
860
|
-
deviceID: uuid,
|
|
861
|
-
isNotifiable: true,
|
|
862
|
-
monitor: jest.fn(callback => {
|
|
863
|
-
notifyCallback = callback;
|
|
864
|
-
return { remove: jest.fn() };
|
|
865
|
-
}),
|
|
866
|
-
};
|
|
867
|
-
const handleWrite = () => {
|
|
868
|
-
const response = ProtocolV2.encodeFrame(
|
|
869
|
-
schemas,
|
|
870
|
-
'Success',
|
|
871
|
-
{ message: 'ok' },
|
|
872
|
-
{ router: PROTOCOL_V2_CHANNEL_BLE_UART, seq: 1 }
|
|
873
|
-
);
|
|
874
|
-
notifyCallback?.(null, { value: Buffer.from(response).toString('base64') });
|
|
875
|
-
return Promise.resolve();
|
|
876
|
-
};
|
|
877
|
-
const writeCharacteristic = {
|
|
878
|
-
uuid: '0002',
|
|
879
|
-
deviceID: uuid,
|
|
880
|
-
isWritableWithResponse: true,
|
|
881
|
-
isWritableWithoutResponse: true,
|
|
882
|
-
writeWithResponse: jest.fn(handleWrite),
|
|
883
|
-
writeWithoutResponse: jest.fn(handleWrite),
|
|
884
|
-
};
|
|
885
|
-
const device = {
|
|
886
|
-
id: uuid,
|
|
887
|
-
name: 'OneKey Pro 2',
|
|
888
|
-
localName: 'OneKey Pro 2',
|
|
889
|
-
mtu: 247,
|
|
890
|
-
serviceUUIDs: ['00000001-0000-1000-8000-00805f9b34fb'],
|
|
891
|
-
isConnected: jest.fn(() => Promise.resolve(true)),
|
|
892
|
-
cancelConnection: jest.fn(() => Promise.resolve()),
|
|
893
|
-
connect: jest.fn(),
|
|
894
|
-
onDisconnected: jest.fn(() => ({ remove: jest.fn() })),
|
|
895
|
-
} as any;
|
|
896
|
-
device.connect.mockResolvedValue(device);
|
|
897
|
-
device.requestMTU = jest.fn(() => Promise.resolve(device));
|
|
898
|
-
device.requestConnectionPriority = jest.fn(() => Promise.resolve(device));
|
|
899
|
-
return { device, writeCharacteristic, notifyCharacteristic };
|
|
900
|
-
};
|
|
901
|
-
|
|
902
|
-
const devA = makeDevice(uuidA);
|
|
903
|
-
const devB = makeDevice(uuidB);
|
|
904
|
-
const devicesById = new Map([
|
|
905
|
-
[uuidA, devA],
|
|
906
|
-
[uuidB, devB],
|
|
907
|
-
]);
|
|
908
|
-
transport.blePlxManager = {
|
|
909
|
-
devices: jest.fn((ids: string[]) =>
|
|
910
|
-
Promise.resolve(ids.map(id => devicesById.get(id)?.device).filter(Boolean))
|
|
911
|
-
),
|
|
912
|
-
connectedDevices: jest.fn(() => Promise.resolve([])),
|
|
913
|
-
cancelTransaction: jest.fn(() => Promise.resolve()),
|
|
914
|
-
cancelDeviceConnection: jest.fn(() => Promise.resolve()),
|
|
915
|
-
destroy: jest.fn(),
|
|
916
|
-
} as any;
|
|
917
|
-
transport.resolveCharacteristics = jest.fn((device: { id: string }) => {
|
|
918
|
-
const entry = devicesById.get(device.id);
|
|
919
|
-
if (!entry) throw new Error(`Unknown device ${device.id}`);
|
|
920
|
-
return Promise.resolve({
|
|
921
|
-
writeCharacteristic: entry.writeCharacteristic,
|
|
922
|
-
notifyCharacteristic: entry.notifyCharacteristic,
|
|
923
|
-
});
|
|
924
|
-
});
|
|
925
|
-
|
|
926
|
-
const bondA = createDeferred<void>();
|
|
927
|
-
const bondB = createDeferred<void>();
|
|
928
|
-
pairDeviceMock.mockImplementation((id: string) =>
|
|
929
|
-
Promise.resolve({ bonded: false, bonding: true, initiated: id === uuidB })
|
|
930
|
-
);
|
|
931
|
-
onDeviceBondStateMock.mockImplementation((id: string, signal?: AbortSignal) => {
|
|
932
|
-
const deferred = id === uuidA ? bondA : bondB;
|
|
933
|
-
return new Promise<void>((resolve, reject) => {
|
|
934
|
-
if (signal?.aborted) {
|
|
935
|
-
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceDisconnected));
|
|
936
|
-
return;
|
|
937
|
-
}
|
|
938
|
-
const onAbort = () => reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceDisconnected));
|
|
939
|
-
signal?.addEventListener('abort', onAbort, { once: true });
|
|
940
|
-
deferred.promise.then(
|
|
941
|
-
() => {
|
|
942
|
-
signal?.removeEventListener('abort', onAbort);
|
|
943
|
-
resolve();
|
|
944
|
-
},
|
|
945
|
-
err => {
|
|
946
|
-
signal?.removeEventListener('abort', onAbort);
|
|
947
|
-
reject(err);
|
|
948
|
-
}
|
|
949
|
-
);
|
|
950
|
-
});
|
|
951
|
-
});
|
|
952
|
-
|
|
953
|
-
const acquiringA = transport.acquire({ uuid: uuidA, expectedProtocol: 'V2' });
|
|
954
|
-
const acquiringB = transport.acquire({ uuid: uuidB, expectedProtocol: 'V2' });
|
|
955
|
-
await new Promise(resolve => {
|
|
956
|
-
setImmediate(resolve);
|
|
957
|
-
});
|
|
958
|
-
|
|
959
|
-
await transport.cancel(uuidA);
|
|
960
|
-
await expect(acquiringA).rejects.toMatchObject({
|
|
961
|
-
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
962
|
-
});
|
|
963
|
-
|
|
964
|
-
bondB.resolve();
|
|
965
|
-
await expect(acquiringB).resolves.toEqual({
|
|
966
|
-
uuid: uuidB,
|
|
967
|
-
protocolType: 'V2',
|
|
968
|
-
});
|
|
969
|
-
await transport.release(uuidB, true);
|
|
970
|
-
});
|
|
971
|
-
|
|
972
|
-
test('does not call pairDevice when stop happens before the first Android bond attempt', async () => {
|
|
973
|
-
setPlatformOS('android');
|
|
974
|
-
const { transport, uuid, device } = createHarness();
|
|
975
|
-
const getBondedDevicesMock = jest.requireMock('../BleManager').getBondedDevices as jest.Mock;
|
|
976
|
-
const pairDeviceMock = jest.requireMock('../BleManager').pairDevice as jest.Mock;
|
|
977
|
-
const bondedList = createDeferred<unknown[]>();
|
|
978
|
-
getBondedDevicesMock.mockImplementationOnce(() => bondedList.promise);
|
|
979
|
-
pairDeviceMock.mockClear();
|
|
980
|
-
device.connect = jest.fn().mockResolvedValue(device);
|
|
981
|
-
|
|
982
|
-
const acquiring = transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
983
|
-
const rejection = expect(acquiring).rejects.toMatchObject({
|
|
984
|
-
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
985
|
-
});
|
|
986
|
-
await new Promise(resolve => {
|
|
987
|
-
setImmediate(resolve);
|
|
988
|
-
});
|
|
989
|
-
expect(pairDeviceMock).not.toHaveBeenCalled();
|
|
990
|
-
|
|
991
|
-
const stopping = transport.stop();
|
|
992
|
-
bondedList.resolve([]);
|
|
993
|
-
await rejection;
|
|
994
|
-
await stopping;
|
|
995
|
-
expect(pairDeviceMock).not.toHaveBeenCalled();
|
|
996
|
-
expect(device.connect).not.toHaveBeenCalled();
|
|
997
|
-
});
|
|
998
|
-
|
|
999
595
|
test('does not connect after stop while Android bonding is pending', async () => {
|
|
1000
596
|
setPlatformOS('android');
|
|
1001
597
|
const { transport, uuid, bleManager } = createHarness();
|
|
@@ -1465,11 +1061,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1465
1061
|
async protocol => {
|
|
1466
1062
|
for (const errorCode of [
|
|
1467
1063
|
HardwareErrorCode.BleDeviceNotBonded,
|
|
1468
|
-
HardwareErrorCode.BleDeviceBondError,
|
|
1469
1064
|
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
1470
|
-
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1471
1065
|
HardwareErrorCode.BleDeviceDisconnected,
|
|
1472
|
-
HardwareErrorCode.BleBondInvalid,
|
|
1473
1066
|
HardwareErrorCode.BleCharacteristicNotifyError,
|
|
1474
1067
|
HardwareErrorCode.BleCharacteristicNotifyChangeFailure,
|
|
1475
1068
|
HardwareErrorCode.BleWriteCharacteristicError,
|
|
@@ -1493,32 +1086,6 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1493
1086
|
}
|
|
1494
1087
|
);
|
|
1495
1088
|
|
|
1496
|
-
test('preserves iOS stale bond from the V2 notification monitor during undeclared protocol probe', async () => {
|
|
1497
|
-
setPlatformOS('ios');
|
|
1498
|
-
const harness = createHarness();
|
|
1499
|
-
const { transport, uuid, writeCharacteristic } = harness;
|
|
1500
|
-
harness.setShouldRespond(false);
|
|
1501
|
-
jest
|
|
1502
|
-
.spyOn(transport as any, 'callProtocolV1')
|
|
1503
|
-
.mockRejectedValueOnce(ERRORS.TypedError(HardwareErrorCode.BleTimeoutError));
|
|
1504
|
-
writeCharacteristic.writeWithoutResponse.mockImplementationOnce(() => {
|
|
1505
|
-
setTimeout(() => {
|
|
1506
|
-
harness.emitMonitorError(
|
|
1507
|
-
Object.assign(new Error('Device was disconnected'), {
|
|
1508
|
-
iosErrorCode: 14,
|
|
1509
|
-
reason: 'Peer removed pairing information',
|
|
1510
|
-
})
|
|
1511
|
-
);
|
|
1512
|
-
}, 0);
|
|
1513
|
-
return Promise.resolve();
|
|
1514
|
-
});
|
|
1515
|
-
|
|
1516
|
-
await expect(transport.acquire({ uuid })).rejects.toMatchObject({
|
|
1517
|
-
errorCode: HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1518
|
-
});
|
|
1519
|
-
expect(transport.getProtocolType(uuid)).toBeUndefined();
|
|
1520
|
-
});
|
|
1521
|
-
|
|
1522
1089
|
test.each(['V1', 'V2'] as const)(
|
|
1523
1090
|
'still falls back after a silent %s probe timeout',
|
|
1524
1091
|
async protocol => {
|
|
@@ -1683,7 +1250,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1683
1250
|
device.connect.mockRejectedValueOnce(
|
|
1684
1251
|
Object.assign(new BleErrorMock('Native connection failed'), {
|
|
1685
1252
|
errorCode: 200,
|
|
1686
|
-
iosErrorCode:
|
|
1253
|
+
iosErrorCode: 14,
|
|
1687
1254
|
})
|
|
1688
1255
|
);
|
|
1689
1256
|
|
|
@@ -2416,56 +1983,6 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
2416
1983
|
});
|
|
2417
1984
|
});
|
|
2418
1985
|
|
|
2419
|
-
test('maps an iOS stale bond from the V2 notification monitor', async () => {
|
|
2420
|
-
setPlatformOS('ios');
|
|
2421
|
-
const harness = createHarness();
|
|
2422
|
-
const { transport, uuid, sentSeqs } = harness;
|
|
2423
|
-
await transport.acquire({ uuid, expectedProtocol: 'V2' });
|
|
2424
|
-
harness.setShouldRespond(false);
|
|
2425
|
-
|
|
2426
|
-
const call = transport.call(uuid, 'Ping', { message: 'stale-bond' }, { timeoutMs: 50 });
|
|
2427
|
-
while (sentSeqs.length < 1) {
|
|
2428
|
-
await new Promise(resolve => {
|
|
2429
|
-
setTimeout(resolve, 0);
|
|
2430
|
-
});
|
|
2431
|
-
}
|
|
2432
|
-
await new Promise(resolve => {
|
|
2433
|
-
setTimeout(resolve, 0);
|
|
2434
|
-
});
|
|
2435
|
-
harness.emitMonitorError(
|
|
2436
|
-
Object.assign(new Error('Device was disconnected'), {
|
|
2437
|
-
iosErrorCode: 14,
|
|
2438
|
-
reason: 'Peer removed pairing information',
|
|
2439
|
-
})
|
|
2440
|
-
);
|
|
2441
|
-
|
|
2442
|
-
await expect(call).rejects.toMatchObject({
|
|
2443
|
-
errorCode: HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
2444
|
-
});
|
|
2445
|
-
});
|
|
2446
|
-
|
|
2447
|
-
test('maps an iOS stale bond from the V1 notification monitor', async () => {
|
|
2448
|
-
setPlatformOS('ios');
|
|
2449
|
-
const harness = createV1Harness({ respondOnWriteCount: [] });
|
|
2450
|
-
const { transport, uuid } = harness;
|
|
2451
|
-
await transport.acquire({ uuid, expectedProtocol: 'V1' });
|
|
2452
|
-
|
|
2453
|
-
const call = transport.call(uuid, 'Initialize', {}, { timeoutMs: 200 });
|
|
2454
|
-
await new Promise(resolve => {
|
|
2455
|
-
setTimeout(resolve, 0);
|
|
2456
|
-
});
|
|
2457
|
-
harness.emitMonitorError(
|
|
2458
|
-
Object.assign(new Error('Device was disconnected'), {
|
|
2459
|
-
iosErrorCode: 14,
|
|
2460
|
-
reason: 'Peer removed pairing information',
|
|
2461
|
-
})
|
|
2462
|
-
);
|
|
2463
|
-
|
|
2464
|
-
await expect(call).rejects.toMatchObject({
|
|
2465
|
-
errorCode: HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
2466
|
-
});
|
|
2467
|
-
});
|
|
2468
|
-
|
|
2469
1986
|
test.each(['ios', 'android'] as const)(
|
|
2470
1987
|
'disconnects after an active V2 timeout and reconnects even if a listener fails on %s',
|
|
2471
1988
|
async platform => {
|