@onekeyfe/hd-transport-react-native 1.2.3-alpha.3 → 1.2.3-alpha.5
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/README.md +1 -1
- package/dist/BleManager.d.ts +1 -1
- package/dist/BleManager.d.ts.map +1 -1
- package/dist/bleNativeDisconnect.d.ts +3 -0
- package/dist/bleNativeDisconnect.d.ts.map +1 -0
- package/dist/index.d.ts +35 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +634 -152
- package/dist/subscribeBleOn.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/BleManager.ts +73 -5
- package/src/__tests__/bleNativeDisconnect.test.ts +54 -0
- package/src/__tests__/connectTimeout.test.ts +465 -3
- package/src/__tests__/enumerate.test.ts +36 -1
- package/src/__tests__/protocolV2Link.test.ts +976 -62
- package/src/__tests__/staleCallTimeout.test.ts +34 -5
- package/src/__tests__/subscribeBleOn.test.ts +111 -0
- package/src/bleNativeDisconnect.ts +41 -0
- package/src/index.ts +647 -159
- package/src/subscribeBleOn.ts +26 -10
|
@@ -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,CAmC9E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "1.2.3-alpha.
|
|
3
|
+
"version": "1.2.3-alpha.5",
|
|
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.3-alpha.
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.3-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.3-alpha.
|
|
26
|
-
"@onekeyfe/react-native-ble-utils": "
|
|
23
|
+
"@onekeyfe/hd-core": "1.2.3-alpha.5",
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.3-alpha.5",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.3-alpha.5",
|
|
26
|
+
"@onekeyfe/react-native-ble-utils": "0.1.6",
|
|
27
27
|
"react-native-ble-plx": "3.5.1"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "ee493c3ad41a6bc7fb249fba71d4b9bf8efcced8"
|
|
30
30
|
}
|
package/src/BleManager.ts
CHANGED
|
@@ -7,6 +7,19 @@ import type { Peripheral } from '@onekeyfe/react-native-ble-utils';
|
|
|
7
7
|
|
|
8
8
|
const Logger = bleLogger;
|
|
9
9
|
|
|
10
|
+
// Android BluetoothDevice.EXTRA_UNBOND_REASON values.
|
|
11
|
+
const bondFailureReasons: Record<number, string> = {
|
|
12
|
+
1: 'authentication_failed',
|
|
13
|
+
2: 'rejected',
|
|
14
|
+
3: 'canceled',
|
|
15
|
+
4: 'device_unreachable',
|
|
16
|
+
5: 'discovery_in_progress',
|
|
17
|
+
6: 'timeout',
|
|
18
|
+
7: 'repeated_attempts',
|
|
19
|
+
8: 'remote_canceled',
|
|
20
|
+
9: 'removed',
|
|
21
|
+
};
|
|
22
|
+
|
|
10
23
|
/**
|
|
11
24
|
* get the device basic info of connected devices
|
|
12
25
|
* @param serviceUuids
|
|
@@ -19,18 +32,37 @@ export const getBondedDevices = () => BleUtils.getBondedPeripherals();
|
|
|
19
32
|
|
|
20
33
|
export const pairDevice = (macAddress: string) => BleUtils.pairDevice(macAddress);
|
|
21
34
|
|
|
22
|
-
export const onDeviceBondState = (
|
|
35
|
+
export const onDeviceBondState = (
|
|
36
|
+
bleMacAddress: string,
|
|
37
|
+
signal?: AbortSignal
|
|
38
|
+
): Promise<Peripheral | undefined> =>
|
|
23
39
|
new Promise((resolve, reject) => {
|
|
40
|
+
if (signal?.aborted) {
|
|
41
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceDisconnected));
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
24
44
|
const cleanup = () => {
|
|
25
45
|
if (timeout) {
|
|
26
46
|
clearTimeout(timeout);
|
|
27
47
|
}
|
|
28
48
|
if (cleanupListener) cleanupListener();
|
|
49
|
+
signal?.removeEventListener('abort', onAbort);
|
|
50
|
+
};
|
|
51
|
+
const onAbort = () => {
|
|
52
|
+
cleanup();
|
|
53
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceDisconnected));
|
|
29
54
|
};
|
|
30
55
|
|
|
56
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
57
|
+
|
|
31
58
|
const timeout = setTimeout(() => {
|
|
32
59
|
cleanup();
|
|
33
|
-
reject(
|
|
60
|
+
reject(
|
|
61
|
+
ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'Bluetooth pairing timed out', {
|
|
62
|
+
phase: 'bond',
|
|
63
|
+
reason: 'timeout',
|
|
64
|
+
})
|
|
65
|
+
);
|
|
34
66
|
}, 60 * 1000);
|
|
35
67
|
|
|
36
68
|
const cleanupListener = BleUtils.onDeviceBondState(peripheral => {
|
|
@@ -40,14 +72,50 @@ export const onDeviceBondState = (bleMacAddress: string): Promise<Peripheral | u
|
|
|
40
72
|
const { bondState } = peripheral;
|
|
41
73
|
|
|
42
74
|
const hasBonded = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_BONDED';
|
|
43
|
-
const
|
|
75
|
+
const hasFailed = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_NONE';
|
|
44
76
|
Logger.debug('onDeviceBondState bondState:', bondState);
|
|
45
77
|
if (hasBonded) {
|
|
46
78
|
cleanup();
|
|
47
79
|
resolve(peripheral);
|
|
48
|
-
} else if (
|
|
80
|
+
} else if (hasFailed) {
|
|
49
81
|
cleanup();
|
|
50
|
-
|
|
82
|
+
const nativeReason =
|
|
83
|
+
'reason' in bondState && typeof bondState.reason === 'number'
|
|
84
|
+
? bondState.reason
|
|
85
|
+
: undefined;
|
|
86
|
+
const reason =
|
|
87
|
+
nativeReason === undefined ? 'unknown' : bondFailureReasons[nativeReason] ?? 'unknown';
|
|
88
|
+
const params = {
|
|
89
|
+
phase: 'bond',
|
|
90
|
+
reason,
|
|
91
|
+
...(nativeReason === undefined ? {} : { nativeReason }),
|
|
92
|
+
};
|
|
93
|
+
if (reason === 'timeout') {
|
|
94
|
+
// Connection timeouts are retried by Core; pairing requires a new user attempt.
|
|
95
|
+
reject(
|
|
96
|
+
ERRORS.TypedError(
|
|
97
|
+
HardwareErrorCode.BleDeviceNotBonded,
|
|
98
|
+
'Bluetooth pairing timed out',
|
|
99
|
+
params
|
|
100
|
+
)
|
|
101
|
+
);
|
|
102
|
+
} else if (reason === 'canceled') {
|
|
103
|
+
reject(
|
|
104
|
+
ERRORS.TypedError(
|
|
105
|
+
HardwareErrorCode.BleDeviceBondedCanceled,
|
|
106
|
+
'Bluetooth pairing canceled',
|
|
107
|
+
params
|
|
108
|
+
)
|
|
109
|
+
);
|
|
110
|
+
} else {
|
|
111
|
+
reject(
|
|
112
|
+
ERRORS.TypedError(
|
|
113
|
+
HardwareErrorCode.BleDeviceNotBonded,
|
|
114
|
+
'Bluetooth pairing failed',
|
|
115
|
+
params
|
|
116
|
+
)
|
|
117
|
+
);
|
|
118
|
+
}
|
|
51
119
|
}
|
|
52
120
|
});
|
|
53
121
|
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
+
|
|
3
|
+
import { isNativeBleDisconnectError, toBleDisconnectHardwareError } from '../bleNativeDisconnect';
|
|
4
|
+
|
|
5
|
+
describe('native BLE disconnect mapping', () => {
|
|
6
|
+
test.each([
|
|
7
|
+
[{ errorCode: 201, reason: 'The specified device has disconnected from us.' }],
|
|
8
|
+
[{ errorCode: 201, iosErrorCode: 7, reason: 'The specified device has disconnected from us.' }],
|
|
9
|
+
[{ iosErrorCode: 7, reason: 'Peripheral disconnected' }],
|
|
10
|
+
])('recognizes %j as a native disconnect', nativeError => {
|
|
11
|
+
expect(isNativeBleDisconnectError(nativeError)).toBe(true);
|
|
12
|
+
expect(toBleDisconnectHardwareError(nativeError)).toMatchObject({
|
|
13
|
+
errorCode: HardwareErrorCode.BleDeviceDisconnected,
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('does not treat a stale-bond iOS code as a disconnect', () => {
|
|
18
|
+
expect(
|
|
19
|
+
isNativeBleDisconnectError({
|
|
20
|
+
iosErrorCode: 14,
|
|
21
|
+
reason: 'Peer removed pairing information',
|
|
22
|
+
})
|
|
23
|
+
).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('does not treat an already-canonical unpaired error as a native disconnect', () => {
|
|
27
|
+
expect(
|
|
28
|
+
isNativeBleDisconnectError({
|
|
29
|
+
errorCode: HardwareErrorCode.BleDeviceNotBonded,
|
|
30
|
+
})
|
|
31
|
+
).toBe(false);
|
|
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
|
+
});
|
|
54
|
+
});
|