@onekeyfe/hd-transport-react-native 1.2.2-alpha.116 → 1.2.2-alpha.118
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.d.ts +13 -59
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +91 -179
- package/package.json +5 -5
- package/src/__tests__/connectTimeout.test.ts +18 -38
- package/src/__tests__/protocolV2Link.test.ts +255 -298
- package/src/index.ts +135 -330
package/dist/index.js
CHANGED
|
@@ -384,8 +384,7 @@ const shouldRethrowProtocolProbeError = (error) => {
|
|
|
384
384
|
code === hdShared.HardwareErrorCode.BleDeviceDisconnected ||
|
|
385
385
|
code === hdShared.HardwareErrorCode.BleCharacteristicNotifyError ||
|
|
386
386
|
code === hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure ||
|
|
387
|
-
code === hdShared.HardwareErrorCode.BleWriteCharacteristicError
|
|
388
|
-
isProtocolV2DefaultMtuError(error));
|
|
387
|
+
code === hdShared.HardwareErrorCode.BleWriteCharacteristicError);
|
|
389
388
|
};
|
|
390
389
|
const BLE_WRITE_TIMEOUT_MANAGER_RESET_THRESHOLD = 2;
|
|
391
390
|
const DEVICE_SCAN_TIMEOUT_MS = 3000;
|
|
@@ -433,25 +432,14 @@ const connectOptions = {
|
|
|
433
432
|
const fallbackConnectOptions = {
|
|
434
433
|
timeout: BLE_NATIVE_CONNECT_TIMEOUT_MS,
|
|
435
434
|
};
|
|
436
|
-
const androidConnectOptions = {
|
|
437
|
-
timeout: BLE_NATIVE_CONNECT_TIMEOUT_MS,
|
|
438
|
-
};
|
|
439
435
|
const androidRefreshGattConnectOptions = {
|
|
440
436
|
timeout: BLE_NATIVE_CONNECT_TIMEOUT_MS,
|
|
441
437
|
refreshGatt: 'OnConnected',
|
|
442
438
|
};
|
|
443
439
|
const ANDROID_MTU_EXCHANGE_TIMEOUT_MS = 12000;
|
|
444
440
|
const ANDROID_LINK_DROP_QUIET_MS = 5000;
|
|
445
|
-
const ANDROID_LINK_DROP_TIMEOUT_MS = 8000;
|
|
446
441
|
const ANDROID_LINK_DROP_POLL_MS = 250;
|
|
447
442
|
const isKnownDefaultMtu = (mtu) => typeof mtu === 'number' && Number.isFinite(mtu) && mtu <= 23;
|
|
448
|
-
const DEFAULT_MTU_LINK_MESSAGE = 'BLE link stayed at the default MTU';
|
|
449
|
-
const createDefaultMtuLinkError = (mtu) => hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, `${DEFAULT_MTU_LINK_MESSAGE} ${String(mtu)}, reconnecting on a fresh link`);
|
|
450
|
-
const PROTOCOL_V2_DEFAULT_MTU_MESSAGE = 'Protocol V2 needs a negotiated BLE MTU';
|
|
451
|
-
const isProtocolV2DefaultMtuError = (error) => (error === null || error === void 0 ? void 0 : error.errorCode) === hdShared.HardwareErrorCode.BleConnectedError &&
|
|
452
|
-
typeof (error === null || error === void 0 ? void 0 : error.message) === 'string' &&
|
|
453
|
-
error.message.startsWith(PROTOCOL_V2_DEFAULT_MTU_MESSAGE);
|
|
454
|
-
const createProtocolV2DefaultMtuError = (mtu) => hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, `${PROTOCOL_V2_DEFAULT_MTU_MESSAGE}, current MTU ${String(mtu)}`);
|
|
455
443
|
const isMissingGattShapeError = (error) => {
|
|
456
444
|
const code = error === null || error === void 0 ? void 0 : error.errorCode;
|
|
457
445
|
const message = error === null || error === void 0 ? void 0 : error.message;
|
|
@@ -481,9 +469,10 @@ const isWedgedBleSetupError = (error) => (error === null || error === void 0 ? v
|
|
|
481
469
|
error.message.startsWith(BLE_SETUP_WEDGED_MESSAGE);
|
|
482
470
|
const shouldRethrowBleSetupError = (error) => isConnectTimeoutError(error) || isWedgedBleSetupError(error);
|
|
483
471
|
const isNativeOperationTimeoutError = (error) => (error === null || error === void 0 ? void 0 : error.errorCode) === reactNativeBlePlx.BleErrorCode.OperationTimedOut;
|
|
484
|
-
const
|
|
472
|
+
const isMtuOrCancelledConnectError = (error) => {
|
|
485
473
|
const errorCode = error === null || error === void 0 ? void 0 : error.errorCode;
|
|
486
|
-
return (errorCode === reactNativeBlePlx.BleErrorCode.
|
|
474
|
+
return (errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed ||
|
|
475
|
+
errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled);
|
|
487
476
|
};
|
|
488
477
|
const tryToGetConfiguration = (device) => {
|
|
489
478
|
if (!device || !device.serviceUUIDs)
|
|
@@ -498,7 +487,7 @@ const tryToGetConfiguration = (device) => {
|
|
|
498
487
|
};
|
|
499
488
|
const requestNegotiatedMtu = (device, stage, attempt, cancelTransaction) => __awaiter(void 0, void 0, void 0, function* () {
|
|
500
489
|
if (reactNative.Platform.OS !== 'ios' && reactNative.Platform.OS !== 'android')
|
|
501
|
-
return { device,
|
|
490
|
+
return { device, timedOut: false };
|
|
502
491
|
const transactionId = `${device.id}:mtu:${stage}:${attempt}:${Date.now()}`;
|
|
503
492
|
let timeoutId;
|
|
504
493
|
let timedOut = false;
|
|
@@ -514,7 +503,7 @@ const requestNegotiatedMtu = (device, stage, attempt, cancelTransaction) => __aw
|
|
|
514
503
|
}, BLE_MTU_REQUEST_TIMEOUT_MS);
|
|
515
504
|
}),
|
|
516
505
|
]);
|
|
517
|
-
return { device: mtuDevice,
|
|
506
|
+
return { device: mtuDevice, timedOut: false };
|
|
518
507
|
}
|
|
519
508
|
catch (error) {
|
|
520
509
|
if (timedOut && cancelTransaction) {
|
|
@@ -542,9 +531,10 @@ const requestNegotiatedMtu = (device, stage, attempt, cancelTransaction) => __aw
|
|
|
542
531
|
stage,
|
|
543
532
|
attempt,
|
|
544
533
|
actual: device.mtu,
|
|
534
|
+
timedOut,
|
|
545
535
|
error: error instanceof Error ? error.message : String(error),
|
|
546
536
|
});
|
|
547
|
-
return { device,
|
|
537
|
+
return { device, timedOut };
|
|
548
538
|
}
|
|
549
539
|
finally {
|
|
550
540
|
if (timeoutId)
|
|
@@ -553,7 +543,7 @@ const requestNegotiatedMtu = (device, stage, attempt, cancelTransaction) => __aw
|
|
|
553
543
|
});
|
|
554
544
|
const resolveNegotiatedMtu = (device, cancelTransaction) => shouldRefreshNegotiatedMtu(device.mtu)
|
|
555
545
|
? requestNegotiatedMtu(device, 'connected', 0, cancelTransaction)
|
|
556
|
-
: Promise.resolve({ device,
|
|
546
|
+
: Promise.resolve({ device, timedOut: false });
|
|
557
547
|
function remapError(error) {
|
|
558
548
|
var _a;
|
|
559
549
|
if (error instanceof reactNativeBlePlx.BleError) {
|
|
@@ -590,12 +580,8 @@ class ReactNativeBleTransport {
|
|
|
590
580
|
this.sessionProtocols = new Map();
|
|
591
581
|
this.confirmedProtocolV2 = new Set();
|
|
592
582
|
this.protocolReprobeFailures = new Map();
|
|
593
|
-
this.silentDetections = new
|
|
583
|
+
this.silentDetections = new Map();
|
|
594
584
|
this.androidGattCacheRefreshes = new Set();
|
|
595
|
-
this.androidMtuExchangeTimeoutMs = ANDROID_MTU_EXCHANGE_TIMEOUT_MS;
|
|
596
|
-
this.androidLinkDropQuietMs = ANDROID_LINK_DROP_QUIET_MS;
|
|
597
|
-
this.androidLinkDropTimeoutMs = ANDROID_LINK_DROP_TIMEOUT_MS;
|
|
598
|
-
this.protocolWakeAttempts = new Set();
|
|
599
585
|
this.staleBondErrors = new Map();
|
|
600
586
|
this.acquiringProtocolV2 = new Set();
|
|
601
587
|
this.protocolV2Assemblers = new Map();
|
|
@@ -829,10 +815,9 @@ class ReactNativeBleTransport {
|
|
|
829
815
|
transport.notifySubscription = undefined;
|
|
830
816
|
let { device } = transport;
|
|
831
817
|
const isConnected = yield device.isConnected().catch(() => false);
|
|
832
|
-
const reconnectOptions = reactNative.Platform.OS === 'android' ? androidRefreshGattConnectOptions : connectOptions;
|
|
833
818
|
if (!isConnected) {
|
|
834
819
|
try {
|
|
835
|
-
device = yield this.connectWithTimeout(uuid, () => device.connect(
|
|
820
|
+
device = yield this.connectWithTimeout(uuid, () => device.connect(connectOptions));
|
|
836
821
|
}
|
|
837
822
|
catch (e) {
|
|
838
823
|
if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed ||
|
|
@@ -845,16 +830,7 @@ class ReactNativeBleTransport {
|
|
|
845
830
|
}
|
|
846
831
|
}
|
|
847
832
|
const { writeCharacteristic, notifyCharacteristic } = yield this.resolveCharacteristicsWithTimeout(uuid, device);
|
|
848
|
-
if (reactNative.Platform.OS === 'android') {
|
|
849
|
-
const manager = yield this.getPlxManager();
|
|
850
|
-
device = yield this.negotiateAndroidMtu(uuid, manager, device);
|
|
851
|
-
if (isKnownDefaultMtu(device.mtu)) {
|
|
852
|
-
yield this.dropAndroidLink(uuid, manager, device, 'firmware reconnect default mtu');
|
|
853
|
-
throw createDefaultMtuLinkError(device.mtu);
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
833
|
transport.device = device;
|
|
857
|
-
transport.mtuSize = typeof device.mtu === 'number' ? device.mtu : transport.mtuSize;
|
|
858
834
|
transport.writeCharacteristic = writeCharacteristic;
|
|
859
835
|
transport.notifyCharacteristic = notifyCharacteristic;
|
|
860
836
|
const monitorToken = this.nextMonitorToken;
|
|
@@ -1001,7 +977,7 @@ class ReactNativeBleTransport {
|
|
|
1001
977
|
}
|
|
1002
978
|
installTransportForAcquire(uuid, device, characteristics) {
|
|
1003
979
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1004
|
-
const { writeCharacteristic, notifyCharacteristic } = characteristics !== null && characteristics !== void 0 ? characteristics : (yield this.
|
|
980
|
+
const { writeCharacteristic, notifyCharacteristic } = characteristics !== null && characteristics !== void 0 ? characteristics : (yield this.resolveCharacteristicsWithTimeout(uuid, device));
|
|
1005
981
|
if (this.stopped)
|
|
1006
982
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1007
983
|
const transport$1 = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
|
|
@@ -1079,9 +1055,7 @@ class ReactNativeBleTransport {
|
|
|
1079
1055
|
const isCachedDeviceConnected = yield cachedTransport.device
|
|
1080
1056
|
.isConnected()
|
|
1081
1057
|
.catch(() => false);
|
|
1082
|
-
const isCachedAndroidLinkUsable = reactNative.Platform.OS !== 'android' ||
|
|
1083
|
-
(!isKnownDefaultMtu(cachedTransport.mtuSize) &&
|
|
1084
|
-
!this.androidGattCacheRefreshes.has(uuid));
|
|
1058
|
+
const isCachedAndroidLinkUsable = reactNative.Platform.OS !== 'android' || !this.androidGattCacheRefreshes.has(uuid);
|
|
1085
1059
|
if (isCachedDeviceConnected &&
|
|
1086
1060
|
isCachedAndroidLinkUsable &&
|
|
1087
1061
|
cachedProtocol &&
|
|
@@ -1102,7 +1076,7 @@ class ReactNativeBleTransport {
|
|
|
1102
1076
|
if (isAndroid) {
|
|
1103
1077
|
nativeConnectOptions = refreshAndroidGattCache
|
|
1104
1078
|
? androidRefreshGattConnectOptions
|
|
1105
|
-
:
|
|
1079
|
+
: fallbackConnectOptions;
|
|
1106
1080
|
}
|
|
1107
1081
|
let androidRefreshConnectRan = false;
|
|
1108
1082
|
if (forceCleanRunPromise && this.runPromise) {
|
|
@@ -1113,6 +1087,7 @@ class ReactNativeBleTransport {
|
|
|
1113
1087
|
Log === null || Log === void 0 ? void 0 : Log.debug('Force clean Bluetooth run promise, forceCleanRunPromise: ', forceCleanRunPromise);
|
|
1114
1088
|
}
|
|
1115
1089
|
const blePlxManager = yield this.getPlxManager();
|
|
1090
|
+
let skipPostConnectMtu = false;
|
|
1116
1091
|
try {
|
|
1117
1092
|
yield subscribeBleOn(blePlxManager);
|
|
1118
1093
|
}
|
|
@@ -1162,10 +1137,9 @@ class ReactNativeBleTransport {
|
|
|
1162
1137
|
if (shouldRethrowBleSetupError(e)) {
|
|
1163
1138
|
throw e;
|
|
1164
1139
|
}
|
|
1165
|
-
if (e
|
|
1166
|
-
|
|
1140
|
+
if (isMtuOrCancelledConnectError(e)) {
|
|
1141
|
+
skipPostConnectMtu = true;
|
|
1167
1142
|
Log === null || Log === void 0 ? void 0 : Log.debug('first try to reconnect without params');
|
|
1168
|
-
yield this.runBestEffortNativeOperation('connect cancelled: cancel manager connection', () => blePlxManager.cancelDeviceConnection(uuid));
|
|
1169
1143
|
device = yield this.connectWithTimeout(uuid, () => blePlxManager.connectToDevice(uuid, fallbackConnectOptions));
|
|
1170
1144
|
}
|
|
1171
1145
|
else if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceAlreadyConnected) {
|
|
@@ -1180,7 +1154,9 @@ class ReactNativeBleTransport {
|
|
|
1180
1154
|
if (!device) {
|
|
1181
1155
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, 'unable to connect to device');
|
|
1182
1156
|
}
|
|
1183
|
-
if (
|
|
1157
|
+
if (refreshAndroidGattCache &&
|
|
1158
|
+
!androidRefreshConnectRan &&
|
|
1159
|
+
(yield device.isConnected().catch(() => false))) {
|
|
1184
1160
|
yield this.dropAndroidLink(uuid, blePlxManager, device, 'gatt cache refresh');
|
|
1185
1161
|
if (this.stopped)
|
|
1186
1162
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
@@ -1197,10 +1173,9 @@ class ReactNativeBleTransport {
|
|
|
1197
1173
|
if (shouldRethrowBleSetupError(e)) {
|
|
1198
1174
|
throw e;
|
|
1199
1175
|
}
|
|
1200
|
-
if (e
|
|
1201
|
-
|
|
1176
|
+
if (isMtuOrCancelledConnectError(e)) {
|
|
1177
|
+
skipPostConnectMtu = true;
|
|
1202
1178
|
Log === null || Log === void 0 ? void 0 : Log.debug('second try to reconnect without params');
|
|
1203
|
-
yield this.runBestEffortNativeOperation('connect cancelled: cancel device connection', () => disconnectedDevice.cancelConnection());
|
|
1204
1179
|
try {
|
|
1205
1180
|
device = yield this.connectWithTimeout(uuid, () => disconnectedDevice.connect(fallbackConnectOptions));
|
|
1206
1181
|
}
|
|
@@ -1238,35 +1213,32 @@ class ReactNativeBleTransport {
|
|
|
1238
1213
|
let characteristics;
|
|
1239
1214
|
if (isAndroid) {
|
|
1240
1215
|
if (refreshAndroidGattCache) {
|
|
1241
|
-
characteristics = yield this.
|
|
1216
|
+
characteristics = yield this.resolveCharacteristicsWithTimeout(uuid, device);
|
|
1242
1217
|
if (androidRefreshConnectRan)
|
|
1243
1218
|
this.androidGattCacheRefreshes.delete(uuid);
|
|
1244
1219
|
if (this.stopped)
|
|
1245
1220
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1246
1221
|
}
|
|
1247
1222
|
device = yield this.negotiateAndroidMtu(uuid, blePlxManager, device);
|
|
1248
|
-
if (isKnownDefaultMtu(device.mtu)) {
|
|
1249
|
-
const resetManager = this.abandonStalledConnection(uuid, 'mtu-default');
|
|
1250
|
-
yield this.dropAndroidLink(uuid, blePlxManager, device, 'default mtu');
|
|
1251
|
-
if (resetManager)
|
|
1252
|
-
throw this.createWedgedBleSetupError();
|
|
1253
|
-
throw createDefaultMtuLinkError(device.mtu);
|
|
1254
|
-
}
|
|
1255
1223
|
}
|
|
1256
|
-
else {
|
|
1257
|
-
const
|
|
1258
|
-
device =
|
|
1259
|
-
if (
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1224
|
+
else if (!skipPostConnectMtu) {
|
|
1225
|
+
const mtuResult = yield resolveNegotiatedMtu(device, transactionId => blePlxManager.cancelTransaction(transactionId));
|
|
1226
|
+
device = mtuResult.device;
|
|
1227
|
+
if (mtuResult.timedOut) {
|
|
1228
|
+
if (this.stopped)
|
|
1229
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1230
|
+
Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] post-connect MTU timed out, reconnecting without requesting MTU');
|
|
1231
|
+
const timedOutDevice = device;
|
|
1232
|
+
yield this.runBestEffortNativeOperation('mtu timeout: cancel device connection', () => timedOutDevice.cancelConnection());
|
|
1233
|
+
if (this.stopped)
|
|
1234
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1235
|
+
device = yield this.connectWithTimeout(uuid, () => timedOutDevice.connect(fallbackConnectOptions));
|
|
1264
1236
|
}
|
|
1265
1237
|
}
|
|
1266
1238
|
if (this.stopped)
|
|
1267
1239
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1268
1240
|
const acquiredDevice = device;
|
|
1269
|
-
const { writeCharacteristic, notifyCharacteristic } = characteristics !== null && characteristics !== void 0 ? characteristics : (yield this.
|
|
1241
|
+
const { writeCharacteristic, notifyCharacteristic } = characteristics !== null && characteristics !== void 0 ? characteristics : (yield this.resolveCharacteristicsWithTimeout(uuid, acquiredDevice));
|
|
1270
1242
|
if (this.stopped)
|
|
1271
1243
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1272
1244
|
const protocolHint = expectedProtocol
|
|
@@ -1333,7 +1305,7 @@ class ReactNativeBleTransport {
|
|
|
1333
1305
|
let bufferLength = 0;
|
|
1334
1306
|
let buffer$1 = [];
|
|
1335
1307
|
const subscription = characteristic.monitor((error, c) => {
|
|
1336
|
-
var _a, _b, _c, _d, _e, _f
|
|
1308
|
+
var _a, _b, _c, _d, _e, _f;
|
|
1337
1309
|
const isCurrentMonitor = this.monitorTokens.get(uuid) === monitorToken;
|
|
1338
1310
|
if (error) {
|
|
1339
1311
|
Log === null || Log === void 0 ? void 0 : Log.debug(`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${error}`);
|
|
@@ -1350,38 +1322,29 @@ class ReactNativeBleTransport {
|
|
|
1350
1322
|
this.rememberStaleBondError(uuid, toBleStaleBondHardwareError(error));
|
|
1351
1323
|
return;
|
|
1352
1324
|
}
|
|
1325
|
+
if (reactNative.Platform.OS === 'android' && isStaleGattTableNotifyReason(error.reason)) {
|
|
1326
|
+
this.androidGattCacheRefreshes.add(uuid);
|
|
1327
|
+
}
|
|
1353
1328
|
if (this.getActiveProtocol(uuid) === 'V2') {
|
|
1354
1329
|
let errorCode = hdShared.HardwareErrorCode.BleCharacteristicNotifyError;
|
|
1355
1330
|
if ((_a = error.reason) === null || _a === void 0 ? void 0 : _a.includes('The connection has timed out unexpectedly')) {
|
|
1356
1331
|
errorCode = hdShared.HardwareErrorCode.BleTimeoutError;
|
|
1357
1332
|
}
|
|
1358
|
-
else if ((
|
|
1359
|
-
((
|
|
1360
|
-
((_d = error.reason) === null || _d === void 0 ? void 0 : _d.includes('The handle is invalid')) ||
|
|
1361
|
-
((_e = error.reason) === null || _e === void 0 ? void 0 : _e.includes('Writing is not permitted')) ||
|
|
1362
|
-
((_f = error.reason) === null || _f === void 0 ? void 0 : _f.includes('notify change failed for device'))) {
|
|
1333
|
+
else if (isStaleGattTableNotifyReason(error.reason) ||
|
|
1334
|
+
((_b = error.reason) === null || _b === void 0 ? void 0 : _b.includes('notify change failed for device'))) {
|
|
1363
1335
|
errorCode = hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure;
|
|
1364
1336
|
}
|
|
1365
|
-
if (reactNative.Platform.OS === 'android' && isStaleGattTableNotifyReason(error.reason)) {
|
|
1366
|
-
this.androidGattCacheRefreshes.add(uuid);
|
|
1367
|
-
}
|
|
1368
1337
|
this.rejectProtocolV2Frames(uuid, hdShared.ERRORS.TypedError(errorCode));
|
|
1369
1338
|
return;
|
|
1370
1339
|
}
|
|
1371
1340
|
if (this.runPromise && this.runPromiseDeviceId === uuid) {
|
|
1372
1341
|
let ERROR = hdShared.HardwareErrorCode.BleCharacteristicNotifyError;
|
|
1373
|
-
if ((
|
|
1342
|
+
if ((_c = error.reason) === null || _c === void 0 ? void 0 : _c.includes('The connection has timed out unexpectedly')) {
|
|
1374
1343
|
ERROR = hdShared.HardwareErrorCode.BleTimeoutError;
|
|
1375
1344
|
}
|
|
1376
|
-
if ((
|
|
1377
|
-
((
|
|
1378
|
-
((_k = error.reason) === null || _k === void 0 ? void 0 : _k.includes('The handle is invalid')) ||
|
|
1379
|
-
((_l = error.reason) === null || _l === void 0 ? void 0 : _l.includes('Writing is not permitted')) ||
|
|
1380
|
-
((_m = error.reason) === null || _m === void 0 ? void 0 : _m.includes('notify change failed for device'))) {
|
|
1345
|
+
if (isStaleGattTableNotifyReason(error.reason) ||
|
|
1346
|
+
((_d = error.reason) === null || _d === void 0 ? void 0 : _d.includes('notify change failed for device'))) {
|
|
1381
1347
|
const notifyError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure);
|
|
1382
|
-
if (reactNative.Platform.OS === 'android' && isStaleGattTableNotifyReason(error.reason)) {
|
|
1383
|
-
this.androidGattCacheRefreshes.add(uuid);
|
|
1384
|
-
}
|
|
1385
1348
|
this.runPromise.reject(notifyError);
|
|
1386
1349
|
Log === null || Log === void 0 ? void 0 : Log.debug(`${hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure} ${error.message} ${error.reason}`);
|
|
1387
1350
|
return;
|
|
@@ -1422,7 +1385,7 @@ class ReactNativeBleTransport {
|
|
|
1422
1385
|
bufferLength = 0;
|
|
1423
1386
|
buffer$1 = [];
|
|
1424
1387
|
if (this.runPromiseDeviceId === uuid) {
|
|
1425
|
-
(
|
|
1388
|
+
(_e = this.runPromise) === null || _e === void 0 ? void 0 : _e.resolve(value.toString('hex'));
|
|
1426
1389
|
}
|
|
1427
1390
|
}
|
|
1428
1391
|
}
|
|
@@ -1433,7 +1396,7 @@ class ReactNativeBleTransport {
|
|
|
1433
1396
|
this.rejectProtocolV2Frames(uuid, notifyError);
|
|
1434
1397
|
}
|
|
1435
1398
|
else if (this.runPromiseDeviceId === uuid) {
|
|
1436
|
-
(
|
|
1399
|
+
(_f = this.runPromise) === null || _f === void 0 ? void 0 : _f.reject(notifyError);
|
|
1437
1400
|
}
|
|
1438
1401
|
}
|
|
1439
1402
|
}, notifyTransactionId);
|
|
@@ -1709,13 +1672,14 @@ class ReactNativeBleTransport {
|
|
|
1709
1672
|
return check.call(jsonData);
|
|
1710
1673
|
}
|
|
1711
1674
|
catch (e) {
|
|
1712
|
-
|
|
1713
|
-
|
|
1675
|
+
const isProbeTimeout = (options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS &&
|
|
1676
|
+
(name === 'GetFeatures' || name === 'Initialize');
|
|
1677
|
+
if (isProbeTimeout) {
|
|
1678
|
+
Log === null || Log === void 0 ? void 0 : Log.debug(`[ReactNativeBleTransport] Protocol V1 ${name} probe call failed:`, e);
|
|
1714
1679
|
}
|
|
1715
1680
|
else {
|
|
1716
1681
|
Log === null || Log === void 0 ? void 0 : Log.error('call error: ', e);
|
|
1717
1682
|
}
|
|
1718
|
-
const isProbeTimeout = name === 'GetFeatures' && (options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS;
|
|
1719
1683
|
const isStaleCall = this.runPromise !== runPromise;
|
|
1720
1684
|
if (!isProbeTimeout &&
|
|
1721
1685
|
!isStaleCall &&
|
|
@@ -1921,51 +1885,46 @@ class ReactNativeBleTransport {
|
|
|
1921
1885
|
if (!shouldRefreshNegotiatedMtu(device.mtu))
|
|
1922
1886
|
return device;
|
|
1923
1887
|
const startedAt = Date.now();
|
|
1924
|
-
const transactionId = `${device.id}:mtu:connected:0:${startedAt}`;
|
|
1925
1888
|
let timer;
|
|
1926
1889
|
let timedOut = false;
|
|
1927
|
-
|
|
1928
|
-
|
|
1890
|
+
let negotiated = device;
|
|
1891
|
+
let failure;
|
|
1929
1892
|
try {
|
|
1930
|
-
|
|
1931
|
-
|
|
1893
|
+
negotiated = yield Promise.race([
|
|
1894
|
+
device.requestMTU(ANDROID_REQUEST_MTU, `${device.id}:mtu:connected:0:${startedAt}`),
|
|
1932
1895
|
new Promise((_, reject) => {
|
|
1933
1896
|
timer = setTimeout(() => {
|
|
1934
1897
|
timedOut = true;
|
|
1935
|
-
reject(new Error(`BLE MTU exchange timeout after ${
|
|
1936
|
-
},
|
|
1898
|
+
reject(new Error(`BLE MTU exchange timeout after ${ANDROID_MTU_EXCHANGE_TIMEOUT_MS}ms`));
|
|
1899
|
+
}, ANDROID_MTU_EXCHANGE_TIMEOUT_MS);
|
|
1937
1900
|
}),
|
|
1938
1901
|
]);
|
|
1939
|
-
Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] BLE MTU exchange completed', {
|
|
1940
|
-
connectIdSuffix: uuid.slice(-8),
|
|
1941
|
-
elapsedMs: Date.now() - startedAt,
|
|
1942
|
-
actual: negotiated.mtu,
|
|
1943
|
-
});
|
|
1944
|
-
return negotiated;
|
|
1945
1902
|
}
|
|
1946
1903
|
catch (error) {
|
|
1947
|
-
|
|
1948
|
-
connectIdSuffix: uuid.slice(-8),
|
|
1949
|
-
elapsedMs: Date.now() - startedAt,
|
|
1950
|
-
timedOut,
|
|
1951
|
-
actual: device.mtu,
|
|
1952
|
-
error: error instanceof Error ? error.message : String(error),
|
|
1953
|
-
});
|
|
1954
|
-
if (this.stopped)
|
|
1955
|
-
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1956
|
-
if (!timedOut)
|
|
1957
|
-
return device;
|
|
1958
|
-
yield Promise.resolve(manager.cancelTransaction(transactionId)).catch(() => undefined);
|
|
1959
|
-
const resetManager = this.abandonStalledConnection(uuid, 'mtu-backstop');
|
|
1960
|
-
yield this.dropAndroidLink(uuid, manager, device, 'mtu exchange timeout');
|
|
1961
|
-
if (resetManager)
|
|
1962
|
-
throw this.createWedgedBleSetupError();
|
|
1963
|
-
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, 'BLE MTU exchange did not complete, reconnecting on a fresh link');
|
|
1904
|
+
failure = error instanceof Error ? error.message : String(error);
|
|
1964
1905
|
}
|
|
1965
1906
|
finally {
|
|
1966
1907
|
if (timer)
|
|
1967
1908
|
clearTimeout(timer);
|
|
1968
1909
|
}
|
|
1910
|
+
Log === null || Log === void 0 ? void 0 : Log.debug(`[ReactNativeBleTransport] BLE MTU exchange ${failure ? 'failed' : 'completed'}`, {
|
|
1911
|
+
connectIdSuffix: uuid.slice(-8),
|
|
1912
|
+
elapsedMs: Date.now() - startedAt,
|
|
1913
|
+
timedOut,
|
|
1914
|
+
actual: negotiated.mtu,
|
|
1915
|
+
error: failure,
|
|
1916
|
+
});
|
|
1917
|
+
if (this.stopped)
|
|
1918
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
|
|
1919
|
+
if (!timedOut && !isKnownDefaultMtu(negotiated.mtu))
|
|
1920
|
+
return negotiated;
|
|
1921
|
+
const resetManager = this.abandonStalledConnection(uuid, timedOut ? 'mtu-backstop' : 'mtu-default');
|
|
1922
|
+
yield this.dropAndroidLink(uuid, manager, negotiated, timedOut ? 'mtu exchange timeout' : 'default mtu');
|
|
1923
|
+
if (resetManager)
|
|
1924
|
+
throw this.createWedgedBleSetupError();
|
|
1925
|
+
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, timedOut
|
|
1926
|
+
? 'BLE MTU exchange did not complete, reconnecting on a fresh link'
|
|
1927
|
+
: `BLE link stayed at the default MTU ${negotiated.mtu}, reconnecting on a fresh link`);
|
|
1969
1928
|
});
|
|
1970
1929
|
}
|
|
1971
1930
|
dropAndroidLink(uuid, manager, device, reason) {
|
|
@@ -1977,50 +1936,16 @@ class ReactNativeBleTransport {
|
|
|
1977
1936
|
]);
|
|
1978
1937
|
}));
|
|
1979
1938
|
const startedAt = Date.now();
|
|
1980
|
-
|
|
1981
|
-
let registryClear = false;
|
|
1982
|
-
let quietPeriodElapsed = false;
|
|
1983
|
-
while (!this.stopped) {
|
|
1984
|
-
const elapsed = Date.now() - startedAt;
|
|
1985
|
-
quietPeriodElapsed = elapsed >= this.androidLinkDropQuietMs;
|
|
1986
|
-
if (!registryClear) {
|
|
1987
|
-
const connected = yield getConnectedDeviceIds([]).catch(() => undefined);
|
|
1988
|
-
registryClear =
|
|
1989
|
-
!!connected &&
|
|
1990
|
-
!connected.some(peripheral => { var _a; return String((_a = peripheral === null || peripheral === void 0 ? void 0 : peripheral.id) !== null && _a !== void 0 ? _a : '').toUpperCase() === target; });
|
|
1991
|
-
}
|
|
1992
|
-
if ((registryClear && quietPeriodElapsed) || elapsed >= this.androidLinkDropTimeoutMs) {
|
|
1993
|
-
break;
|
|
1994
|
-
}
|
|
1939
|
+
while (!this.stopped && Date.now() - startedAt < ANDROID_LINK_DROP_QUIET_MS) {
|
|
1995
1940
|
yield delay(ANDROID_LINK_DROP_POLL_MS);
|
|
1996
1941
|
}
|
|
1997
1942
|
Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Android BLE link drop', {
|
|
1998
1943
|
connectIdSuffix: uuid.slice(-8),
|
|
1999
1944
|
reason,
|
|
2000
|
-
registryClear,
|
|
2001
|
-
quietPeriodElapsed,
|
|
2002
1945
|
stopped: this.stopped,
|
|
2003
|
-
elapsedMs: Date.now() - startedAt,
|
|
2004
1946
|
});
|
|
2005
1947
|
});
|
|
2006
1948
|
}
|
|
2007
|
-
resolveCharacteristicsForAcquire(uuid, device) {
|
|
2008
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
2009
|
-
try {
|
|
2010
|
-
return yield this.resolveCharacteristicsWithTimeout(uuid, device);
|
|
2011
|
-
}
|
|
2012
|
-
catch (error) {
|
|
2013
|
-
if (reactNative.Platform.OS === 'android' && isMissingGattShapeError(error)) {
|
|
2014
|
-
this.androidGattCacheRefreshes.add(uuid);
|
|
2015
|
-
const manager = this.blePlxManager;
|
|
2016
|
-
if (manager) {
|
|
2017
|
-
yield this.dropAndroidLink(uuid, manager, device, 'stale gatt table');
|
|
2018
|
-
}
|
|
2019
|
-
}
|
|
2020
|
-
throw error;
|
|
2021
|
-
}
|
|
2022
|
-
});
|
|
2023
|
-
}
|
|
2024
1949
|
connectWithTimeout(uuid, connect) {
|
|
2025
1950
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2026
1951
|
if (this.stopped)
|
|
@@ -2088,12 +2013,15 @@ class ReactNativeBleTransport {
|
|
|
2088
2013
|
return result;
|
|
2089
2014
|
}
|
|
2090
2015
|
catch (error) {
|
|
2091
|
-
if (timedOut ||
|
|
2016
|
+
if (timedOut || isNativeOperationTimeoutError(error)) {
|
|
2092
2017
|
const resetManager = this.abandonStalledConnection(uuid, timedOut ? 'gatt-backstop' : 'gatt-native');
|
|
2093
2018
|
if (resetManager) {
|
|
2094
2019
|
throw this.createWedgedBleSetupError();
|
|
2095
2020
|
}
|
|
2096
2021
|
}
|
|
2022
|
+
if (reactNative.Platform.OS === 'android' && isMissingGattShapeError(error)) {
|
|
2023
|
+
this.androidGattCacheRefreshes.add(uuid);
|
|
2024
|
+
}
|
|
2097
2025
|
throw error;
|
|
2098
2026
|
}
|
|
2099
2027
|
finally {
|
|
@@ -2246,7 +2174,6 @@ class ReactNativeBleTransport {
|
|
|
2246
2174
|
this.sessionProtocols.clear();
|
|
2247
2175
|
this.protocolReprobeFailures.clear();
|
|
2248
2176
|
this.silentDetections.clear();
|
|
2249
|
-
this.protocolWakeAttempts.clear();
|
|
2250
2177
|
this.writeTimeoutCounts.clear();
|
|
2251
2178
|
this.connectionSetupTimeoutCounts.clear();
|
|
2252
2179
|
this.monitorTokens.clear();
|
|
@@ -2341,7 +2268,6 @@ class ReactNativeBleTransport {
|
|
|
2341
2268
|
}
|
|
2342
2269
|
this.protocolReprobeFailures.delete(uuid);
|
|
2343
2270
|
this.silentDetections.delete(uuid);
|
|
2344
|
-
this.protocolWakeAttempts.delete(uuid);
|
|
2345
2271
|
Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] protocol detected', {
|
|
2346
2272
|
deviceId: uuid,
|
|
2347
2273
|
protocol,
|
|
@@ -2350,7 +2276,8 @@ class ReactNativeBleTransport {
|
|
|
2350
2276
|
return protocol;
|
|
2351
2277
|
}
|
|
2352
2278
|
}
|
|
2353
|
-
this.silentDetections.
|
|
2279
|
+
if (!this.silentDetections.has(uuid))
|
|
2280
|
+
this.silentDetections.set(uuid, 'silent');
|
|
2354
2281
|
if (trustSessionProtocol) {
|
|
2355
2282
|
this.protocolReprobeFailures.set(uuid, reprobeFailures + 1);
|
|
2356
2283
|
}
|
|
@@ -2364,17 +2291,12 @@ class ReactNativeBleTransport {
|
|
|
2364
2291
|
}
|
|
2365
2292
|
wakeSilentProtocolV1Device(uuid, probeOrder) {
|
|
2366
2293
|
return __awaiter(this, void 0, void 0, function* () {
|
|
2367
|
-
if (
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
return;
|
|
2371
|
-
if (!this.silentDetections.has(uuid))
|
|
2294
|
+
if (reactNative.Platform.OS !== 'android' ||
|
|
2295
|
+
!probeOrder.includes('V1') ||
|
|
2296
|
+
this.silentDetections.get(uuid) !== 'silent') {
|
|
2372
2297
|
return;
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
if (!transportCache[uuid])
|
|
2376
|
-
return;
|
|
2377
|
-
this.protocolWakeAttempts.add(uuid);
|
|
2298
|
+
}
|
|
2299
|
+
this.silentDetections.set(uuid, 'woken');
|
|
2378
2300
|
Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] sending Protocol V1 Initialize wake', {
|
|
2379
2301
|
connectIdSuffix: uuid.slice(-8),
|
|
2380
2302
|
});
|
|
@@ -2383,11 +2305,8 @@ class ReactNativeBleTransport {
|
|
|
2383
2305
|
yield this.callProtocolV1(uuid, 'Initialize', {}, { timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS });
|
|
2384
2306
|
}
|
|
2385
2307
|
catch (error) {
|
|
2386
|
-
if (shouldRethrowProtocolProbeError(error))
|
|
2387
|
-
this.clearProbeProtocol(uuid, 'V1');
|
|
2308
|
+
if (shouldRethrowProtocolProbeError(error))
|
|
2388
2309
|
throw error;
|
|
2389
|
-
}
|
|
2390
|
-
Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Protocol V1 Initialize wake did not answer:', error);
|
|
2391
2310
|
}
|
|
2392
2311
|
finally {
|
|
2393
2312
|
this.clearProbeProtocol(uuid, 'V1');
|
|
@@ -2648,12 +2567,6 @@ class ReactNativeBleTransport {
|
|
|
2648
2567
|
if (!this._messages || !this._messagesV2) {
|
|
2649
2568
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
|
|
2650
2569
|
}
|
|
2651
|
-
if (reactNative.Platform.OS === 'android') {
|
|
2652
|
-
const activeTransport = transportCache[uuid];
|
|
2653
|
-
if (activeTransport && isKnownDefaultMtu(activeTransport.mtuSize)) {
|
|
2654
|
-
throw createProtocolV2DefaultMtuError(activeTransport.mtuSize);
|
|
2655
|
-
}
|
|
2656
|
-
}
|
|
2657
2570
|
const isProtocolProbe = this.probingProtocols.get(uuid) === 'V2';
|
|
2658
2571
|
const callOptions = options;
|
|
2659
2572
|
const highThroughputWrite = transport.isProtocolV2HighThroughputCall(name);
|
|
@@ -2717,11 +2630,11 @@ class ReactNativeBleTransport {
|
|
|
2717
2630
|
const transport = this.getCachedTransport(uuid);
|
|
2718
2631
|
if (!shouldRefreshNegotiatedMtu(transport.mtuSize))
|
|
2719
2632
|
return;
|
|
2720
|
-
const { device: refreshedDevice
|
|
2633
|
+
const { device: refreshedDevice } = yield requestNegotiatedMtu(transport.device, 'highThroughput', 1, transactionId => { var _a; return (_a = this.blePlxManager) === null || _a === void 0 ? void 0 : _a.cancelTransaction(transactionId); });
|
|
2721
2634
|
transport.device = refreshedDevice;
|
|
2722
2635
|
transport.mtuSize =
|
|
2723
2636
|
typeof refreshedDevice.mtu === 'number' ? refreshedDevice.mtu : transport.mtuSize;
|
|
2724
|
-
if (
|
|
2637
|
+
if (shouldRefreshNegotiatedMtu(transport.mtuSize)) {
|
|
2725
2638
|
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, `Protocol V2 high-throughput BLE MTU unavailable: ${String(transport.mtuSize)}`);
|
|
2726
2639
|
}
|
|
2727
2640
|
});
|
|
@@ -2835,7 +2748,6 @@ class ReactNativeBleTransport {
|
|
|
2835
2748
|
}
|
|
2836
2749
|
|
|
2837
2750
|
exports.ANDROID_LINK_DROP_QUIET_MS = ANDROID_LINK_DROP_QUIET_MS;
|
|
2838
|
-
exports.ANDROID_LINK_DROP_TIMEOUT_MS = ANDROID_LINK_DROP_TIMEOUT_MS;
|
|
2839
2751
|
exports.ANDROID_MTU_EXCHANGE_TIMEOUT_MS = ANDROID_MTU_EXCHANGE_TIMEOUT_MS;
|
|
2840
2752
|
exports.BLE_CONNECT_TIMEOUT_MANAGER_RESET_THRESHOLD = BLE_CONNECT_TIMEOUT_MANAGER_RESET_THRESHOLD;
|
|
2841
2753
|
exports.BLE_CONNECT_TIMEOUT_MS = BLE_CONNECT_TIMEOUT_MS;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-react-native",
|
|
3
|
-
"version": "1.2.2-alpha.
|
|
3
|
+
"version": "1.2.2-alpha.118",
|
|
4
4
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -20,11 +20,11 @@
|
|
|
20
20
|
"lint:fix": "eslint . --fix"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@onekeyfe/hd-core": "1.2.2-alpha.
|
|
24
|
-
"@onekeyfe/hd-shared": "1.2.2-alpha.
|
|
25
|
-
"@onekeyfe/hd-transport": "1.2.2-alpha.
|
|
23
|
+
"@onekeyfe/hd-core": "1.2.2-alpha.118",
|
|
24
|
+
"@onekeyfe/hd-shared": "1.2.2-alpha.118",
|
|
25
|
+
"@onekeyfe/hd-transport": "1.2.2-alpha.118",
|
|
26
26
|
"@onekeyfe/react-native-ble-utils": "0.1.6",
|
|
27
27
|
"react-native-ble-plx": "3.5.1"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "f73c3ec61e0ffe97c1d38b18058a18e169a72217"
|
|
30
30
|
}
|