@onekeyfe/hd-transport-react-native 1.0.20 → 1.0.21
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/BleTransport.d.ts +3 -0
- package/dist/BleTransport.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +94 -35
- package/package.json +4 -4
- package/src/BleTransport.ts +43 -1
- package/src/index.ts +57 -26
package/dist/BleTransport.d.ts
CHANGED
|
@@ -6,6 +6,9 @@ export default class BleTransport {
|
|
|
6
6
|
writeCharacteristic: Characteristic;
|
|
7
7
|
notifyCharacteristic: Characteristic;
|
|
8
8
|
nofitySubscription?: () => void;
|
|
9
|
+
static MAX_RETRIES: number;
|
|
10
|
+
static RETRY_DELAY: number;
|
|
9
11
|
constructor(device: Device, writeCharacteristic: Characteristic, notifyCharacteristic: Characteristic);
|
|
12
|
+
writeWithRetry(data: string, retryCount?: number): Promise<void>;
|
|
10
13
|
}
|
|
11
14
|
//# sourceMappingURL=BleTransport.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BleTransport.d.ts","sourceRoot":"","sources":["../src/BleTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,
|
|
1
|
+
{"version":3,"file":"BleTransport.d.ts","sourceRoot":"","sources":["../src/BleTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAgB,MAAM,gCAAgC,CAAC;AAMtF,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,EAAE,EAAE,MAAM,CAAC;IAEX,MAAM,EAAE,MAAM,CAAC;IAEf,OAAO,SAAM;IAEb,mBAAmB,EAAE,cAAc,CAAC;IAEpC,oBAAoB,EAAE,cAAc,CAAC;IAErC,kBAAkB,CAAC,EAAE,MAAM,IAAI,CAAC;IAEhC,MAAM,CAAC,WAAW,SAAK;IAEvB,MAAM,CAAC,WAAW,SAAQ;gBAGxB,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,cAAc,EACnC,oBAAoB,EAAE,cAAc;IAehC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,SAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;CA2BzF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ declare class ReactNativeBleTransport {
|
|
|
32
32
|
}>;
|
|
33
33
|
_monitorCharacteristic(characteristic: Characteristic): () => void;
|
|
34
34
|
release(uuid: string): Promise<boolean>;
|
|
35
|
-
call(uuid: string, name: string, data: Record<string, unknown>): Promise<transport.MessageFromOneKey
|
|
35
|
+
call(uuid: string, name: string, data: Record<string, unknown>): Promise<transport.MessageFromOneKey>;
|
|
36
36
|
stop(): void;
|
|
37
37
|
cancel(): void;
|
|
38
38
|
}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,EACL,UAAU,IAAI,aAAa,EAC3B,MAAM,EAEN,cAAc,EAEf,MAAM,gCAAgC,CAAC;AAExC,OAAO,SAAkD,MAAM,wBAAwB,CAAC;AACxF,OAAO,EAAkB,QAAQ,EAA6B,MAAM,qBAAqB,CAAC;AAC1F,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AAavC,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAoBjE,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;IAExC,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;gBAEX,OAAO,EAAE,gBAAgB;IAIrC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,YAAY;IAKvC,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,MAAM;IAIN,aAAa,IAAI,OAAO,CAAC,aAAa,CAAC;IAYjC,SAAS;IA6FT,OAAO,CAAC,KAAK,EAAE,eAAe;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,EACL,UAAU,IAAI,aAAa,EAC3B,MAAM,EAEN,cAAc,EAEf,MAAM,gCAAgC,CAAC;AAExC,OAAO,SAAkD,MAAM,wBAAwB,CAAC;AACxF,OAAO,EAAkB,QAAQ,EAA6B,MAAM,qBAAqB,CAAC;AAC1F,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AAavC,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAoBjE,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;IAExC,GAAG,CAAC,EAAE,GAAG,CAAC;IAEV,OAAO,CAAC,EAAE,YAAY,CAAC;gBAEX,OAAO,EAAE,gBAAgB;IAIrC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,YAAY;IAKvC,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,MAAM;IAIN,aAAa,IAAI,OAAO,CAAC,aAAa,CAAC;IAYjC,SAAS;IA6FT,OAAO,CAAC,KAAK,EAAE,eAAe;;;IAmNpC,sBAAsB,CAAC,cAAc,EAAE,cAAc;IAgF/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;IAwHpE,IAAI;IAIJ,MAAM;CAOP"}
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,7 @@ var ByteBuffer = require('bytebuffer');
|
|
|
7
7
|
var transport = require('@onekeyfe/hd-transport');
|
|
8
8
|
var hdShared = require('@onekeyfe/hd-shared');
|
|
9
9
|
var BleManager = require('react-native-ble-manager');
|
|
10
|
+
var hdCore = require('@onekeyfe/hd-core');
|
|
10
11
|
|
|
11
12
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
13
|
|
|
@@ -158,6 +159,7 @@ const isHeaderChunk = (chunk) => {
|
|
|
158
159
|
return false;
|
|
159
160
|
};
|
|
160
161
|
|
|
162
|
+
const Log = hdCore.getLogger(hdCore.LoggerNames.HdBleTransport);
|
|
161
163
|
class BleTransport {
|
|
162
164
|
constructor(device, writeCharacteristic, notifyCharacteristic) {
|
|
163
165
|
this.mtuSize = 20;
|
|
@@ -167,7 +169,37 @@ class BleTransport {
|
|
|
167
169
|
this.notifyCharacteristic = notifyCharacteristic;
|
|
168
170
|
console.log(`BleTransport(${String(this.id)}) new instance`);
|
|
169
171
|
}
|
|
172
|
+
writeWithRetry(data, retryCount = BleTransport.MAX_RETRIES) {
|
|
173
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
+
try {
|
|
175
|
+
yield this.writeCharacteristic.writeWithoutResponse(data);
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
Log === null || Log === void 0 ? void 0 : Log.debug(`Write retry attempt ${BleTransport.MAX_RETRIES - retryCount + 1}, error: ${error}`);
|
|
179
|
+
if (retryCount > 0) {
|
|
180
|
+
yield hdCore.wait(BleTransport.RETRY_DELAY);
|
|
181
|
+
if (error.errorCode === reactNativeBlePlx.BleErrorCode.DeviceDisconnected ||
|
|
182
|
+
error.errorCode === reactNativeBlePlx.BleErrorCode.CharacteristicNotFound) {
|
|
183
|
+
try {
|
|
184
|
+
yield this.device.connect();
|
|
185
|
+
yield this.device.discoverAllServicesAndCharacteristics();
|
|
186
|
+
}
|
|
187
|
+
catch (e) {
|
|
188
|
+
Log === null || Log === void 0 ? void 0 : Log.debug(`Connect or discoverAllServicesAndCharacteristics error: ${e}`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
Log === null || Log === void 0 ? void 0 : Log.debug(`writeCharacteristic error: ${error}`);
|
|
193
|
+
}
|
|
194
|
+
return this.writeWithRetry(data, retryCount - 1);
|
|
195
|
+
}
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
170
200
|
}
|
|
201
|
+
BleTransport.MAX_RETRIES = 5;
|
|
202
|
+
BleTransport.RETRY_DELAY = 2000;
|
|
171
203
|
|
|
172
204
|
const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"];
|
|
173
205
|
const transportCache = {};
|
|
@@ -447,14 +479,24 @@ class ReactNativeBleTransport {
|
|
|
447
479
|
});
|
|
448
480
|
const disconnectSubscription = device.onDisconnected(() => {
|
|
449
481
|
var _a;
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
482
|
+
try {
|
|
483
|
+
this.Log.debug('device disconnect: ', device === null || device === void 0 ? void 0 : device.id);
|
|
484
|
+
(_a = this.emitter) === null || _a === void 0 ? void 0 : _a.emit('device-disconnect', {
|
|
485
|
+
name: device === null || device === void 0 ? void 0 : device.name,
|
|
486
|
+
id: device === null || device === void 0 ? void 0 : device.id,
|
|
487
|
+
connectId: device === null || device === void 0 ? void 0 : device.id,
|
|
488
|
+
});
|
|
489
|
+
if (this.runPromise) {
|
|
490
|
+
this.runPromise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError));
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
catch (e) {
|
|
494
|
+
this.Log.debug('device disconnect error: ', e);
|
|
495
|
+
}
|
|
496
|
+
finally {
|
|
497
|
+
this.release(uuid);
|
|
498
|
+
disconnectSubscription === null || disconnectSubscription === void 0 ? void 0 : disconnectSubscription.remove();
|
|
499
|
+
}
|
|
458
500
|
});
|
|
459
501
|
return { uuid };
|
|
460
502
|
});
|
|
@@ -463,7 +505,7 @@ class ReactNativeBleTransport {
|
|
|
463
505
|
let bufferLength = 0;
|
|
464
506
|
let buffer$1 = [];
|
|
465
507
|
const subscription = characteristic.monitor((error, c) => {
|
|
466
|
-
var _a, _b, _c, _d, _e, _f, _g
|
|
508
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
467
509
|
if (error) {
|
|
468
510
|
this.Log.debug(`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${error}`);
|
|
469
511
|
if (this.runPromise) {
|
|
@@ -477,12 +519,12 @@ class ReactNativeBleTransport {
|
|
|
477
519
|
if (((_c = error.reason) === null || _c === void 0 ? void 0 : _c.includes('Cannot write client characteristic config descriptor')) ||
|
|
478
520
|
((_d = error.reason) === null || _d === void 0 ? void 0 : _d.includes('Cannot find client characteristic config descriptor')) ||
|
|
479
521
|
((_e = error.reason) === null || _e === void 0 ? void 0 : _e.includes('The handle is invalid'))) {
|
|
480
|
-
this.runPromise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure
|
|
522
|
+
this.runPromise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure));
|
|
481
523
|
this.Log.debug(`${hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure} ${error.message} ${error.reason}`);
|
|
482
524
|
return;
|
|
483
525
|
}
|
|
484
|
-
this.runPromise.reject(hdShared.ERRORS.TypedError(ERROR
|
|
485
|
-
this.Log.debug(': monitor notify error, and has unreleased Promise');
|
|
526
|
+
this.runPromise.reject(hdShared.ERRORS.TypedError(ERROR));
|
|
527
|
+
this.Log.debug(': monitor notify error, and has unreleased Promise', Error);
|
|
486
528
|
}
|
|
487
529
|
return;
|
|
488
530
|
}
|
|
@@ -502,12 +544,12 @@ class ReactNativeBleTransport {
|
|
|
502
544
|
const value = buffer.Buffer.from(buffer$1);
|
|
503
545
|
bufferLength = 0;
|
|
504
546
|
buffer$1 = [];
|
|
505
|
-
(
|
|
547
|
+
(_f = this.runPromise) === null || _f === void 0 ? void 0 : _f.resolve(value.toString('hex'));
|
|
506
548
|
}
|
|
507
549
|
}
|
|
508
550
|
catch (error) {
|
|
509
551
|
this.Log.debug('monitor data error: ', error);
|
|
510
|
-
(
|
|
552
|
+
(_g = this.runPromise) === null || _g === void 0 ? void 0 : _g.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleWriteCharacteristicError));
|
|
511
553
|
}
|
|
512
554
|
});
|
|
513
555
|
return () => {
|
|
@@ -557,27 +599,42 @@ class ReactNativeBleTransport {
|
|
|
557
599
|
this.Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
|
|
558
600
|
}
|
|
559
601
|
const buffers = buildBuffers(messages, name, data);
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
chunk.
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
602
|
+
function writeChunkedData(buffers, writeFunction, onError) {
|
|
603
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
604
|
+
const packetCapacity = reactNative.Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH;
|
|
605
|
+
let index = 0;
|
|
606
|
+
let chunk = ByteBuffer__default["default"].allocate(packetCapacity);
|
|
607
|
+
while (index < buffers.length) {
|
|
608
|
+
const buffer = buffers[index].toBuffer();
|
|
609
|
+
chunk.append(buffer);
|
|
610
|
+
index += 1;
|
|
611
|
+
if (chunk.offset === packetCapacity || index >= buffers.length) {
|
|
612
|
+
chunk.reset();
|
|
613
|
+
try {
|
|
614
|
+
yield writeFunction(chunk.toString('base64'));
|
|
615
|
+
chunk = ByteBuffer__default["default"].allocate(packetCapacity);
|
|
616
|
+
}
|
|
617
|
+
catch (e) {
|
|
618
|
+
onError(e);
|
|
619
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleWriteCharacteristicError);
|
|
620
|
+
}
|
|
578
621
|
}
|
|
579
622
|
}
|
|
580
|
-
}
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
if (name === 'EmmcFileWrite') {
|
|
626
|
+
yield writeChunkedData(buffers, data => transport$1.writeWithRetry(data), e => {
|
|
627
|
+
this.runPromise = null;
|
|
628
|
+
this.Log.error('writeCharacteristic write error: ', e);
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
else if (name === 'FirmwareUpload') {
|
|
632
|
+
yield writeChunkedData(buffers, (data) => __awaiter(this, void 0, void 0, function* () {
|
|
633
|
+
yield transport$1.writeCharacteristic.writeWithoutResponse(data);
|
|
634
|
+
}), e => {
|
|
635
|
+
this.runPromise = null;
|
|
636
|
+
this.Log.error('writeCharacteristic write error: ', e);
|
|
637
|
+
});
|
|
581
638
|
}
|
|
582
639
|
else {
|
|
583
640
|
for (const o of buffers) {
|
|
@@ -591,10 +648,12 @@ class ReactNativeBleTransport {
|
|
|
591
648
|
if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceDisconnected) {
|
|
592
649
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded);
|
|
593
650
|
}
|
|
594
|
-
if (e.errorCode === reactNativeBlePlx.BleErrorCode.OperationStartFailed) {
|
|
651
|
+
else if (e.errorCode === reactNativeBlePlx.BleErrorCode.OperationStartFailed) {
|
|
595
652
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|
|
596
653
|
}
|
|
597
|
-
|
|
654
|
+
else {
|
|
655
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleWriteCharacteristicError);
|
|
656
|
+
}
|
|
598
657
|
}
|
|
599
658
|
}
|
|
600
659
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.21",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
"lint:fix": "eslint . --fix"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@onekeyfe/hd-shared": "^1.0.
|
|
23
|
-
"@onekeyfe/hd-transport": "^1.0.
|
|
22
|
+
"@onekeyfe/hd-shared": "^1.0.21",
|
|
23
|
+
"@onekeyfe/hd-transport": "^1.0.21",
|
|
24
24
|
"@onekeyfe/react-native-ble-plx": "3.0.1",
|
|
25
25
|
"react-native-ble-manager": "^8.1.0"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "b50c64ebefa5db4435b9d1a4d64409587c8d1416"
|
|
28
28
|
}
|
package/src/BleTransport.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import { Device, Characteristic } from '@onekeyfe/react-native-ble-plx';
|
|
1
|
+
import { Device, Characteristic, BleErrorCode } from '@onekeyfe/react-native-ble-plx';
|
|
2
|
+
import { getLogger, LoggerNames, wait } from '@onekeyfe/hd-core';
|
|
3
|
+
// import { wait } from '@onekeyfe/hd-core/src/utils';
|
|
4
|
+
|
|
5
|
+
const Log = getLogger(LoggerNames.HdBleTransport);
|
|
2
6
|
|
|
3
7
|
export default class BleTransport {
|
|
4
8
|
id: string;
|
|
@@ -13,6 +17,10 @@ export default class BleTransport {
|
|
|
13
17
|
|
|
14
18
|
nofitySubscription?: () => void;
|
|
15
19
|
|
|
20
|
+
static MAX_RETRIES = 5;
|
|
21
|
+
|
|
22
|
+
static RETRY_DELAY = 2000;
|
|
23
|
+
|
|
16
24
|
constructor(
|
|
17
25
|
device: Device,
|
|
18
26
|
writeCharacteristic: Characteristic,
|
|
@@ -24,4 +32,38 @@ export default class BleTransport {
|
|
|
24
32
|
this.notifyCharacteristic = notifyCharacteristic;
|
|
25
33
|
console.log(`BleTransport(${String(this.id)}) new instance`);
|
|
26
34
|
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @description only for pro / touch , while upgrade firmware
|
|
38
|
+
* @param data
|
|
39
|
+
* @param retryCount
|
|
40
|
+
* @returns
|
|
41
|
+
*/
|
|
42
|
+
async writeWithRetry(data: string, retryCount = BleTransport.MAX_RETRIES): Promise<void> {
|
|
43
|
+
try {
|
|
44
|
+
await this.writeCharacteristic.writeWithoutResponse(data);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
Log?.debug(
|
|
47
|
+
`Write retry attempt ${BleTransport.MAX_RETRIES - retryCount + 1}, error: ${error}`
|
|
48
|
+
);
|
|
49
|
+
if (retryCount > 0) {
|
|
50
|
+
await wait(BleTransport.RETRY_DELAY);
|
|
51
|
+
if (
|
|
52
|
+
error.errorCode === BleErrorCode.DeviceDisconnected ||
|
|
53
|
+
error.errorCode === BleErrorCode.CharacteristicNotFound
|
|
54
|
+
) {
|
|
55
|
+
try {
|
|
56
|
+
await this.device.connect();
|
|
57
|
+
await this.device.discoverAllServicesAndCharacteristics();
|
|
58
|
+
} catch (e) {
|
|
59
|
+
Log?.debug(`Connect or discoverAllServicesAndCharacteristics error: ${e}`);
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
Log?.debug(`writeCharacteristic error: ${error}`);
|
|
63
|
+
}
|
|
64
|
+
return this.writeWithRetry(data, retryCount - 1);
|
|
65
|
+
}
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
27
69
|
}
|
package/src/index.ts
CHANGED
|
@@ -374,14 +374,22 @@ export default class ReactNativeBleTransport {
|
|
|
374
374
|
});
|
|
375
375
|
|
|
376
376
|
const disconnectSubscription = device.onDisconnected(() => {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
377
|
+
try {
|
|
378
|
+
this.Log.debug('device disconnect: ', device?.id);
|
|
379
|
+
this.emitter?.emit('device-disconnect', {
|
|
380
|
+
name: device?.name,
|
|
381
|
+
id: device?.id,
|
|
382
|
+
connectId: device?.id,
|
|
383
|
+
});
|
|
384
|
+
if (this.runPromise) {
|
|
385
|
+
this.runPromise.reject(ERRORS.TypedError(HardwareErrorCode.BleConnectedError));
|
|
386
|
+
}
|
|
387
|
+
} catch (e) {
|
|
388
|
+
this.Log.debug('device disconnect error: ', e);
|
|
389
|
+
} finally {
|
|
390
|
+
this.release(uuid);
|
|
391
|
+
disconnectSubscription?.remove();
|
|
392
|
+
}
|
|
385
393
|
});
|
|
386
394
|
|
|
387
395
|
return { uuid };
|
|
@@ -415,19 +423,17 @@ export default class ReactNativeBleTransport {
|
|
|
415
423
|
error.reason?.includes('The handle is invalid')
|
|
416
424
|
) {
|
|
417
425
|
this.runPromise.reject(
|
|
418
|
-
ERRORS.TypedError(
|
|
419
|
-
HardwareErrorCode.BleCharacteristicNotifyChangeFailure,
|
|
420
|
-
error.message ?? error.reason
|
|
421
|
-
)
|
|
426
|
+
ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotifyChangeFailure)
|
|
422
427
|
);
|
|
423
428
|
this.Log.debug(
|
|
424
429
|
`${HardwareErrorCode.BleCharacteristicNotifyChangeFailure} ${error.message} ${error.reason}`
|
|
425
430
|
);
|
|
426
431
|
return;
|
|
427
432
|
}
|
|
428
|
-
this.runPromise.reject(ERRORS.TypedError(ERROR
|
|
429
|
-
this.Log.debug(': monitor notify error, and has unreleased Promise');
|
|
433
|
+
this.runPromise.reject(ERRORS.TypedError(ERROR));
|
|
434
|
+
this.Log.debug(': monitor notify error, and has unreleased Promise', Error);
|
|
430
435
|
}
|
|
436
|
+
|
|
431
437
|
return;
|
|
432
438
|
}
|
|
433
439
|
|
|
@@ -459,7 +465,7 @@ export default class ReactNativeBleTransport {
|
|
|
459
465
|
}
|
|
460
466
|
} catch (error) {
|
|
461
467
|
this.Log.debug('monitor data error: ', error);
|
|
462
|
-
this.runPromise?.reject(
|
|
468
|
+
this.runPromise?.reject(ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError));
|
|
463
469
|
}
|
|
464
470
|
});
|
|
465
471
|
|
|
@@ -516,30 +522,55 @@ export default class ReactNativeBleTransport {
|
|
|
516
522
|
this.Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
|
|
517
523
|
}
|
|
518
524
|
|
|
519
|
-
const buffers = buildBuffers(messages, name, data)
|
|
525
|
+
const buffers = buildBuffers(messages, name, data) as Array<ByteBuffer>;
|
|
520
526
|
|
|
521
|
-
|
|
527
|
+
async function writeChunkedData(
|
|
528
|
+
buffers: ByteBuffer[],
|
|
529
|
+
writeFunction: (data: string) => Promise<void>,
|
|
530
|
+
onError: (e: any) => void
|
|
531
|
+
) {
|
|
522
532
|
const packetCapacity = Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH;
|
|
523
533
|
let index = 0;
|
|
524
534
|
let chunk = ByteBuffer.allocate(packetCapacity);
|
|
535
|
+
|
|
525
536
|
while (index < buffers.length) {
|
|
526
537
|
const buffer = buffers[index].toBuffer();
|
|
527
538
|
chunk.append(buffer);
|
|
528
539
|
index += 1;
|
|
540
|
+
|
|
529
541
|
if (chunk.offset === packetCapacity || index >= buffers.length) {
|
|
530
542
|
chunk.reset();
|
|
531
|
-
// Upgrading Packet Logs, Too much content to ignore
|
|
532
|
-
// this.Log.debug('send more packet hex strting: ', chunk.toString('hex'));
|
|
533
543
|
try {
|
|
534
|
-
await
|
|
544
|
+
await writeFunction(chunk.toString('base64'));
|
|
535
545
|
chunk = ByteBuffer.allocate(packetCapacity);
|
|
536
546
|
} catch (e) {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
return;
|
|
547
|
+
onError(e);
|
|
548
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError);
|
|
540
549
|
}
|
|
541
550
|
}
|
|
542
551
|
}
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
if (name === 'EmmcFileWrite') {
|
|
555
|
+
await writeChunkedData(
|
|
556
|
+
buffers,
|
|
557
|
+
data => transport.writeWithRetry(data),
|
|
558
|
+
e => {
|
|
559
|
+
this.runPromise = null;
|
|
560
|
+
this.Log.error('writeCharacteristic write error: ', e);
|
|
561
|
+
}
|
|
562
|
+
);
|
|
563
|
+
} else if (name === 'FirmwareUpload') {
|
|
564
|
+
await writeChunkedData(
|
|
565
|
+
buffers,
|
|
566
|
+
async data => {
|
|
567
|
+
await transport.writeCharacteristic.writeWithoutResponse(data);
|
|
568
|
+
},
|
|
569
|
+
e => {
|
|
570
|
+
this.runPromise = null;
|
|
571
|
+
this.Log.error('writeCharacteristic write error: ', e);
|
|
572
|
+
}
|
|
573
|
+
);
|
|
543
574
|
} else {
|
|
544
575
|
for (const o of buffers) {
|
|
545
576
|
const outData = o.toString('base64');
|
|
@@ -552,11 +583,11 @@ export default class ReactNativeBleTransport {
|
|
|
552
583
|
this.runPromise = null;
|
|
553
584
|
if (e.errorCode === BleErrorCode.DeviceDisconnected) {
|
|
554
585
|
throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded);
|
|
555
|
-
}
|
|
556
|
-
if (e.errorCode === BleErrorCode.OperationStartFailed) {
|
|
586
|
+
} else if (e.errorCode === BleErrorCode.OperationStartFailed) {
|
|
557
587
|
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError, e.reason);
|
|
588
|
+
} else {
|
|
589
|
+
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError);
|
|
558
590
|
}
|
|
559
|
-
return;
|
|
560
591
|
}
|
|
561
592
|
}
|
|
562
593
|
}
|