@ukeyfe/hardware-transport-react-native 1.1.13
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 +29 -0
- package/dist/BleManager.d.ts +9 -0
- package/dist/BleManager.d.ts.map +1 -0
- package/dist/BleTransport.d.ts +15 -0
- package/dist/BleTransport.d.ts.map +1 -0
- package/dist/constants.d.ts +9 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +724 -0
- package/dist/subscribeBleOn.d.ts +3 -0
- package/dist/subscribeBleOn.d.ts.map +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/timer.d.ts +5 -0
- package/dist/utils/timer.d.ts.map +1 -0
- package/dist/utils/validateNotify.d.ts +3 -0
- package/dist/utils/validateNotify.d.ts.map +1 -0
- package/package.json +27 -0
- package/src/BleManager.ts +54 -0
- package/src/BleTransport.ts +71 -0
- package/src/constants.ts +43 -0
- package/src/index.ts +672 -0
- package/src/subscribeBleOn.ts +26 -0
- package/src/types.ts +10 -0
- package/src/utils/timer.ts +24 -0
- package/src/utils/validateNotify.ts +16 -0
- package/tsconfig.json +7 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGxC,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAqB9E,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +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;CAChC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timer.d.ts","sourceRoot":"","sources":["../../src/utils/timer.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,KAAK;4BAGqB,MAAM,GAAG,CAAC,KAAK,GAAG,MAAM,MAAM;CAkBvD,CAAC;AAER,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateNotify.d.ts","sourceRoot":"","sources":["../../src/utils/validateNotify.ts"],"names":[],"mappings":";AAEA,eAAO,MAAM,aAAa,UAAW,MAAM,KAAG,OAa7C,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ukeyfe/hardware-transport-react-native",
|
|
3
|
+
"version": "1.1.13",
|
|
4
|
+
"homepage": "https://github.com/UKeyHQ/hardware-js-sdk#readme",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/bestyourwallet/hardware-js-sdk"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
|
|
17
|
+
"build": "rimraf dist && rollup -c ../../build/rollup.config.js",
|
|
18
|
+
"lint": "eslint .",
|
|
19
|
+
"lint:fix": "eslint . --fix"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@ukeyfe/hardware-shared": "1.1.13",
|
|
23
|
+
"@ukeyfe/hardware-transport": "1.1.13",
|
|
24
|
+
"@ukeyfe/react-native-ble-utils": "^0.1.5",
|
|
25
|
+
"react-native-ble-plx": "3.5.0"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import BleUtils, { Peripheral } from '@ukeyfe/react-native-ble-utils';
|
|
2
|
+
import { ERRORS, HardwareErrorCode } from '@ukeyfe/hardware-shared';
|
|
3
|
+
import { getLogger, LoggerNames } from '@ukeyfe/hardware-core';
|
|
4
|
+
|
|
5
|
+
const Logger = getLogger(LoggerNames.HdBleTransport);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* get the device basic info of connected devices
|
|
9
|
+
* @param serviceUuids
|
|
10
|
+
* @returns {Promise<[string[]]>}
|
|
11
|
+
*/
|
|
12
|
+
export const getConnectedDeviceIds = (serviceUuids: string[]) =>
|
|
13
|
+
BleUtils.getConnectedPeripherals(serviceUuids);
|
|
14
|
+
|
|
15
|
+
export const getBondedDevices = () => BleUtils.getBondedPeripherals();
|
|
16
|
+
|
|
17
|
+
export const pairDevice = (macAddress: string) => BleUtils.pairDevice(macAddress);
|
|
18
|
+
|
|
19
|
+
export const onDeviceBondState = (bleMacAddress: string): Promise<Peripheral | undefined> =>
|
|
20
|
+
new Promise((resolve, reject) => {
|
|
21
|
+
let timeout: any | undefined;
|
|
22
|
+
|
|
23
|
+
const cleanup = (cleanupListener: (() => void) | undefined) => {
|
|
24
|
+
if (timeout) {
|
|
25
|
+
clearTimeout(timeout);
|
|
26
|
+
}
|
|
27
|
+
if (cleanupListener) cleanupListener();
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const cleanupListener = BleUtils.onDeviceBondState(peripheral => {
|
|
31
|
+
if (peripheral.id?.toLowerCase() !== bleMacAddress.toLowerCase()) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const { bondState } = peripheral;
|
|
35
|
+
|
|
36
|
+
if (bondState.preState === 'BOND_NONE' && bondState.state === 'BOND_BONDING') {
|
|
37
|
+
timeout = setTimeout(() => {
|
|
38
|
+
cleanup(cleanupListener);
|
|
39
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded'));
|
|
40
|
+
}, 60 * 1000);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const hasBonded = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_BONDED';
|
|
44
|
+
const hasCanceled = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_NONE';
|
|
45
|
+
Logger.debug('onDeviceBondState bondState:', bondState);
|
|
46
|
+
if (hasBonded) {
|
|
47
|
+
cleanup(cleanupListener);
|
|
48
|
+
resolve(peripheral);
|
|
49
|
+
} else if (hasCanceled) {
|
|
50
|
+
cleanup(cleanupListener);
|
|
51
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceBondedCanceled, 'bonding canceled'));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Device, Characteristic, BleErrorCode } from 'react-native-ble-plx';
|
|
2
|
+
import { getLogger, LoggerNames, wait } from '@ukeyfe/hardware-core';
|
|
3
|
+
// import { wait } from '@ukeyfe/hardware-core/src/utils';
|
|
4
|
+
|
|
5
|
+
const Log = getLogger(LoggerNames.HdBleTransport);
|
|
6
|
+
|
|
7
|
+
export default class BleTransport {
|
|
8
|
+
id: string;
|
|
9
|
+
|
|
10
|
+
name = 'ReactNativeBleTransport';
|
|
11
|
+
|
|
12
|
+
device: Device;
|
|
13
|
+
|
|
14
|
+
mtuSize = 20;
|
|
15
|
+
|
|
16
|
+
writeCharacteristic: Characteristic;
|
|
17
|
+
|
|
18
|
+
notifyCharacteristic: Characteristic;
|
|
19
|
+
|
|
20
|
+
nofitySubscription?: () => void;
|
|
21
|
+
|
|
22
|
+
static MAX_RETRIES = 5;
|
|
23
|
+
|
|
24
|
+
static RETRY_DELAY = 2000;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
device: Device,
|
|
28
|
+
writeCharacteristic: Characteristic,
|
|
29
|
+
notifyCharacteristic: Characteristic
|
|
30
|
+
) {
|
|
31
|
+
this.id = device.id;
|
|
32
|
+
this.device = device;
|
|
33
|
+
this.writeCharacteristic = writeCharacteristic;
|
|
34
|
+
this.notifyCharacteristic = notifyCharacteristic;
|
|
35
|
+
console.log(`BleTransport(${String(this.id)}) new instance`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @description only for pro / touch , while upgrade firmware
|
|
40
|
+
* @param data
|
|
41
|
+
* @param retryCount
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
async writeWithRetry(data: string, retryCount = BleTransport.MAX_RETRIES): Promise<void> {
|
|
45
|
+
try {
|
|
46
|
+
await this.writeCharacteristic.writeWithoutResponse(data);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
Log?.debug(
|
|
49
|
+
`Write retry attempt ${BleTransport.MAX_RETRIES - retryCount + 1}, error: ${error}`
|
|
50
|
+
);
|
|
51
|
+
if (retryCount > 0) {
|
|
52
|
+
await wait(BleTransport.RETRY_DELAY);
|
|
53
|
+
if (
|
|
54
|
+
error.errorCode === BleErrorCode.DeviceDisconnected ||
|
|
55
|
+
error.errorCode === BleErrorCode.CharacteristicNotFound
|
|
56
|
+
) {
|
|
57
|
+
try {
|
|
58
|
+
await this.device.connect();
|
|
59
|
+
await this.device.discoverAllServicesAndCharacteristics();
|
|
60
|
+
} catch (e) {
|
|
61
|
+
Log?.debug(`Connect or discoverAllServicesAndCharacteristics error: ${e}`);
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
Log?.debug(`writeCharacteristic error: ${error}`);
|
|
65
|
+
}
|
|
66
|
+
return this.writeWithRetry(data, retryCount - 1);
|
|
67
|
+
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const IOS_PACKET_LENGTH = 128;
|
|
2
|
+
export const ANDROID_PACKET_LENGTH = 192;
|
|
3
|
+
|
|
4
|
+
type BluetoothServices = Record<
|
|
5
|
+
string,
|
|
6
|
+
{
|
|
7
|
+
serviceUuid: string;
|
|
8
|
+
writeUuid?: string;
|
|
9
|
+
notifyUuid?: string;
|
|
10
|
+
}
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
const ClassicServiceUUID = '00000001-0000-1000-8000-00805f9b34fb';
|
|
14
|
+
|
|
15
|
+
const UKeyServices: Record<string, BluetoothServices> = {
|
|
16
|
+
classic: {
|
|
17
|
+
[ClassicServiceUUID]: {
|
|
18
|
+
serviceUuid: ClassicServiceUUID,
|
|
19
|
+
writeUuid: '00000002-0000-1000-8000-00805f9b34fb',
|
|
20
|
+
notifyUuid: '00000003-0000-1000-8000-00805f9b34fb',
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const bluetoothServices: string[] = [];
|
|
26
|
+
|
|
27
|
+
for (const deviceType of Object.keys(UKeyServices)) {
|
|
28
|
+
const services = UKeyServices[deviceType];
|
|
29
|
+
bluetoothServices.push(...Object.keys(services));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const getBluetoothServiceUuids = () => bluetoothServices;
|
|
33
|
+
export const getInfosForServiceUuid = (serviceUuid: string, deviceType: 'classic') => {
|
|
34
|
+
const services = UKeyServices[deviceType];
|
|
35
|
+
if (!services) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const service = services[serviceUuid];
|
|
39
|
+
if (!service) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return service;
|
|
43
|
+
};
|