@onekeyfe/hd-transport-react-native 0.1.40 → 0.1.43
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/index.js +65 -73
- package/package.json +4 -5
package/dist/index.js
CHANGED
|
@@ -1,18 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var hdShared = require('@onekeyfe/hd-shared');
|
|
9
|
-
var BleManager = require('react-native-ble-manager');
|
|
10
|
-
|
|
11
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
-
|
|
13
|
-
var ByteBuffer__default = /*#__PURE__*/_interopDefaultLegacy(ByteBuffer);
|
|
14
|
-
var transport__default = /*#__PURE__*/_interopDefaultLegacy(transport);
|
|
15
|
-
var BleManager__default = /*#__PURE__*/_interopDefaultLegacy(BleManager);
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
import { Buffer } from 'buffer';
|
|
3
|
+
import { BleManager as BleManager$1, BleErrorCode, ScanMode } from 'react-native-ble-plx';
|
|
4
|
+
import ByteBuffer from 'bytebuffer';
|
|
5
|
+
import transport, { MESSAGE_TOP_CHAR, MESSAGE_HEADER_BYTE, COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport';
|
|
6
|
+
import { ERRORS, HardwareErrorCode, createDeferred } from '@onekeyfe/hd-shared';
|
|
7
|
+
import BleManager from 'react-native-ble-manager';
|
|
16
8
|
|
|
17
9
|
/******************************************************************************
|
|
18
10
|
Copyright (c) Microsoft Corporation.
|
|
@@ -40,17 +32,17 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
40
32
|
}
|
|
41
33
|
|
|
42
34
|
const initializeBleManager = () => {
|
|
43
|
-
|
|
35
|
+
BleManager.start({ showAlert: false });
|
|
44
36
|
};
|
|
45
37
|
const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0, function* () {
|
|
46
|
-
const connectedPeripherals = yield
|
|
38
|
+
const connectedPeripherals = yield BleManager.getConnectedPeripherals(serviceUuids);
|
|
47
39
|
return connectedPeripherals.map(peripheral => {
|
|
48
40
|
const { id, name, advertising = {} } = peripheral;
|
|
49
41
|
return Object.assign({ id, name }, advertising);
|
|
50
42
|
});
|
|
51
43
|
});
|
|
52
44
|
const getBondedDevices = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
-
const peripherals = yield
|
|
45
|
+
const peripherals = yield BleManager.getBondedPeripherals();
|
|
54
46
|
return peripherals.map(peripheral => {
|
|
55
47
|
const { id, name, advertising = {} } = peripheral;
|
|
56
48
|
return Object.assign({ id, name }, advertising);
|
|
@@ -96,7 +88,7 @@ const subscribeBleOn = (bleManager, ms = 1000) => new Promise((resolve, reject)
|
|
|
96
88
|
if (done)
|
|
97
89
|
return;
|
|
98
90
|
subscription.remove();
|
|
99
|
-
reject(
|
|
91
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BlePermissionError));
|
|
100
92
|
}, ms);
|
|
101
93
|
});
|
|
102
94
|
|
|
@@ -145,9 +137,9 @@ const isHeaderChunk = (chunk) => {
|
|
|
145
137
|
if (chunk.length < 9)
|
|
146
138
|
return false;
|
|
147
139
|
const [MagicQuestionMark, sharp1, sharp2] = chunk;
|
|
148
|
-
if (String.fromCharCode(MagicQuestionMark) === String.fromCharCode(
|
|
149
|
-
String.fromCharCode(sharp1) === String.fromCharCode(
|
|
150
|
-
String.fromCharCode(sharp2) === String.fromCharCode(
|
|
140
|
+
if (String.fromCharCode(MagicQuestionMark) === String.fromCharCode(MESSAGE_TOP_CHAR) &&
|
|
141
|
+
String.fromCharCode(sharp1) === String.fromCharCode(MESSAGE_HEADER_BYTE) &&
|
|
142
|
+
String.fromCharCode(sharp2) === String.fromCharCode(MESSAGE_HEADER_BYTE)) {
|
|
151
143
|
return true;
|
|
152
144
|
}
|
|
153
145
|
return false;
|
|
@@ -164,7 +156,7 @@ class BleTransport {
|
|
|
164
156
|
}
|
|
165
157
|
}
|
|
166
158
|
|
|
167
|
-
const { check, buildBuffers, receiveOne, parseConfigure } =
|
|
159
|
+
const { check, buildBuffers, receiveOne, parseConfigure } = transport;
|
|
168
160
|
const transportCache = {};
|
|
169
161
|
let connectOptions = {
|
|
170
162
|
requestMTU: 256,
|
|
@@ -202,7 +194,7 @@ class ReactNativeBleTransport {
|
|
|
202
194
|
getPlxManager() {
|
|
203
195
|
if (this.blePlxManager)
|
|
204
196
|
return Promise.resolve(this.blePlxManager);
|
|
205
|
-
this.blePlxManager = new
|
|
197
|
+
this.blePlxManager = new BleManager$1();
|
|
206
198
|
initializeBleManager();
|
|
207
199
|
return Promise.resolve(this.blePlxManager);
|
|
208
200
|
}
|
|
@@ -220,23 +212,23 @@ class ReactNativeBleTransport {
|
|
|
220
212
|
return;
|
|
221
213
|
}
|
|
222
214
|
blePlxManager.startDeviceScan(null, {
|
|
223
|
-
scanMode:
|
|
215
|
+
scanMode: ScanMode.LowLatency,
|
|
224
216
|
}, (error, device) => {
|
|
225
217
|
var _a, _b;
|
|
226
218
|
if (error) {
|
|
227
219
|
this.Log.debug('ble scan manager: ', blePlxManager);
|
|
228
220
|
this.Log.debug('ble scan error: ', error);
|
|
229
|
-
if ([
|
|
230
|
-
reject(
|
|
221
|
+
if ([BleErrorCode.BluetoothPoweredOff, BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
|
|
222
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BlePermissionError));
|
|
231
223
|
}
|
|
232
|
-
else if (error.errorCode ===
|
|
233
|
-
reject(
|
|
224
|
+
else if (error.errorCode === BleErrorCode.BluetoothUnauthorized) {
|
|
225
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BleLocationError));
|
|
234
226
|
}
|
|
235
|
-
else if (error.errorCode ===
|
|
227
|
+
else if (error.errorCode === BleErrorCode.ScanStartFailed) {
|
|
236
228
|
timer.timeout(() => { }, this.scanTimeout);
|
|
237
229
|
}
|
|
238
230
|
else {
|
|
239
|
-
reject(
|
|
231
|
+
reject(ERRORS.TypedError(HardwareErrorCode.BleScanError, (_a = error.reason) !== null && _a !== void 0 ? _a : ''));
|
|
240
232
|
}
|
|
241
233
|
return;
|
|
242
234
|
}
|
|
@@ -271,7 +263,7 @@ class ReactNativeBleTransport {
|
|
|
271
263
|
return __awaiter(this, void 0, void 0, function* () {
|
|
272
264
|
const { uuid } = input;
|
|
273
265
|
if (!uuid) {
|
|
274
|
-
throw
|
|
266
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleRequiredUUID);
|
|
275
267
|
}
|
|
276
268
|
let device = null;
|
|
277
269
|
if (transportCache[uuid]) {
|
|
@@ -303,23 +295,23 @@ class ReactNativeBleTransport {
|
|
|
303
295
|
}
|
|
304
296
|
catch (e) {
|
|
305
297
|
this.Log.debug('try to connect to device has error: ', e);
|
|
306
|
-
if (e.errorCode ===
|
|
307
|
-
e.errorCode ===
|
|
298
|
+
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed ||
|
|
299
|
+
e.errorCode === BleErrorCode.OperationCancelled) {
|
|
308
300
|
connectOptions = {};
|
|
309
301
|
this.Log.debug('first try to reconnect without params');
|
|
310
302
|
device = yield blePlxManager.connectToDevice(uuid);
|
|
311
303
|
}
|
|
312
|
-
else if (e.errorCode ===
|
|
304
|
+
else if (e.errorCode === BleErrorCode.DeviceAlreadyConnected) {
|
|
313
305
|
this.Log.debug('device already connected');
|
|
314
|
-
throw
|
|
306
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleAlreadyConnected);
|
|
315
307
|
}
|
|
316
308
|
else {
|
|
317
|
-
throw
|
|
309
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, (_a = e.reason) !== null && _a !== void 0 ? _a : e);
|
|
318
310
|
}
|
|
319
311
|
}
|
|
320
312
|
}
|
|
321
313
|
if (!device) {
|
|
322
|
-
throw
|
|
314
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, 'unable to connect to device');
|
|
323
315
|
}
|
|
324
316
|
if (!(yield device.isConnected())) {
|
|
325
317
|
this.Log.debug('not connected, try to connect to device: ', uuid);
|
|
@@ -328,8 +320,8 @@ class ReactNativeBleTransport {
|
|
|
328
320
|
}
|
|
329
321
|
catch (e) {
|
|
330
322
|
this.Log.debug('not connected, try to connect to device has error: ', e);
|
|
331
|
-
if (e.errorCode ===
|
|
332
|
-
e.errorCode ===
|
|
323
|
+
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed ||
|
|
324
|
+
e.errorCode === BleErrorCode.OperationCancelled) {
|
|
333
325
|
connectOptions = {};
|
|
334
326
|
this.Log.debug('second try to reconnect without params');
|
|
335
327
|
try {
|
|
@@ -337,7 +329,7 @@ class ReactNativeBleTransport {
|
|
|
337
329
|
}
|
|
338
330
|
catch (e) {
|
|
339
331
|
this.Log.debug('last try to reconnect error: ', e);
|
|
340
|
-
if (e.errorCode ===
|
|
332
|
+
if (e.errorCode === BleErrorCode.OperationCancelled) {
|
|
341
333
|
this.Log.debug('last try to reconnect');
|
|
342
334
|
yield device.cancelConnection();
|
|
343
335
|
yield device.connect();
|
|
@@ -345,15 +337,15 @@ class ReactNativeBleTransport {
|
|
|
345
337
|
}
|
|
346
338
|
}
|
|
347
339
|
else {
|
|
348
|
-
throw
|
|
340
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, (_b = e.reason) !== null && _b !== void 0 ? _b : e);
|
|
349
341
|
}
|
|
350
342
|
}
|
|
351
343
|
}
|
|
352
|
-
if (
|
|
344
|
+
if (Platform.OS === 'android') {
|
|
353
345
|
const bondedDevices = yield getBondedDevices();
|
|
354
346
|
const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === (device === null || device === void 0 ? void 0 : device.id));
|
|
355
347
|
if (!hasBonded) {
|
|
356
|
-
throw
|
|
348
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded');
|
|
357
349
|
}
|
|
358
350
|
}
|
|
359
351
|
yield device.discoverAllServicesAndCharacteristics();
|
|
@@ -379,14 +371,14 @@ class ReactNativeBleTransport {
|
|
|
379
371
|
catch (e) {
|
|
380
372
|
this.Log.debug('cancel connection error when service not found: ', e.message || e.reason);
|
|
381
373
|
}
|
|
382
|
-
throw
|
|
374
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleServiceNotFound);
|
|
383
375
|
}
|
|
384
376
|
const { serviceUuid, writeUuid, notifyUuid } = infos;
|
|
385
377
|
if (!characteristics) {
|
|
386
378
|
characteristics = yield device.characteristicsForService(serviceUuid);
|
|
387
379
|
}
|
|
388
380
|
if (!characteristics) {
|
|
389
|
-
throw
|
|
381
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotFound);
|
|
390
382
|
}
|
|
391
383
|
let writeCharacteristic;
|
|
392
384
|
let notifyCharacteristic;
|
|
@@ -399,16 +391,16 @@ class ReactNativeBleTransport {
|
|
|
399
391
|
}
|
|
400
392
|
}
|
|
401
393
|
if (!writeCharacteristic) {
|
|
402
|
-
throw
|
|
394
|
+
throw ERRORS.TypedError('BLECharacteristicNotFound: write characteristic not found');
|
|
403
395
|
}
|
|
404
396
|
if (!notifyCharacteristic) {
|
|
405
|
-
throw
|
|
397
|
+
throw ERRORS.TypedError('BLECharacteristicNotFound: notify characteristic not found');
|
|
406
398
|
}
|
|
407
399
|
if (!writeCharacteristic.isWritableWithResponse) {
|
|
408
|
-
throw
|
|
400
|
+
throw ERRORS.TypedError('BLECharacteristicNotWritable: write characteristic not writable');
|
|
409
401
|
}
|
|
410
402
|
if (!notifyCharacteristic.isNotifiable) {
|
|
411
|
-
throw
|
|
403
|
+
throw ERRORS.TypedError('BLECharacteristicNotNotifiable: notify characteristic not notifiable');
|
|
412
404
|
}
|
|
413
405
|
const transport = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
|
|
414
406
|
transport.nofitySubscription = this._monitorCharacteristic(transport.notifyCharacteristic);
|
|
@@ -434,33 +426,33 @@ class ReactNativeBleTransport {
|
|
|
434
426
|
}
|
|
435
427
|
_monitorCharacteristic(characteristic) {
|
|
436
428
|
let bufferLength = 0;
|
|
437
|
-
let buffer
|
|
429
|
+
let buffer = [];
|
|
438
430
|
const subscription = characteristic.monitor((error, c) => {
|
|
439
431
|
var _a, _b, _c;
|
|
440
432
|
if (error) {
|
|
441
433
|
this.Log.debug(`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${error}`);
|
|
442
434
|
if (this.runPromise) {
|
|
443
|
-
this.runPromise.reject(
|
|
435
|
+
this.runPromise.reject(ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotifyError, (_a = error.reason) !== null && _a !== void 0 ? _a : error.message));
|
|
444
436
|
this.Log.debug(': monitor notify error, and has unreleased Promise');
|
|
445
437
|
}
|
|
446
438
|
return;
|
|
447
439
|
}
|
|
448
440
|
if (!c) {
|
|
449
|
-
throw
|
|
441
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleMonitorError);
|
|
450
442
|
}
|
|
451
443
|
try {
|
|
452
|
-
const data =
|
|
444
|
+
const data = Buffer.from(c.value, 'base64');
|
|
453
445
|
if (isHeaderChunk(data)) {
|
|
454
446
|
bufferLength = data.readInt32BE(5);
|
|
455
|
-
buffer
|
|
447
|
+
buffer = [...data.subarray(3)];
|
|
456
448
|
}
|
|
457
449
|
else {
|
|
458
|
-
buffer
|
|
450
|
+
buffer = buffer.concat([...data]);
|
|
459
451
|
}
|
|
460
|
-
if (buffer
|
|
461
|
-
const value =
|
|
452
|
+
if (buffer.length - COMMON_HEADER_SIZE >= bufferLength) {
|
|
453
|
+
const value = Buffer.from(buffer);
|
|
462
454
|
bufferLength = 0;
|
|
463
|
-
buffer
|
|
455
|
+
buffer = [];
|
|
464
456
|
(_b = this.runPromise) === null || _b === void 0 ? void 0 : _b.resolve(value.toString('hex'));
|
|
465
457
|
}
|
|
466
458
|
}
|
|
@@ -481,7 +473,7 @@ class ReactNativeBleTransport {
|
|
|
481
473
|
if (transport) {
|
|
482
474
|
delete transportCache[uuid];
|
|
483
475
|
(_a = transport.nofitySubscription) === null || _a === void 0 ? void 0 : _a.call(transport);
|
|
484
|
-
if (
|
|
476
|
+
if (Platform.OS === 'android') ;
|
|
485
477
|
}
|
|
486
478
|
return Promise.resolve(true);
|
|
487
479
|
});
|
|
@@ -489,26 +481,26 @@ class ReactNativeBleTransport {
|
|
|
489
481
|
call(uuid, name, data) {
|
|
490
482
|
return __awaiter(this, void 0, void 0, function* () {
|
|
491
483
|
if (this.stopped) {
|
|
492
|
-
return Promise.reject(
|
|
484
|
+
return Promise.reject(ERRORS.TypedError('Transport stopped.'));
|
|
493
485
|
}
|
|
494
486
|
if (this._messages == null) {
|
|
495
|
-
throw
|
|
487
|
+
throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
|
|
496
488
|
}
|
|
497
489
|
if (this.runPromise) {
|
|
498
|
-
throw
|
|
490
|
+
throw ERRORS.TypedError(HardwareErrorCode.TransportCallInProgress);
|
|
499
491
|
}
|
|
500
492
|
const transport = transportCache[uuid];
|
|
501
493
|
if (!transport) {
|
|
502
|
-
throw
|
|
494
|
+
throw ERRORS.TypedError(HardwareErrorCode.TransportNotFound);
|
|
503
495
|
}
|
|
504
|
-
this.runPromise =
|
|
496
|
+
this.runPromise = createDeferred();
|
|
505
497
|
const messages = this._messages;
|
|
506
498
|
this.Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
|
|
507
499
|
const buffers = buildBuffers(messages, name, data);
|
|
508
500
|
if (name === 'FirmwareUpload') {
|
|
509
|
-
const packetCapacity =
|
|
501
|
+
const packetCapacity = Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH;
|
|
510
502
|
let index = 0;
|
|
511
|
-
let chunk =
|
|
503
|
+
let chunk = ByteBuffer.allocate(packetCapacity);
|
|
512
504
|
while (index < buffers.length) {
|
|
513
505
|
const buffer = buffers[index].toBuffer();
|
|
514
506
|
chunk.append(buffer);
|
|
@@ -517,7 +509,7 @@ class ReactNativeBleTransport {
|
|
|
517
509
|
chunk.reset();
|
|
518
510
|
try {
|
|
519
511
|
yield transport.writeCharacteristic.writeWithoutResponse(chunk.toString('base64'));
|
|
520
|
-
chunk =
|
|
512
|
+
chunk = ByteBuffer.allocate(packetCapacity);
|
|
521
513
|
}
|
|
522
514
|
catch (e) {
|
|
523
515
|
this.runPromise = null;
|
|
@@ -537,11 +529,11 @@ class ReactNativeBleTransport {
|
|
|
537
529
|
catch (e) {
|
|
538
530
|
this.Log.debug('writeCharacteristic write error: ', e);
|
|
539
531
|
this.runPromise = null;
|
|
540
|
-
if (e.errorCode ===
|
|
541
|
-
throw
|
|
532
|
+
if (e.errorCode === BleErrorCode.DeviceDisconnected) {
|
|
533
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded);
|
|
542
534
|
}
|
|
543
|
-
if (e.errorCode ===
|
|
544
|
-
throw
|
|
535
|
+
if (e.errorCode === BleErrorCode.OperationStartFailed) {
|
|
536
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|
|
545
537
|
}
|
|
546
538
|
return;
|
|
547
539
|
}
|
|
@@ -575,4 +567,4 @@ class ReactNativeBleTransport {
|
|
|
575
567
|
}
|
|
576
568
|
}
|
|
577
569
|
|
|
578
|
-
|
|
570
|
+
export { ReactNativeBleTransport as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.43",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -13,17 +13,16 @@
|
|
|
13
13
|
"url": "git+https://github.com/OneKeyHQ/hardware-js-sdk.git"
|
|
14
14
|
},
|
|
15
15
|
"scripts": {
|
|
16
|
-
"test": "echo \"Error: run tests from root\" && exit 1",
|
|
17
16
|
"dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w",
|
|
18
17
|
"build": "rimraf dist && rollup -c ../../build/rollup.config.js",
|
|
19
18
|
"lint": "eslint .",
|
|
20
19
|
"lint:fix": "eslint . --fix"
|
|
21
20
|
},
|
|
22
21
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-shared": "^0.1.
|
|
24
|
-
"@onekeyfe/hd-transport": "^0.1.
|
|
22
|
+
"@onekeyfe/hd-shared": "^0.1.43",
|
|
23
|
+
"@onekeyfe/hd-transport": "^0.1.43",
|
|
25
24
|
"react-native-ble-manager": "^8.1.0",
|
|
26
25
|
"react-native-ble-plx": "^2.0.3"
|
|
27
26
|
},
|
|
28
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "15d5f1b90ef26478f2183317dd192443e44adbe2"
|
|
29
28
|
}
|