@onekeyfe/hd-transport-react-native 0.0.4 → 0.0.7
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/constants.d.ts +2 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +68 -52
- package/dist/subscribeBleOn.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/constants.ts +5 -2
- package/src/index.ts +36 -12
- package/src/subscribeBleOn.ts +3 -5
package/dist/constants.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export declare const PERMISSION_ERROR = "Bluetooth required to be turned on";
|
|
2
|
+
export declare const LOCATION_ERROR = "Device is not authorized to use BluetoothLE";
|
|
1
3
|
export declare const isOnekeyDevice: (name: string | null, id?: string | undefined) => boolean;
|
|
2
4
|
export declare const getBluetoothServiceUuids: () => string[];
|
|
3
5
|
export declare const getInfosForServiceUuid: (serviceUuid: string, deviceType: 'classic') => {
|
package/dist/constants.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,SAAU,MAAM,GAAG,IAAI,8BAAgB,OAYjE,CAAC;AA8BF,eAAO,MAAM,wBAAwB,gBAA0B,CAAC;AAChE,eAAO,MAAM,sBAAsB,gBAAiB,MAAM,cAAc,SAAS;iBA1BhE,MAAM;;;QAoCtB,CAAC"}
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,uCAAuC,CAAC;AACrE,eAAO,MAAM,cAAc,gDAAgD,CAAC;AAE5E,eAAO,MAAM,cAAc,SAAU,MAAM,GAAG,IAAI,8BAAgB,OAYjE,CAAC;AA8BF,eAAO,MAAM,wBAAwB,gBAA0B,CAAC;AAChE,eAAO,MAAM,sBAAsB,gBAAiB,MAAM,cAAc,SAAS;iBA1BhE,MAAM;;;QAoCtB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import transport from '@onekeyfe/hd-transport';
|
|
2
|
-
import { Device, Characteristic } from 'react-native-ble-plx';
|
|
2
|
+
import { BleManager, Device, Characteristic } from 'react-native-ble-plx';
|
|
3
|
+
|
|
4
|
+
declare const PERMISSION_ERROR = "Bluetooth required to be turned on";
|
|
5
|
+
declare const LOCATION_ERROR = "Device is not authorized to use BluetoothLE";
|
|
3
6
|
|
|
4
7
|
declare type Deferred<T, I = any, D = any> = {
|
|
5
8
|
id?: I;
|
|
@@ -17,6 +20,7 @@ declare type BleAcquireInput = {
|
|
|
17
20
|
};
|
|
18
21
|
|
|
19
22
|
declare class ReactNativeBleTransport {
|
|
23
|
+
blePlxManager: BleManager | undefined;
|
|
20
24
|
_messages: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
21
25
|
configured: boolean;
|
|
22
26
|
stopped: boolean;
|
|
@@ -26,6 +30,7 @@ declare class ReactNativeBleTransport {
|
|
|
26
30
|
init(): void;
|
|
27
31
|
configure(signedData: any): void;
|
|
28
32
|
listen(): void;
|
|
33
|
+
getPlxManager(): Promise<BleManager>;
|
|
29
34
|
enumerate(): Promise<Device[]>;
|
|
30
35
|
acquire(input: BleAcquireInput): Promise<{
|
|
31
36
|
uuid: string;
|
|
@@ -36,4 +41,4 @@ declare class ReactNativeBleTransport {
|
|
|
36
41
|
stop(): void;
|
|
37
42
|
}
|
|
38
43
|
|
|
39
|
-
export { ReactNativeBleTransport as default };
|
|
44
|
+
export { LOCATION_ERROR, PERMISSION_ERROR, ReactNativeBleTransport as default };
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,SAAiC,MAAM,wBAAwB,CAAC;AACvE,OAAO,EACL,UAAU,IAAI,aAAa,EAC3B,MAAM,EAEN,cAAc,EAEf,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACL,gBAAgB,EAChB,cAAc,EAIf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,QAAQ,EAA4B,MAAM,kBAAkB,CAAC;AAItE,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAmBjE,MAAM,CAAC,OAAO,OAAO,uBAAuB;IAC1C,aAAa,EAAE,aAAa,GAAG,SAAS,CAAC;IAEzC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEnE,UAAU,UAAS;IAEnB,OAAO,UAAS;IAEhB,WAAW,SAAQ;IAEnB,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAQ;gBAE5B,OAAO,EAAE,gBAAgB;IAIrC,IAAI;IAEJ,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,MAAM;IAIN,aAAa,IAAI,OAAO,CAAC,aAAa,CAAC;IAYjC,SAAS;IAoET,OAAO,CAAC,KAAK,EAAE,eAAe;;;IAkJpC,sBAAsB,CAAC,cAAc,EAAE,cAAc;IA+C/C,OAAO,CAAC,IAAI,EAAE,MAAM;IAepB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAmDpE,IAAI;CAGL;AAED,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
3
5
|
var buffer = require('buffer');
|
|
4
6
|
var transport = require('@onekeyfe/hd-transport');
|
|
5
7
|
var reactNativeBlePlx = require('react-native-ble-plx');
|
|
@@ -46,6 +48,47 @@ const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0
|
|
|
46
48
|
});
|
|
47
49
|
});
|
|
48
50
|
|
|
51
|
+
const PERMISSION_ERROR = 'Bluetooth required to be turned on';
|
|
52
|
+
const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
|
|
53
|
+
const isOnekeyDevice = (name, id) => {
|
|
54
|
+
var _a;
|
|
55
|
+
if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
const re = /(BixinKey\d{10})|(K\d{4})|(T\d{4})/i;
|
|
59
|
+
if (name && re.exec(name)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
};
|
|
64
|
+
const ClassicServiceUUID = '00000001-0000-1000-8000-00805f9b34fb';
|
|
65
|
+
const OneKeyServices = {
|
|
66
|
+
classic: {
|
|
67
|
+
[ClassicServiceUUID]: {
|
|
68
|
+
serviceUuid: ClassicServiceUUID,
|
|
69
|
+
writeUuid: '00000002-0000-1000-8000-00805f9b34fb',
|
|
70
|
+
notifyUuid: '00000003-0000-1000-8000-00805f9b34fb',
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
const bluetoothServices = [];
|
|
75
|
+
for (const deviceType of Object.keys(OneKeyServices)) {
|
|
76
|
+
const services = OneKeyServices[deviceType];
|
|
77
|
+
bluetoothServices.push(...Object.keys(services));
|
|
78
|
+
}
|
|
79
|
+
const getBluetoothServiceUuids = () => bluetoothServices;
|
|
80
|
+
const getInfosForServiceUuid = (serviceUuid, deviceType) => {
|
|
81
|
+
const services = OneKeyServices[deviceType];
|
|
82
|
+
if (!services) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const service = services[serviceUuid];
|
|
86
|
+
if (!service) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
return service;
|
|
90
|
+
};
|
|
91
|
+
|
|
49
92
|
const timer = process.env.NODE_ENV === 'development'
|
|
50
93
|
? {
|
|
51
94
|
timeout: (fn, ms) => {
|
|
@@ -68,11 +111,9 @@ const timer = process.env.NODE_ENV === 'development'
|
|
|
68
111
|
},
|
|
69
112
|
};
|
|
70
113
|
|
|
71
|
-
const subscribeBleOn = (bleManager, ms =
|
|
114
|
+
const subscribeBleOn = (bleManager, ms = 1000) => new Promise((resolve, reject) => {
|
|
72
115
|
let done = false;
|
|
73
|
-
let lastState = 'Unknown';
|
|
74
116
|
const subscription = bleManager.onStateChange(state => {
|
|
75
|
-
lastState = state;
|
|
76
117
|
console.log('ble state -> ', state);
|
|
77
118
|
if (state === 'PoweredOn') {
|
|
78
119
|
if (done)
|
|
@@ -87,49 +128,10 @@ const subscribeBleOn = (bleManager, ms = 3000) => new Promise((resolve, reject)
|
|
|
87
128
|
if (done)
|
|
88
129
|
return;
|
|
89
130
|
subscription.remove();
|
|
90
|
-
reject(new Error(
|
|
131
|
+
reject(new Error(PERMISSION_ERROR));
|
|
91
132
|
}, ms);
|
|
92
133
|
});
|
|
93
134
|
|
|
94
|
-
const isOnekeyDevice = (name, id) => {
|
|
95
|
-
var _a;
|
|
96
|
-
if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
const re = /(BixinKey\d{10})|(K\d{4})/i;
|
|
100
|
-
if (name && re.exec(name)) {
|
|
101
|
-
return true;
|
|
102
|
-
}
|
|
103
|
-
return false;
|
|
104
|
-
};
|
|
105
|
-
const ClassicServiceUUID = '00000001-0000-1000-8000-00805f9b34fb';
|
|
106
|
-
const OneKeyServices = {
|
|
107
|
-
classic: {
|
|
108
|
-
[ClassicServiceUUID]: {
|
|
109
|
-
serviceUuid: ClassicServiceUUID,
|
|
110
|
-
writeUuid: '00000002-0000-1000-8000-00805f9b34fb',
|
|
111
|
-
notifyUuid: '00000003-0000-1000-8000-00805f9b34fb',
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
|
-
};
|
|
115
|
-
const bluetoothServices = [];
|
|
116
|
-
for (const deviceType of Object.keys(OneKeyServices)) {
|
|
117
|
-
const services = OneKeyServices[deviceType];
|
|
118
|
-
bluetoothServices.push(...Object.keys(services));
|
|
119
|
-
}
|
|
120
|
-
const getBluetoothServiceUuids = () => bluetoothServices;
|
|
121
|
-
const getInfosForServiceUuid = (serviceUuid, deviceType) => {
|
|
122
|
-
const services = OneKeyServices[deviceType];
|
|
123
|
-
if (!services) {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
const service = services[serviceUuid];
|
|
127
|
-
if (!service) {
|
|
128
|
-
return null;
|
|
129
|
-
}
|
|
130
|
-
return service;
|
|
131
|
-
};
|
|
132
|
-
|
|
133
135
|
function create(arg, data) {
|
|
134
136
|
let localResolve = (_t) => { };
|
|
135
137
|
let localReject = (_e) => { };
|
|
@@ -181,10 +183,9 @@ class BleTransport {
|
|
|
181
183
|
}
|
|
182
184
|
|
|
183
185
|
const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"];
|
|
184
|
-
const blePlxManager = new reactNativeBlePlx.BleManager();
|
|
185
186
|
const transportCache = {};
|
|
186
187
|
let connectOptions = {
|
|
187
|
-
requestMTU:
|
|
188
|
+
requestMTU: 512,
|
|
188
189
|
timeout: 3000,
|
|
189
190
|
};
|
|
190
191
|
const tryToGetConfiguration = (device) => {
|
|
@@ -205,9 +206,7 @@ class ReactNativeBleTransport {
|
|
|
205
206
|
this.runPromise = null;
|
|
206
207
|
this.scanTimeout = (_a = options.scanTimeout) !== null && _a !== void 0 ? _a : 3000;
|
|
207
208
|
}
|
|
208
|
-
init() {
|
|
209
|
-
initializeBleManager();
|
|
210
|
-
}
|
|
209
|
+
init() { }
|
|
211
210
|
configure(signedData) {
|
|
212
211
|
const messages = parseConfigure(signedData);
|
|
213
212
|
this.configured = true;
|
|
@@ -215,16 +214,24 @@ class ReactNativeBleTransport {
|
|
|
215
214
|
}
|
|
216
215
|
listen() {
|
|
217
216
|
}
|
|
217
|
+
getPlxManager() {
|
|
218
|
+
if (this.blePlxManager)
|
|
219
|
+
return Promise.resolve(this.blePlxManager);
|
|
220
|
+
this.blePlxManager = new reactNativeBlePlx.BleManager();
|
|
221
|
+
initializeBleManager();
|
|
222
|
+
return Promise.resolve(this.blePlxManager);
|
|
223
|
+
}
|
|
218
224
|
enumerate() {
|
|
219
225
|
return __awaiter(this, void 0, void 0, function* () {
|
|
220
|
-
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
221
227
|
const deviceList = [];
|
|
228
|
+
const blePlxManager = yield this.getPlxManager();
|
|
222
229
|
try {
|
|
223
230
|
yield subscribeBleOn(blePlxManager);
|
|
224
231
|
}
|
|
225
232
|
catch (error) {
|
|
226
233
|
console.log('subscribeBleOn error: ', error);
|
|
227
|
-
|
|
234
|
+
reject(error);
|
|
228
235
|
return;
|
|
229
236
|
}
|
|
230
237
|
blePlxManager.startDeviceScan(null, {
|
|
@@ -234,6 +241,12 @@ class ReactNativeBleTransport {
|
|
|
234
241
|
if (error) {
|
|
235
242
|
console.log('ble scan manager: ', blePlxManager);
|
|
236
243
|
console.log('ble scan error: ', error);
|
|
244
|
+
if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
|
|
245
|
+
reject(new Error(PERMISSION_ERROR));
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
reject(error);
|
|
249
|
+
}
|
|
237
250
|
return;
|
|
238
251
|
}
|
|
239
252
|
if (isOnekeyDevice((_a = device === null || device === void 0 ? void 0 : device.name) !== null && _a !== void 0 ? _a : null, device === null || device === void 0 ? void 0 : device.id)) {
|
|
@@ -273,6 +286,7 @@ class ReactNativeBleTransport {
|
|
|
273
286
|
console.log('@onekey/hd-ble-sdk transport not be released, will release: ', uuid);
|
|
274
287
|
yield this.release(uuid);
|
|
275
288
|
}
|
|
289
|
+
const blePlxManager = yield this.getPlxManager();
|
|
276
290
|
try {
|
|
277
291
|
yield subscribeBleOn(blePlxManager);
|
|
278
292
|
}
|
|
@@ -403,7 +417,7 @@ class ReactNativeBleTransport {
|
|
|
403
417
|
else {
|
|
404
418
|
buffer$1 = buffer$1.concat([...data]);
|
|
405
419
|
}
|
|
406
|
-
if (buffer$1.length >= bufferLength) {
|
|
420
|
+
if (buffer$1.length - transport.COMMON_HEADER_SIZE >= bufferLength) {
|
|
407
421
|
const value = buffer.Buffer.from(buffer$1);
|
|
408
422
|
bufferLength = 0;
|
|
409
423
|
buffer$1 = [];
|
|
@@ -485,4 +499,6 @@ class ReactNativeBleTransport {
|
|
|
485
499
|
}
|
|
486
500
|
}
|
|
487
501
|
|
|
488
|
-
|
|
502
|
+
exports.LOCATION_ERROR = LOCATION_ERROR;
|
|
503
|
+
exports.PERMISSION_ERROR = PERMISSION_ERROR;
|
|
504
|
+
exports["default"] = ReactNativeBleTransport;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-transport": "^0.0.
|
|
23
|
+
"@onekeyfe/hd-transport": "^0.0.7",
|
|
24
24
|
"react-native-ble-manager": "^8.1.0",
|
|
25
25
|
"react-native-ble-plx": "^2.0.3"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "7be7677368fcb50f411891f009367424ea77df33"
|
|
28
28
|
}
|
package/src/constants.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
export const PERMISSION_ERROR = 'Bluetooth required to be turned on';
|
|
2
|
+
export const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
|
|
3
|
+
|
|
1
4
|
export const isOnekeyDevice = (name: string | null, id?: string): boolean => {
|
|
2
5
|
if (id?.startsWith?.('MI')) {
|
|
3
6
|
return true;
|
|
4
7
|
}
|
|
5
8
|
|
|
6
|
-
// 过滤 BixinKeyxxx 和 Kxxxx
|
|
9
|
+
// 过滤 BixinKeyxxx 和 Kxxxx 和 Txxxx
|
|
7
10
|
// i 忽略大小写模式
|
|
8
|
-
const re = /(BixinKey\d{10})|(K\d{4})/i;
|
|
11
|
+
const re = /(BixinKey\d{10})|(K\d{4})|(T\d{4})/i;
|
|
9
12
|
if (name && re.exec(name)) {
|
|
10
13
|
return true;
|
|
11
14
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Buffer } from 'buffer';
|
|
2
|
-
import transport from '@onekeyfe/hd-transport';
|
|
2
|
+
import transport, { COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport';
|
|
3
3
|
import {
|
|
4
4
|
BleManager as BlePlxManager,
|
|
5
5
|
Device,
|
|
@@ -9,7 +9,13 @@ import {
|
|
|
9
9
|
} from 'react-native-ble-plx';
|
|
10
10
|
import { initializeBleManager, getConnectedDeviceIds } from './BleManager';
|
|
11
11
|
import { subscribeBleOn } from './subscribeBleOn';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
PERMISSION_ERROR,
|
|
14
|
+
LOCATION_ERROR,
|
|
15
|
+
isOnekeyDevice,
|
|
16
|
+
getBluetoothServiceUuids,
|
|
17
|
+
getInfosForServiceUuid,
|
|
18
|
+
} from './constants';
|
|
13
19
|
import { Deferred, create as createDeferred } from './utils/deferred';
|
|
14
20
|
import { isHeaderChunk } from './utils/validateNotify';
|
|
15
21
|
import BleTransport from './BleTransport';
|
|
@@ -18,12 +24,10 @@ import type { BleAcquireInput, TransportOptions } from './types';
|
|
|
18
24
|
|
|
19
25
|
const { check, buildBuffers, receiveOne, parseConfigure } = transport;
|
|
20
26
|
|
|
21
|
-
const blePlxManager = new BlePlxManager();
|
|
22
|
-
|
|
23
27
|
const transportCache: Record<string, any> = {};
|
|
24
28
|
|
|
25
29
|
let connectOptions: Record<string, unknown> = {
|
|
26
|
-
requestMTU:
|
|
30
|
+
requestMTU: 512,
|
|
27
31
|
timeout: 3000,
|
|
28
32
|
};
|
|
29
33
|
|
|
@@ -36,6 +40,8 @@ const tryToGetConfiguration = (device: Device) => {
|
|
|
36
40
|
};
|
|
37
41
|
|
|
38
42
|
export default class ReactNativeBleTransport {
|
|
43
|
+
blePlxManager: BlePlxManager | undefined;
|
|
44
|
+
|
|
39
45
|
_messages: ReturnType<typeof transport.parseConfigure> | undefined;
|
|
40
46
|
|
|
41
47
|
configured = false;
|
|
@@ -50,9 +56,7 @@ export default class ReactNativeBleTransport {
|
|
|
50
56
|
this.scanTimeout = options.scanTimeout ?? 3000;
|
|
51
57
|
}
|
|
52
58
|
|
|
53
|
-
init() {
|
|
54
|
-
initializeBleManager();
|
|
55
|
-
}
|
|
59
|
+
init() {}
|
|
56
60
|
|
|
57
61
|
configure(signedData: any) {
|
|
58
62
|
const messages = parseConfigure(signedData);
|
|
@@ -64,6 +68,13 @@ export default class ReactNativeBleTransport {
|
|
|
64
68
|
// empty
|
|
65
69
|
}
|
|
66
70
|
|
|
71
|
+
getPlxManager(): Promise<BlePlxManager> {
|
|
72
|
+
if (this.blePlxManager) return Promise.resolve(this.blePlxManager);
|
|
73
|
+
this.blePlxManager = new BlePlxManager();
|
|
74
|
+
initializeBleManager();
|
|
75
|
+
return Promise.resolve(this.blePlxManager);
|
|
76
|
+
}
|
|
77
|
+
|
|
67
78
|
/**
|
|
68
79
|
* 获取设备列表
|
|
69
80
|
* 在搜索超过超时时间或设备数量大于 5 台时,返回 OneKey 设备,
|
|
@@ -71,14 +82,14 @@ export default class ReactNativeBleTransport {
|
|
|
71
82
|
*/
|
|
72
83
|
async enumerate() {
|
|
73
84
|
// eslint-disable-next-line no-async-promise-executor
|
|
74
|
-
return new Promise<Device[]>(async resolve => {
|
|
85
|
+
return new Promise<Device[]>(async (resolve, reject) => {
|
|
75
86
|
const deviceList: Device[] = [];
|
|
76
|
-
|
|
87
|
+
const blePlxManager = await this.getPlxManager();
|
|
77
88
|
try {
|
|
78
89
|
await subscribeBleOn(blePlxManager);
|
|
79
90
|
} catch (error) {
|
|
80
91
|
console.log('subscribeBleOn error: ', error);
|
|
81
|
-
|
|
92
|
+
reject(error);
|
|
82
93
|
return;
|
|
83
94
|
}
|
|
84
95
|
|
|
@@ -91,6 +102,15 @@ export default class ReactNativeBleTransport {
|
|
|
91
102
|
if (error) {
|
|
92
103
|
console.log('ble scan manager: ', blePlxManager);
|
|
93
104
|
console.log('ble scan error: ', error);
|
|
105
|
+
if (
|
|
106
|
+
[BleErrorCode.BluetoothPoweredOff, BleErrorCode.BluetoothInUnknownState].includes(
|
|
107
|
+
error.errorCode
|
|
108
|
+
)
|
|
109
|
+
) {
|
|
110
|
+
reject(new Error(PERMISSION_ERROR));
|
|
111
|
+
} else {
|
|
112
|
+
reject(error);
|
|
113
|
+
}
|
|
94
114
|
return;
|
|
95
115
|
}
|
|
96
116
|
|
|
@@ -146,6 +166,7 @@ export default class ReactNativeBleTransport {
|
|
|
146
166
|
await this.release(uuid);
|
|
147
167
|
}
|
|
148
168
|
|
|
169
|
+
const blePlxManager = await this.getPlxManager();
|
|
149
170
|
try {
|
|
150
171
|
await subscribeBleOn(blePlxManager);
|
|
151
172
|
} catch (error) {
|
|
@@ -288,6 +309,7 @@ export default class ReactNativeBleTransport {
|
|
|
288
309
|
|
|
289
310
|
try {
|
|
290
311
|
const data = Buffer.from(c.value as string, 'base64');
|
|
312
|
+
// console.log('[hd-transport-react-native] Received a packet, ', 'buffer: ', data);
|
|
291
313
|
if (isHeaderChunk(data)) {
|
|
292
314
|
bufferLength = data.readInt32BE(5);
|
|
293
315
|
buffer = [...data.subarray(3)];
|
|
@@ -295,7 +317,7 @@ export default class ReactNativeBleTransport {
|
|
|
295
317
|
buffer = buffer.concat([...data]);
|
|
296
318
|
}
|
|
297
319
|
|
|
298
|
-
if (buffer.length >= bufferLength) {
|
|
320
|
+
if (buffer.length - COMMON_HEADER_SIZE >= bufferLength) {
|
|
299
321
|
const value = Buffer.from(buffer);
|
|
300
322
|
// console.log(
|
|
301
323
|
// '[hd-transport-react-native] Received a complete packet of data, resolve Promise, this.runPromise: ',
|
|
@@ -389,3 +411,5 @@ export default class ReactNativeBleTransport {
|
|
|
389
411
|
this.stopped = true;
|
|
390
412
|
}
|
|
391
413
|
}
|
|
414
|
+
|
|
415
|
+
export { PERMISSION_ERROR, LOCATION_ERROR };
|
package/src/subscribeBleOn.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
+
import { PERMISSION_ERROR } from './constants';
|
|
1
2
|
import { BlePlxManager } from './types';
|
|
2
3
|
import timer from './utils/timer';
|
|
3
4
|
|
|
4
|
-
export const subscribeBleOn = (bleManager: BlePlxManager, ms =
|
|
5
|
+
export const subscribeBleOn = (bleManager: BlePlxManager, ms = 1000): Promise<void> =>
|
|
5
6
|
new Promise((resolve, reject) => {
|
|
6
7
|
let done = false;
|
|
7
|
-
let lastState = 'Unknown';
|
|
8
8
|
|
|
9
9
|
const subscription = bleManager.onStateChange(state => {
|
|
10
|
-
lastState = state;
|
|
11
|
-
|
|
12
10
|
console.log('ble state -> ', state);
|
|
13
11
|
|
|
14
12
|
if (state === 'PoweredOn') {
|
|
@@ -23,6 +21,6 @@ export const subscribeBleOn = (bleManager: BlePlxManager, ms = 3000): Promise<vo
|
|
|
23
21
|
const clearTimeout = timer.timeout(() => {
|
|
24
22
|
if (done) return;
|
|
25
23
|
subscription.remove();
|
|
26
|
-
reject(new Error(
|
|
24
|
+
reject(new Error(PERMISSION_ERROR));
|
|
27
25
|
}, ms);
|
|
28
26
|
});
|