@onekeyfe/hd-transport-react-native 1.1.27-patch.1 → 1.2.0-alpha.0
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.map +1 -1
- package/dist/BleTransport.d.ts +2 -0
- package/dist/BleTransport.d.ts.map +1 -1
- package/dist/bleStrategy.d.ts +15 -0
- package/dist/bleStrategy.d.ts.map +1 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +58 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +814 -122
- package/dist/logger.d.ts +14 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/BleManager.ts +11 -14
- package/src/BleTransport.ts +9 -5
- package/src/__tests__/bleStrategy.test.ts +47 -0
- package/src/bleStrategy.ts +60 -0
- package/src/constants.ts +25 -1
- package/src/index.ts +888 -65
- package/src/logger.ts +19 -0
- package/src/types.ts +3 -0
- package/src/utils/validateNotify.ts +4 -4
package/dist/logger.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type LogMethod = (...args: unknown[]) => void;
|
|
2
|
+
type TransportLogger = {
|
|
3
|
+
debug?: LogMethod;
|
|
4
|
+
error?: LogMethod;
|
|
5
|
+
warn?: LogMethod;
|
|
6
|
+
};
|
|
7
|
+
export declare const setBleLogger: (logger?: TransportLogger) => void;
|
|
8
|
+
export declare const bleLogger: {
|
|
9
|
+
debug: (...args: unknown[]) => void | undefined;
|
|
10
|
+
error: (...args: unknown[]) => void | undefined;
|
|
11
|
+
warn: (...args: unknown[]) => void | undefined;
|
|
12
|
+
};
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=logger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,KAAK,SAAS,GAAG,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;AAE9C,KAAK,eAAe,GAAG;IACrB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,IAAI,CAAC,EAAE,SAAS,CAAC;CAClB,CAAC;AAIF,eAAO,MAAM,YAAY,YAAa,eAAe,SAEpD,CAAC;AAEF,eAAO,MAAM,SAAS;qBACH,OAAO,EAAE;qBACT,OAAO,EAAE;oBACV,OAAO,EAAE;CAC1B,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ProtocolType } from '@onekeyfe/hd-transport';
|
|
1
2
|
export type { BleManager as BlePlxManager } from 'react-native-ble-plx';
|
|
2
3
|
export type TransportOptions = {
|
|
3
4
|
scanTimeout?: number;
|
|
@@ -5,5 +6,6 @@ export type TransportOptions = {
|
|
|
5
6
|
export type BleAcquireInput = {
|
|
6
7
|
uuid: string;
|
|
7
8
|
forceCleanRunPromise?: boolean;
|
|
9
|
+
expectedProtocol?: ProtocolType;
|
|
8
10
|
};
|
|
9
11
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAExE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,YAAY,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAExE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,gBAAgB,CAAC,EAAE,YAAY,CAAC;CACjC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-alpha.0",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -19,11 +19,11 @@
|
|
|
19
19
|
"lint:fix": "eslint . --fix"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@onekeyfe/hd-core": "1.
|
|
23
|
-
"@onekeyfe/hd-shared": "1.
|
|
24
|
-
"@onekeyfe/hd-transport": "1.
|
|
22
|
+
"@onekeyfe/hd-core": "1.2.0-alpha.0",
|
|
23
|
+
"@onekeyfe/hd-shared": "1.2.0-alpha.0",
|
|
24
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.0",
|
|
25
25
|
"@onekeyfe/react-native-ble-utils": "^0.1.4",
|
|
26
26
|
"react-native-ble-plx": "3.5.1"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "de9efe20b83666b641ab9e8d945e6350f6450904"
|
|
29
29
|
}
|
package/src/BleManager.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import BleUtils from '@onekeyfe/react-native-ble-utils';
|
|
2
2
|
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
import { bleLogger } from './logger';
|
|
4
5
|
|
|
5
6
|
import type { Peripheral } from '@onekeyfe/react-native-ble-utils';
|
|
6
7
|
|
|
7
|
-
const Logger =
|
|
8
|
+
const Logger = bleLogger;
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* get the device basic info of connected devices
|
|
@@ -20,36 +21,32 @@ export const pairDevice = (macAddress: string) => BleUtils.pairDevice(macAddress
|
|
|
20
21
|
|
|
21
22
|
export const onDeviceBondState = (bleMacAddress: string): Promise<Peripheral | undefined> =>
|
|
22
23
|
new Promise((resolve, reject) => {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const cleanup = (cleanupListener: (() => void) | undefined) => {
|
|
24
|
+
const cleanup = () => {
|
|
26
25
|
if (timeout) {
|
|
27
26
|
clearTimeout(timeout);
|
|
28
27
|
}
|
|
29
28
|
if (cleanupListener) cleanupListener();
|
|
30
29
|
};
|
|
31
30
|
|
|
31
|
+
const timeout = setTimeout(() => {
|
|
32
|
+
cleanup();
|
|
33
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded'));
|
|
34
|
+
}, 60 * 1000);
|
|
35
|
+
|
|
32
36
|
const cleanupListener = BleUtils.onDeviceBondState(peripheral => {
|
|
33
37
|
if (peripheral.id?.toLowerCase() !== bleMacAddress.toLowerCase()) {
|
|
34
38
|
return;
|
|
35
39
|
}
|
|
36
40
|
const { bondState } = peripheral;
|
|
37
41
|
|
|
38
|
-
if (bondState.preState === 'BOND_NONE' && bondState.state === 'BOND_BONDING') {
|
|
39
|
-
timeout = setTimeout(() => {
|
|
40
|
-
cleanup(cleanupListener);
|
|
41
|
-
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded'));
|
|
42
|
-
}, 60 * 1000);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
42
|
const hasBonded = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_BONDED';
|
|
46
43
|
const hasCanceled = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_NONE';
|
|
47
44
|
Logger.debug('onDeviceBondState bondState:', bondState);
|
|
48
45
|
if (hasBonded) {
|
|
49
|
-
cleanup(
|
|
46
|
+
cleanup();
|
|
50
47
|
resolve(peripheral);
|
|
51
48
|
} else if (hasCanceled) {
|
|
52
|
-
cleanup(
|
|
49
|
+
cleanup();
|
|
53
50
|
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceBondedCanceled, 'bonding canceled'));
|
|
54
51
|
}
|
|
55
52
|
});
|
package/src/BleTransport.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { BleErrorCode } from 'react-native-ble-plx';
|
|
2
|
-
import {
|
|
2
|
+
import { wait } from '@onekeyfe/hd-shared';
|
|
3
|
+
|
|
4
|
+
import { bleLogger } from './logger';
|
|
3
5
|
|
|
4
6
|
import type { Characteristic, Device, Subscription } from 'react-native-ble-plx';
|
|
5
|
-
// import { wait } from '@onekeyfe/hd-core/src/utils';
|
|
6
7
|
|
|
7
|
-
const Log =
|
|
8
|
+
const Log = bleLogger;
|
|
8
9
|
|
|
9
10
|
export default class BleTransport {
|
|
10
11
|
id: string;
|
|
@@ -13,7 +14,7 @@ export default class BleTransport {
|
|
|
13
14
|
|
|
14
15
|
device: Device;
|
|
15
16
|
|
|
16
|
-
mtuSize =
|
|
17
|
+
mtuSize = 23;
|
|
17
18
|
|
|
18
19
|
writeCharacteristic: Characteristic;
|
|
19
20
|
|
|
@@ -23,6 +24,10 @@ export default class BleTransport {
|
|
|
23
24
|
|
|
24
25
|
disconnectSubscription?: Subscription;
|
|
25
26
|
|
|
27
|
+
notifyTransactionId?: string;
|
|
28
|
+
|
|
29
|
+
monitorToken?: number;
|
|
30
|
+
|
|
26
31
|
static MAX_RETRIES = 5;
|
|
27
32
|
|
|
28
33
|
static RETRY_DELAY = 2000;
|
|
@@ -36,7 +41,6 @@ export default class BleTransport {
|
|
|
36
41
|
this.device = device;
|
|
37
42
|
this.writeCharacteristic = writeCharacteristic;
|
|
38
43
|
this.notifyCharacteristic = notifyCharacteristic;
|
|
39
|
-
console.log(`BleTransport(${String(this.id)}) new instance`);
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
/**
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {
|
|
2
|
+
hasWritableCapability,
|
|
3
|
+
resolveBleWriteMode,
|
|
4
|
+
resolveProtocolV2PacketCapacity,
|
|
5
|
+
} from '../bleStrategy';
|
|
6
|
+
|
|
7
|
+
describe('React Native BLE strategy', () => {
|
|
8
|
+
test('accepts writeWithoutResponse-only characteristics', () => {
|
|
9
|
+
const characteristic = {
|
|
10
|
+
isWritableWithResponse: false,
|
|
11
|
+
isWritableWithoutResponse: true,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
expect(hasWritableCapability(characteristic)).toBe(true);
|
|
15
|
+
expect(resolveBleWriteMode(characteristic)).toBe('withoutResponse');
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test('falls back to Android default ATT payload when MTU is unavailable', () => {
|
|
19
|
+
expect(
|
|
20
|
+
resolveProtocolV2PacketCapacity({
|
|
21
|
+
platform: 'android',
|
|
22
|
+
androidPacketLength: 192,
|
|
23
|
+
mtu: null,
|
|
24
|
+
})
|
|
25
|
+
).toBe(20);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('caps Android packet length by negotiated MTU payload', () => {
|
|
29
|
+
expect(
|
|
30
|
+
resolveProtocolV2PacketCapacity({
|
|
31
|
+
platform: 'android',
|
|
32
|
+
androidPacketLength: 192,
|
|
33
|
+
mtu: 100,
|
|
34
|
+
})
|
|
35
|
+
).toBe(97);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('keeps iOS packet length controlled by tuning profile', () => {
|
|
39
|
+
expect(
|
|
40
|
+
resolveProtocolV2PacketCapacity({
|
|
41
|
+
platform: 'ios',
|
|
42
|
+
iosPacketLength: 244,
|
|
43
|
+
mtu: 256,
|
|
44
|
+
})
|
|
45
|
+
).toBe(244);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ANDROID_DEFAULT_MTU, ANDROID_PACKET_LENGTH, IOS_PACKET_LENGTH } from './constants';
|
|
2
|
+
|
|
3
|
+
export type BlePlatform = 'ios' | 'android' | string;
|
|
4
|
+
|
|
5
|
+
export type BleWriteCapability = {
|
|
6
|
+
isWritableWithResponse?: boolean | null;
|
|
7
|
+
isWritableWithoutResponse?: boolean | null;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type BleWriteMode = 'withResponse' | 'withoutResponse';
|
|
11
|
+
|
|
12
|
+
export function hasWritableCapability(characteristic: BleWriteCapability) {
|
|
13
|
+
return !!(characteristic.isWritableWithResponse || characteristic.isWritableWithoutResponse);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function resolveBleWriteMode(
|
|
17
|
+
characteristic: BleWriteCapability,
|
|
18
|
+
preferredMode: BleWriteMode = 'withoutResponse'
|
|
19
|
+
): BleWriteMode {
|
|
20
|
+
if (preferredMode === 'withoutResponse' && characteristic.isWritableWithoutResponse) {
|
|
21
|
+
return 'withoutResponse';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (preferredMode === 'withResponse' && characteristic.isWritableWithResponse) {
|
|
25
|
+
return 'withResponse';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (characteristic.isWritableWithoutResponse) {
|
|
29
|
+
return 'withoutResponse';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (characteristic.isWritableWithResponse) {
|
|
33
|
+
return 'withResponse';
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return preferredMode;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function resolveProtocolV2PacketCapacity({
|
|
40
|
+
platform,
|
|
41
|
+
iosPacketLength = IOS_PACKET_LENGTH,
|
|
42
|
+
androidPacketLength = ANDROID_PACKET_LENGTH,
|
|
43
|
+
mtu,
|
|
44
|
+
}: {
|
|
45
|
+
platform: BlePlatform;
|
|
46
|
+
iosPacketLength?: number;
|
|
47
|
+
androidPacketLength?: number;
|
|
48
|
+
mtu?: number | null;
|
|
49
|
+
}) {
|
|
50
|
+
if (platform === 'ios') {
|
|
51
|
+
return iosPacketLength;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (platform === 'android') {
|
|
55
|
+
const payloadLength = Math.max((mtu ?? ANDROID_DEFAULT_MTU) - 3, 1);
|
|
56
|
+
return Math.min(androidPacketLength, payloadLength);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return androidPacketLength;
|
|
60
|
+
}
|
package/src/constants.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export const IOS_PACKET_LENGTH = 128;
|
|
2
2
|
export const ANDROID_PACKET_LENGTH = 192;
|
|
3
|
+
export const ANDROID_DEFAULT_MTU = 23;
|
|
3
4
|
|
|
4
5
|
type BluetoothServices = Record<
|
|
5
6
|
string,
|
|
@@ -35,9 +36,32 @@ export const getInfosForServiceUuid = (serviceUuid: string, deviceType: 'classic
|
|
|
35
36
|
if (!services) {
|
|
36
37
|
return null;
|
|
37
38
|
}
|
|
38
|
-
const
|
|
39
|
+
const normalizedServiceUuid = normalizeBleUuid(serviceUuid);
|
|
40
|
+
const service =
|
|
41
|
+
services[serviceUuid] ??
|
|
42
|
+
Object.values(services).find(
|
|
43
|
+
item => normalizeBleUuid(item.serviceUuid) === normalizedServiceUuid
|
|
44
|
+
);
|
|
39
45
|
if (!service) {
|
|
40
46
|
return null;
|
|
41
47
|
}
|
|
42
48
|
return service;
|
|
43
49
|
};
|
|
50
|
+
|
|
51
|
+
export const normalizeBleUuid = (uuid?: string | null) =>
|
|
52
|
+
(uuid ?? '').replace(/-/g, '').toLowerCase();
|
|
53
|
+
|
|
54
|
+
export const getBleUuidKey = (uuid?: string | null) => {
|
|
55
|
+
const normalized = normalizeBleUuid(uuid);
|
|
56
|
+
return normalized.length >= 8 ? normalized.substring(4, 8) : normalized;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const isSameBleUuid = (left?: string | null, right?: string | null) => {
|
|
60
|
+
const normalizedLeft = normalizeBleUuid(left);
|
|
61
|
+
const normalizedRight = normalizeBleUuid(right);
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
normalizedLeft === normalizedRight ||
|
|
65
|
+
(getBleUuidKey(left) !== '' && getBleUuidKey(left) === getBleUuidKey(right))
|
|
66
|
+
);
|
|
67
|
+
};
|