@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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,
|
|
1
|
+
{"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAsC9E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10-alpha.1",
|
|
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.
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.
|
|
23
|
+
"@onekeyfe/hd-core": "1.2.10-alpha.1",
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.10-alpha.1",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.10-alpha.1",
|
|
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": "df785eba29f144fac2cfdcbd808426e0a2c259b2"
|
|
30
30
|
}
|
package/src/BleManager.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
import
|
|
1
|
+
import BleUtilsModule from '@onekeyfe/react-native-ble-utils';
|
|
2
2
|
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
3
3
|
|
|
4
|
-
import { onBleKeyMissing, startBleKeyMissingTracking } from './bleKeyMissing';
|
|
5
4
|
import { bleLogger } from './logger';
|
|
6
5
|
|
|
7
6
|
import type { Peripheral } from '@onekeyfe/react-native-ble-utils';
|
|
8
7
|
|
|
9
8
|
const Logger = bleLogger;
|
|
9
|
+
const BleUtils =
|
|
10
|
+
(BleUtilsModule as typeof BleUtilsModule & { default?: typeof BleUtilsModule }).default ??
|
|
11
|
+
BleUtilsModule;
|
|
10
12
|
|
|
11
13
|
// Android BluetoothDevice.EXTRA_UNBOND_REASON values.
|
|
12
14
|
const bondFailureReasons: Record<number, string> = {
|
|
@@ -33,20 +35,7 @@ export const getBondedDevices = () => BleUtils.getBondedPeripherals();
|
|
|
33
35
|
|
|
34
36
|
export const pairDevice = (macAddress: string) => BleUtils.pairDevice(macAddress);
|
|
35
37
|
|
|
36
|
-
|
|
37
|
-
* Android replaces a bond as BONDING -> NONE, then NONE -> BONDING within milliseconds.
|
|
38
|
-
* The window covers slow broadcast delivery before the first half counts as a failure.
|
|
39
|
-
*/
|
|
40
|
-
export const SYSTEM_BONDING_RESTART_WINDOW_MS = 1500;
|
|
41
|
-
|
|
42
|
-
export type DeviceBondStateOptions = {
|
|
43
|
-
/**
|
|
44
|
-
* The system, not this transport, started the bonding in progress. Android 16+ re-pairs
|
|
45
|
-
* by itself after it detects a lost bond, and a successful re-pair replaces the old bond
|
|
46
|
-
* instead of going straight to BONDED.
|
|
47
|
-
*/
|
|
48
|
-
systemInitiated?: boolean;
|
|
49
|
-
};
|
|
38
|
+
const SYSTEM_BONDING_RESTART_WINDOW_MS = 1500;
|
|
50
39
|
|
|
51
40
|
const createBondFailureError = (bondState: Peripheral['bondState']) => {
|
|
52
41
|
const nativeReason =
|
|
@@ -83,7 +72,7 @@ const createBondFailureError = (bondState: Peripheral['bondState']) => {
|
|
|
83
72
|
export const onDeviceBondState = (
|
|
84
73
|
bleMacAddress: string,
|
|
85
74
|
signal?: AbortSignal,
|
|
86
|
-
options?:
|
|
75
|
+
options?: { systemInitiated?: boolean }
|
|
87
76
|
): Promise<Peripheral | undefined> =>
|
|
88
77
|
new Promise((resolve, reject) => {
|
|
89
78
|
if (signal?.aborted) {
|
|
@@ -97,7 +86,6 @@ export const onDeviceBondState = (
|
|
|
97
86
|
}
|
|
98
87
|
if (pendingFailure) clearTimeout(pendingFailure);
|
|
99
88
|
if (cleanupListener) cleanupListener();
|
|
100
|
-
cleanupKeyMissing?.();
|
|
101
89
|
signal?.removeEventListener('abort', onAbort);
|
|
102
90
|
};
|
|
103
91
|
const onAbort = () => {
|
|
@@ -117,20 +105,6 @@ export const onDeviceBondState = (
|
|
|
117
105
|
);
|
|
118
106
|
}, 60 * 1000);
|
|
119
107
|
|
|
120
|
-
// A failed system re-pair restores the old bond, so it also ends in BONDED. Key
|
|
121
|
-
// missing is what tells it apart from a bond that now works.
|
|
122
|
-
const cleanupKeyMissing = startBleKeyMissingTracking()
|
|
123
|
-
? onBleKeyMissing(bleMacAddress, () => {
|
|
124
|
-
cleanup();
|
|
125
|
-
reject(
|
|
126
|
-
ERRORS.TypedError(HardwareErrorCode.BleBondInvalid, undefined, {
|
|
127
|
-
phase: 'bond',
|
|
128
|
-
reason: 'key_missing',
|
|
129
|
-
})
|
|
130
|
-
);
|
|
131
|
-
})
|
|
132
|
-
: undefined;
|
|
133
|
-
|
|
134
108
|
const cleanupListener = BleUtils.onDeviceBondState(peripheral => {
|
|
135
109
|
if (peripheral.id?.toLowerCase() !== bleMacAddress.toLowerCase()) {
|
|
136
110
|
return;
|
|
@@ -148,6 +122,8 @@ export const onDeviceBondState = (
|
|
|
148
122
|
clearTimeout(pendingFailure);
|
|
149
123
|
pendingFailure = undefined;
|
|
150
124
|
} else if (hasFailed && options?.systemInitiated) {
|
|
125
|
+
// Android can briefly remove the old bond before continuing an
|
|
126
|
+
// already-running system pairing. The original overall deadline stays.
|
|
151
127
|
if (pendingFailure) clearTimeout(pendingFailure);
|
|
152
128
|
pendingFailure = setTimeout(() => {
|
|
153
129
|
cleanup();
|
|
@@ -14,7 +14,7 @@ describe('native BLE disconnect mapping', () => {
|
|
|
14
14
|
});
|
|
15
15
|
});
|
|
16
16
|
|
|
17
|
-
test('does not treat
|
|
17
|
+
test('does not treat an unrelated iOS code as a disconnect', () => {
|
|
18
18
|
expect(
|
|
19
19
|
isNativeBleDisconnectError({
|
|
20
20
|
iosErrorCode: 14,
|
|
@@ -30,4 +30,25 @@ describe('native BLE disconnect mapping', () => {
|
|
|
30
30
|
})
|
|
31
31
|
).toBe(false);
|
|
32
32
|
});
|
|
33
|
+
|
|
34
|
+
test('recognizes the unstructured ble-plx disconnect observed during GATT discovery', () => {
|
|
35
|
+
const nativeError = {
|
|
36
|
+
errorCode: 0,
|
|
37
|
+
message: 'BleError: Device stalled-connect-device was disconnected',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
expect(isNativeBleDisconnectError(nativeError)).toBe(true);
|
|
41
|
+
expect(toBleDisconnectHardwareError(nativeError)).toMatchObject({
|
|
42
|
+
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('does not infer a disconnect from an unrelated discovery failure', () => {
|
|
47
|
+
expect(
|
|
48
|
+
isNativeBleDisconnectError({
|
|
49
|
+
errorCode: 0,
|
|
50
|
+
message: 'Known OneKey service UUID not found',
|
|
51
|
+
})
|
|
52
|
+
).toBe(false);
|
|
53
|
+
});
|
|
33
54
|
});
|
|
@@ -129,6 +129,102 @@ describe('Android bond failure reasons', () => {
|
|
|
129
129
|
expect(cleanup).toHaveBeenCalledTimes(1);
|
|
130
130
|
});
|
|
131
131
|
|
|
132
|
+
test('lets system-initiated bonding restart before accepting the replacement bond', async () => {
|
|
133
|
+
const result = onDeviceBondState(UUID, undefined, { systemInitiated: true });
|
|
134
|
+
const outcome = result.catch(error => error);
|
|
135
|
+
emitBondState({
|
|
136
|
+
id: UUID,
|
|
137
|
+
advertising: {},
|
|
138
|
+
bondState: { preState: 'BOND_BONDING', state: 'BOND_NONE', reason: 9 },
|
|
139
|
+
});
|
|
140
|
+
jest.advanceTimersByTime(1499);
|
|
141
|
+
expect(cleanup).not.toHaveBeenCalled();
|
|
142
|
+
|
|
143
|
+
emitBondState({
|
|
144
|
+
id: UUID,
|
|
145
|
+
advertising: {},
|
|
146
|
+
bondState: { preState: 'BOND_NONE', state: 'BOND_BONDING' },
|
|
147
|
+
});
|
|
148
|
+
jest.advanceTimersByTime(1500);
|
|
149
|
+
expect(cleanup).not.toHaveBeenCalled();
|
|
150
|
+
const bonded: Peripheral = {
|
|
151
|
+
id: UUID,
|
|
152
|
+
advertising: {},
|
|
153
|
+
bondState: { preState: 'BOND_BONDING', state: 'BOND_BONDED' },
|
|
154
|
+
};
|
|
155
|
+
emitBondState(bonded);
|
|
156
|
+
|
|
157
|
+
await expect(outcome).resolves.toBe(bonded);
|
|
158
|
+
expect(cleanup).toHaveBeenCalledTimes(1);
|
|
159
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test('rejects a genuine system bond failure when the restart window expires', async () => {
|
|
163
|
+
const result = onDeviceBondState(UUID, undefined, { systemInitiated: true });
|
|
164
|
+
const rejection = expect(result).rejects.toMatchObject({
|
|
165
|
+
errorCode: HardwareErrorCode.BleDeviceNotBonded,
|
|
166
|
+
params: { phase: 'bond', reason: 'authentication_failed', nativeReason: 1 },
|
|
167
|
+
});
|
|
168
|
+
emitBondState({
|
|
169
|
+
id: UUID,
|
|
170
|
+
advertising: {},
|
|
171
|
+
bondState: { preState: 'BOND_BONDING', state: 'BOND_NONE', reason: 1 },
|
|
172
|
+
});
|
|
173
|
+
jest.advanceTimersByTime(1499);
|
|
174
|
+
expect(cleanup).not.toHaveBeenCalled();
|
|
175
|
+
jest.advanceTimersByTime(1);
|
|
176
|
+
|
|
177
|
+
await rejection;
|
|
178
|
+
expect(cleanup).toHaveBeenCalledTimes(1);
|
|
179
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('aborts a pending system restart without leaving either timer or listener', async () => {
|
|
183
|
+
const controller = new AbortController();
|
|
184
|
+
const result = onDeviceBondState(UUID, controller.signal, { systemInitiated: true });
|
|
185
|
+
const outcome = result.catch(error => error);
|
|
186
|
+
emitBondState({
|
|
187
|
+
id: UUID,
|
|
188
|
+
advertising: {},
|
|
189
|
+
bondState: { preState: 'BOND_BONDING', state: 'BOND_NONE', reason: 9 },
|
|
190
|
+
});
|
|
191
|
+
expect(cleanup).not.toHaveBeenCalled();
|
|
192
|
+
|
|
193
|
+
controller.abort();
|
|
194
|
+
await expect(outcome).resolves.toMatchObject({
|
|
195
|
+
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
196
|
+
});
|
|
197
|
+
expect(cleanup).toHaveBeenCalledTimes(1);
|
|
198
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test('keeps the overall deadline when system bonding repeatedly restarts', async () => {
|
|
202
|
+
const result = onDeviceBondState(UUID, undefined, { systemInitiated: true });
|
|
203
|
+
const outcome = result.catch(error => error);
|
|
204
|
+
for (let index = 0; index < 40; index += 1) {
|
|
205
|
+
emitBondState({
|
|
206
|
+
id: UUID,
|
|
207
|
+
advertising: {},
|
|
208
|
+
bondState: { preState: 'BOND_BONDING', state: 'BOND_NONE', reason: 9 },
|
|
209
|
+
});
|
|
210
|
+
emitBondState({
|
|
211
|
+
id: UUID,
|
|
212
|
+
advertising: {},
|
|
213
|
+
bondState: { preState: 'BOND_NONE', state: 'BOND_BONDING' },
|
|
214
|
+
});
|
|
215
|
+
jest.advanceTimersByTime(1000);
|
|
216
|
+
}
|
|
217
|
+
expect(cleanup).not.toHaveBeenCalled();
|
|
218
|
+
jest.advanceTimersByTime(20_000);
|
|
219
|
+
|
|
220
|
+
await expect(outcome).resolves.toMatchObject({
|
|
221
|
+
errorCode: HardwareErrorCode.BleDeviceNotBonded,
|
|
222
|
+
params: { phase: 'bond', reason: 'timeout' },
|
|
223
|
+
});
|
|
224
|
+
expect(cleanup).toHaveBeenCalledTimes(1);
|
|
225
|
+
expect(jest.getTimerCount()).toBe(0);
|
|
226
|
+
});
|
|
227
|
+
|
|
132
228
|
test('aborts the bond wait and removes its listener and deadline', async () => {
|
|
133
229
|
const controller = new AbortController();
|
|
134
230
|
const result = onDeviceBondState(UUID, controller.signal);
|
|
@@ -623,6 +719,20 @@ describe('BLE connect timeout', () => {
|
|
|
623
719
|
expect(bleManager.cancelDeviceConnection).toHaveBeenCalledWith(UUID);
|
|
624
720
|
});
|
|
625
721
|
|
|
722
|
+
test('normalizes an unstructured GATT disconnect instead of leaking an unknown error', async () => {
|
|
723
|
+
const { transport, device } = createHarness(() => Promise.resolve());
|
|
724
|
+
const nativeError = Object.assign(new Error(`BleError: Device ${UUID} was disconnected`), {
|
|
725
|
+
errorCode: 0,
|
|
726
|
+
});
|
|
727
|
+
device.discoverAllServicesAndCharacteristics.mockRejectedValueOnce(nativeError);
|
|
728
|
+
|
|
729
|
+
await expect(
|
|
730
|
+
(transport as any).resolveCharacteristicsWithTimeout(UUID, device)
|
|
731
|
+
).rejects.toMatchObject({
|
|
732
|
+
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
733
|
+
});
|
|
734
|
+
});
|
|
735
|
+
|
|
626
736
|
test('a successful GATT retry clears the timeout budget before an abandoned call settles', async () => {
|
|
627
737
|
const { transport, device, bleManager } = createHarness(() => Promise.resolve());
|
|
628
738
|
let resolveAbandonedDiscovery: (() => void) | undefined;
|
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import { EventEmitter } from 'events';
|
|
2
|
+
import { PermissionsAndroid, Platform } from 'react-native';
|
|
3
|
+
import { BleErrorCode } from 'react-native-ble-plx';
|
|
4
|
+
import { HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
5
|
|
|
3
6
|
import { getConnectedDeviceIds } from '../BleManager';
|
|
4
7
|
import ReactNativeBleTransport from '../index';
|
|
8
|
+
import { subscribeBleOn } from '../subscribeBleOn';
|
|
5
9
|
|
|
6
10
|
jest.mock(
|
|
7
11
|
'react-native',
|
|
8
12
|
() => ({
|
|
9
|
-
PermissionsAndroid: {
|
|
13
|
+
PermissionsAndroid: {
|
|
14
|
+
PERMISSIONS: {
|
|
15
|
+
BLUETOOTH_CONNECT: 'android.permission.BLUETOOTH_CONNECT',
|
|
16
|
+
BLUETOOTH_SCAN: 'android.permission.BLUETOOTH_SCAN',
|
|
17
|
+
},
|
|
18
|
+
requestMultiple: jest.fn(),
|
|
19
|
+
},
|
|
10
20
|
Platform: { OS: 'ios' },
|
|
11
21
|
}),
|
|
12
22
|
{ virtual: true }
|
|
@@ -14,7 +24,14 @@ jest.mock(
|
|
|
14
24
|
|
|
15
25
|
jest.mock('react-native-ble-plx', () => ({
|
|
16
26
|
BleError: class BleError extends Error {},
|
|
17
|
-
BleErrorCode: {
|
|
27
|
+
BleErrorCode: {
|
|
28
|
+
BluetoothUnsupported: 100,
|
|
29
|
+
BluetoothUnauthorized: 101,
|
|
30
|
+
BluetoothPoweredOff: 102,
|
|
31
|
+
BluetoothInUnknownState: 103,
|
|
32
|
+
ScanStartFailed: 600,
|
|
33
|
+
LocationServicesDisabled: 601,
|
|
34
|
+
},
|
|
18
35
|
BleManager: jest.fn(),
|
|
19
36
|
ScanMode: { LowLatency: 2 },
|
|
20
37
|
}));
|
|
@@ -25,27 +42,61 @@ jest.mock('../BleManager', () => ({
|
|
|
25
42
|
pairDevice: jest.fn(),
|
|
26
43
|
}));
|
|
27
44
|
|
|
28
|
-
// The native facade is replaced wholesale above; key missing and link encryption are covered
|
|
29
|
-
// by their own suites.
|
|
30
|
-
jest.mock('../bleKeyMissing', () => ({
|
|
31
|
-
isBleKeyMissingSupported: jest.fn(() => false),
|
|
32
|
-
startBleKeyMissingTracking: jest.fn(() => false),
|
|
33
|
-
stopBleKeyMissingTracking: jest.fn(),
|
|
34
|
-
waitForBleKeyMissing: jest.fn(() => Promise.resolve(false)),
|
|
35
|
-
}));
|
|
36
|
-
jest.mock('../bleEncryption', () => ({
|
|
37
|
-
markBleLinkEncrypted: jest.fn(),
|
|
38
|
-
startBleEncryptionTracking: jest.fn(() => false),
|
|
39
|
-
stopBleEncryptionTracking: jest.fn(),
|
|
40
|
-
waitForAndroidLinkEncryption: jest.fn(() => Promise.resolve('unresolved')),
|
|
41
|
-
}));
|
|
42
|
-
|
|
43
45
|
jest.mock('../subscribeBleOn', () => ({
|
|
44
46
|
subscribeBleOn: jest.fn(() => Promise.resolve()),
|
|
45
47
|
}));
|
|
46
48
|
|
|
47
49
|
const ONEKEY_SERVICE_UUID = '00000001-0000-1000-8000-00805f9b34fb';
|
|
48
50
|
|
|
51
|
+
describe('ReactNativeBleTransport scan error mapping', () => {
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
jest.clearAllMocks();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
Object.assign(Platform, { OS: 'ios', Version: undefined });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('checks Android Bluetooth permissions before waiting for adapter state', async () => {
|
|
61
|
+
Object.assign(Platform, { OS: 'android', Version: 31 });
|
|
62
|
+
jest.mocked(PermissionsAndroid).requestMultiple.mockResolvedValueOnce({
|
|
63
|
+
[PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT]: 'denied',
|
|
64
|
+
[PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN]: 'denied',
|
|
65
|
+
});
|
|
66
|
+
const transport = new ReactNativeBleTransport({});
|
|
67
|
+
transport.blePlxManager = {} as never;
|
|
68
|
+
transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
|
|
69
|
+
|
|
70
|
+
await expect(transport.enumerate()).rejects.toMatchObject({
|
|
71
|
+
errorCode: HardwareErrorCode.BleLocationError,
|
|
72
|
+
});
|
|
73
|
+
expect(subscribeBleOn).not.toHaveBeenCalled();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test.each([
|
|
77
|
+
[BleErrorCode.BluetoothPoweredOff, HardwareErrorCode.BlePoweredOff],
|
|
78
|
+
[BleErrorCode.BluetoothUnsupported, HardwareErrorCode.BleUnsupported],
|
|
79
|
+
[BleErrorCode.BluetoothInUnknownState, HardwareErrorCode.BleScanError],
|
|
80
|
+
[BleErrorCode.BluetoothUnauthorized, HardwareErrorCode.BleLocationError],
|
|
81
|
+
])('maps native BLE error %s to hardware error %s', async (nativeCode, errorCode) => {
|
|
82
|
+
jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([]);
|
|
83
|
+
const blePlxManager = {
|
|
84
|
+
startDeviceScan: jest.fn((_serviceUUIDs, _options, listener) => {
|
|
85
|
+
queueMicrotask(() => {
|
|
86
|
+
listener({ errorCode: nativeCode, reason: 'native scan failure' }, null);
|
|
87
|
+
});
|
|
88
|
+
}),
|
|
89
|
+
stopDeviceScan: jest.fn(() => Promise.resolve()),
|
|
90
|
+
};
|
|
91
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 10_000 });
|
|
92
|
+
transport.blePlxManager = blePlxManager as never;
|
|
93
|
+
transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
|
|
94
|
+
|
|
95
|
+
await expect(transport.enumerate()).rejects.toMatchObject({ errorCode });
|
|
96
|
+
expect(blePlxManager.stopDeviceScan).toHaveBeenCalledTimes(1);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
49
100
|
describe('ReactNativeBleTransport iOS discovery', () => {
|
|
50
101
|
test('keeps a bonded Pro2 communication peripheral after Find My changes its name', async () => {
|
|
51
102
|
jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([
|
|
@@ -163,3 +214,142 @@ describe('ReactNativeBleTransport iOS discovery', () => {
|
|
|
163
214
|
expect(devices.map(device => device.id)).toEqual(['wallet-peripheral']);
|
|
164
215
|
});
|
|
165
216
|
});
|
|
217
|
+
|
|
218
|
+
describe('ReactNativeBleTransport advertisement color', () => {
|
|
219
|
+
const PRO2_SERVICE_UUIDS = [
|
|
220
|
+
'0000180a-0000-1000-8000-00805f9b34fb',
|
|
221
|
+
'0000180f-0000-1000-8000-00805f9b34fb',
|
|
222
|
+
'0000fffd-0000-1000-8000-00805f9b34fb',
|
|
223
|
+
ONEKEY_SERVICE_UUID,
|
|
224
|
+
];
|
|
225
|
+
const manufacturerData = (bytes: number[]) => Buffer.from(bytes).toString('base64');
|
|
226
|
+
|
|
227
|
+
const scanOnce = async (advertisements: Record<string, unknown>[]) => {
|
|
228
|
+
const blePlxManager = {
|
|
229
|
+
startDeviceScan: jest.fn((_serviceUUIDs, _options, listener) => {
|
|
230
|
+
queueMicrotask(() => {
|
|
231
|
+
advertisements.forEach(advertisement => listener(null, advertisement));
|
|
232
|
+
});
|
|
233
|
+
}),
|
|
234
|
+
stopDeviceScan: jest.fn(),
|
|
235
|
+
};
|
|
236
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
|
|
237
|
+
transport.blePlxManager = blePlxManager as never;
|
|
238
|
+
transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
|
|
239
|
+
return transport.enumerate();
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
beforeEach(() => {
|
|
243
|
+
jest.clearAllMocks();
|
|
244
|
+
jest.mocked(getConnectedDeviceIds).mockResolvedValue([]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
afterEach(() => {
|
|
248
|
+
Object.assign(Platform, { OS: 'ios', Version: undefined });
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test('keeps the color from an unnamed iOS advertisement for the named device', async () => {
|
|
252
|
+
const devices = await scanOnce([
|
|
253
|
+
{
|
|
254
|
+
id: 'COLOR-UNNAMED',
|
|
255
|
+
name: null,
|
|
256
|
+
localName: null,
|
|
257
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
258
|
+
manufacturerData: manufacturerData([0x42, 0, 0, 0, 0]),
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
id: 'COLOR-UNNAMED',
|
|
262
|
+
name: 'Pro 2 769D',
|
|
263
|
+
localName: 'Pro 2 769D',
|
|
264
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
265
|
+
manufacturerData: null,
|
|
266
|
+
},
|
|
267
|
+
]);
|
|
268
|
+
|
|
269
|
+
expect(devices).toHaveLength(1);
|
|
270
|
+
expect(devices[0]).toMatchObject({ id: 'COLOR-UNNAMED', bleColorCode: 'B' });
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test('fills a later color without letting a legacy payload clear it', async () => {
|
|
274
|
+
Object.assign(Platform, { OS: 'android', Version: 30 });
|
|
275
|
+
|
|
276
|
+
const devices = await scanOnce([
|
|
277
|
+
{
|
|
278
|
+
id: 'AA:BB:CC:00:00:01',
|
|
279
|
+
name: 'Neo 1A2B',
|
|
280
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
281
|
+
manufacturerData: null,
|
|
282
|
+
},
|
|
283
|
+
{
|
|
284
|
+
id: 'AA:BB:CC:00:00:01',
|
|
285
|
+
name: 'Neo 1A2B',
|
|
286
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
287
|
+
manufacturerData: manufacturerData([0x45, 0, 0, 0, 0]),
|
|
288
|
+
},
|
|
289
|
+
{
|
|
290
|
+
id: 'AA:BB:CC:00:00:01',
|
|
291
|
+
name: 'Neo 1A2B',
|
|
292
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
293
|
+
manufacturerData: manufacturerData([0, 0, 0, 0]),
|
|
294
|
+
},
|
|
295
|
+
]);
|
|
296
|
+
|
|
297
|
+
expect(devices).toHaveLength(1);
|
|
298
|
+
expect(devices[0]).toMatchObject({ id: 'AA:BB:CC:00:00:01', bleColorCode: 'E' });
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('reports no color for legacy, unset or undecodable payloads', async () => {
|
|
302
|
+
const devices = await scanOnce([
|
|
303
|
+
{
|
|
304
|
+
id: 'LEGACY-NONCE',
|
|
305
|
+
name: 'Pro 2 0001',
|
|
306
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
307
|
+
manufacturerData: manufacturerData([0, 0, 0, 0]),
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
id: 'SN-NOT-SYNCED',
|
|
311
|
+
name: 'Pro 2 0002',
|
|
312
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
313
|
+
manufacturerData: manufacturerData([0, 0, 0, 0, 0]),
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
id: 'INVALID-BASE64',
|
|
317
|
+
name: 'Pro 2 0003',
|
|
318
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
319
|
+
manufacturerData: '%%%',
|
|
320
|
+
},
|
|
321
|
+
]);
|
|
322
|
+
|
|
323
|
+
expect(devices.map(device => device.id)).toEqual([
|
|
324
|
+
'LEGACY-NONCE',
|
|
325
|
+
'SN-NOT-SYNCED',
|
|
326
|
+
'INVALID-BASE64',
|
|
327
|
+
]);
|
|
328
|
+
devices.forEach(device => expect(device).not.toHaveProperty('bleColorCode'));
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('reuses an earlier color for a connected peripheral that no longer advertises', async () => {
|
|
332
|
+
await scanOnce([
|
|
333
|
+
{
|
|
334
|
+
id: 'CONNECTED-NEO',
|
|
335
|
+
name: 'Neo 3C4D',
|
|
336
|
+
serviceUUIDs: PRO2_SERVICE_UUIDS,
|
|
337
|
+
manufacturerData: manufacturerData([0x46, 0, 0, 0, 0]),
|
|
338
|
+
},
|
|
339
|
+
]);
|
|
340
|
+
// iOS reports already-connected peripherals with lowercased ids.
|
|
341
|
+
jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([
|
|
342
|
+
{
|
|
343
|
+
id: 'connected-neo',
|
|
344
|
+
name: 'Neo 3C4D',
|
|
345
|
+
localName: null,
|
|
346
|
+
serviceUUIDs: [ONEKEY_SERVICE_UUID],
|
|
347
|
+
},
|
|
348
|
+
] as never);
|
|
349
|
+
|
|
350
|
+
const devices = await scanOnce([]);
|
|
351
|
+
|
|
352
|
+
expect(devices).toHaveLength(1);
|
|
353
|
+
expect(devices[0]).toMatchObject({ id: 'connected-neo', bleColorCode: 'F' });
|
|
354
|
+
});
|
|
355
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { getConnectedDeviceIds } from '../BleManager';
|
|
2
|
+
import ReactNativeBleTransport from '../index';
|
|
3
|
+
|
|
4
|
+
jest.mock('react-native', () => ({ PermissionsAndroid: {}, Platform: { OS: 'android' } }), {
|
|
5
|
+
virtual: true,
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
jest.mock('react-native-ble-plx', () => ({
|
|
9
|
+
BleError: class BleError extends Error {},
|
|
10
|
+
BleErrorCode: {},
|
|
11
|
+
BleManager: jest.fn(),
|
|
12
|
+
ConnectionPriority: { Balanced: 0, High: 1, LowPower: 2 },
|
|
13
|
+
ScanMode: { LowLatency: 2 },
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
jest.mock('../subscribeBleOn', () => ({
|
|
17
|
+
subscribeBleOn: jest.fn(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
jest.mock('@onekeyfe/react-native-ble-utils', () => ({
|
|
21
|
+
default: { getConnectedPeripherals: jest.fn(() => Promise.resolve([])) },
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
jest.mock('@onekeyfe/hd-transport', () => {
|
|
25
|
+
const actual = jest.requireActual('@onekeyfe/hd-transport');
|
|
26
|
+
return { ...actual, __esModule: true, default: actual };
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('CommonJS external module interop', () => {
|
|
30
|
+
test('reads the BLE utilities singleton from its default export', async () => {
|
|
31
|
+
await expect(getConnectedDeviceIds([])).resolves.toEqual([]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('configures both protocol schemas through the transport default export', () => {
|
|
35
|
+
const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
|
|
36
|
+
const schema = {
|
|
37
|
+
nested: {
|
|
38
|
+
Ping: { fields: {} },
|
|
39
|
+
MessageType: { values: { MessageType_Ping: 60206 } },
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
expect(() => transport.configure(schema)).not.toThrow();
|
|
44
|
+
expect(() => transport.configureProtocolV2(schema)).not.toThrow();
|
|
45
|
+
expect(transport.configured).toBe(true);
|
|
46
|
+
expect(transport._messagesV2).toBeDefined();
|
|
47
|
+
});
|
|
48
|
+
});
|