@onekeyfe/hd-transport-react-native 1.2.11-alpha.2 → 1.2.11-alpha.3
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/bleIosBond.d.ts +3 -0
- package/dist/bleIosBond.d.ts.map +1 -0
- package/dist/index.d.ts +53 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +461 -272
- package/package.json +5 -5
- package/src/__tests__/bleIosBond.test.ts +35 -0
- package/src/__tests__/iosPresentDeviceConnect.test.ts +228 -0
- package/src/__tests__/nativeFrameWrite.test.ts +1 -0
- package/src/__tests__/protocolV2Link.test.ts +486 -3
- package/src/__tests__/releaseBondConcurrency.test.ts +284 -0
- package/src/bleIosBond.ts +39 -0
- package/src/index.ts +622 -336
|
@@ -41,6 +41,7 @@ jest.mock('react-native-ble-plx', () => ({
|
|
|
41
41
|
}));
|
|
42
42
|
|
|
43
43
|
jest.mock('../BleManager', () => ({
|
|
44
|
+
getBondedDevices: jest.fn(() => Promise.resolve([])),
|
|
44
45
|
getConnectedDeviceIds: jest.fn(() => Promise.resolve([])),
|
|
45
46
|
onDeviceBondState: jest.fn(() => Promise.resolve()),
|
|
46
47
|
pairDevice: jest.fn(() => Promise.resolve({ bonded: true, bonding: false })),
|
|
@@ -299,6 +300,9 @@ const createV1Harness = ({
|
|
|
299
300
|
device,
|
|
300
301
|
bleManager,
|
|
301
302
|
writeCharacteristic,
|
|
303
|
+
emitMonitorError(error: Error & { reason?: string; iosErrorCode?: number }) {
|
|
304
|
+
notifyCallback?.(error, null);
|
|
305
|
+
},
|
|
302
306
|
notifySubscriptionRemovers,
|
|
303
307
|
disconnectSubscriptionRemovers,
|
|
304
308
|
};
|
|
@@ -526,16 +530,129 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
526
530
|
|
|
527
531
|
device.connect = jest.fn().mockResolvedValue(device);
|
|
528
532
|
pairDeviceMock.mockClear();
|
|
529
|
-
pairDeviceMock
|
|
533
|
+
pairDeviceMock
|
|
534
|
+
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
535
|
+
.mockResolvedValueOnce({ bonded: false, bonding: false })
|
|
536
|
+
.mockResolvedValueOnce({ bonded: false, bonding: false });
|
|
530
537
|
|
|
531
538
|
await expect(transport.acquire({ uuid, expectedProtocol: 'V2' })).rejects.toMatchObject({
|
|
532
539
|
errorCode: HardwareErrorCode.BleDeviceNotBonded,
|
|
533
540
|
});
|
|
541
|
+
// One initial attempt plus two bounded retries; the device was never bonded.
|
|
542
|
+
expect(pairDeviceMock).toHaveBeenCalledTimes(3);
|
|
534
543
|
expect(device.connect).not.toHaveBeenCalled();
|
|
535
544
|
expect(bleManager.devices).not.toHaveBeenCalled();
|
|
536
545
|
expect(bleManager.cancelDeviceConnection).toHaveBeenCalledWith(uuid);
|
|
537
546
|
});
|
|
538
547
|
|
|
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
|
+
|
|
539
656
|
test('checks that Android GATT is connected after bonding and reconnecting', async () => {
|
|
540
657
|
setPlatformOS('android');
|
|
541
658
|
const { transport, uuid, device, bleManager } = createHarness();
|
|
@@ -550,7 +667,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
550
667
|
});
|
|
551
668
|
|
|
552
669
|
test.each([
|
|
553
|
-
['ios', 'OneKey Neo', { iosErrorCode:
|
|
670
|
+
['ios', 'OneKey Neo', { iosErrorCode: 6, reason: 'Native connection failed' }],
|
|
554
671
|
['android', 'OneKey Pro 2', { androidErrorCode: 5, reason: 'Native connection failed' }],
|
|
555
672
|
] as const)(
|
|
556
673
|
'keeps a %s %s native connect failure generic before protocol detection',
|
|
@@ -592,6 +709,293 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
592
709
|
expect(device.isConnected).not.toHaveBeenCalled();
|
|
593
710
|
});
|
|
594
711
|
|
|
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
|
+
|
|
595
999
|
test('does not connect after stop while Android bonding is pending', async () => {
|
|
596
1000
|
setPlatformOS('android');
|
|
597
1001
|
const { transport, uuid, bleManager } = createHarness();
|
|
@@ -1061,8 +1465,11 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1061
1465
|
async protocol => {
|
|
1062
1466
|
for (const errorCode of [
|
|
1063
1467
|
HardwareErrorCode.BleDeviceNotBonded,
|
|
1468
|
+
HardwareErrorCode.BleDeviceBondError,
|
|
1064
1469
|
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
1470
|
+
HardwareErrorCode.BlePeerRemovedPairingInformation,
|
|
1065
1471
|
HardwareErrorCode.BleDeviceDisconnected,
|
|
1472
|
+
HardwareErrorCode.BleBondInvalid,
|
|
1066
1473
|
HardwareErrorCode.BleCharacteristicNotifyError,
|
|
1067
1474
|
HardwareErrorCode.BleCharacteristicNotifyChangeFailure,
|
|
1068
1475
|
HardwareErrorCode.BleWriteCharacteristicError,
|
|
@@ -1086,6 +1493,32 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1086
1493
|
}
|
|
1087
1494
|
);
|
|
1088
1495
|
|
|
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
|
+
|
|
1089
1522
|
test.each(['V1', 'V2'] as const)(
|
|
1090
1523
|
'still falls back after a silent %s probe timeout',
|
|
1091
1524
|
async protocol => {
|
|
@@ -1250,7 +1683,7 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1250
1683
|
device.connect.mockRejectedValueOnce(
|
|
1251
1684
|
Object.assign(new BleErrorMock('Native connection failed'), {
|
|
1252
1685
|
errorCode: 200,
|
|
1253
|
-
iosErrorCode:
|
|
1686
|
+
iosErrorCode: 6,
|
|
1254
1687
|
})
|
|
1255
1688
|
);
|
|
1256
1689
|
|
|
@@ -1983,6 +2416,56 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
|
|
|
1983
2416
|
});
|
|
1984
2417
|
});
|
|
1985
2418
|
|
|
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
|
+
|
|
1986
2469
|
test.each(['ios', 'android'] as const)(
|
|
1987
2470
|
'disconnects after an active V2 timeout and reconnects even if a listener fails on %s',
|
|
1988
2471
|
async platform => {
|