@onekeyfe/hd-transport-react-native 1.2.8-alpha.7 → 1.2.8

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 CHANGED
@@ -46,6 +46,76 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
46
46
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
47
47
  };
48
48
 
49
+ const lastKeyMissingAt = new Map();
50
+ const listeners$1 = new Set();
51
+ let unsubscribeNative$1;
52
+ const normalizeDeviceId$1 = (deviceId) => deviceId.toLowerCase();
53
+ const getCapableBleUtils$1 = () => {
54
+ if (reactNative.Platform.OS !== 'android')
55
+ return undefined;
56
+ const bleUtils = BleUtils__default["default"];
57
+ if (typeof bleUtils.supportsDeviceKeyMissing !== 'function' ||
58
+ typeof bleUtils.onDeviceKeyMissing !== 'function') {
59
+ return undefined;
60
+ }
61
+ return bleUtils.supportsDeviceKeyMissing() ? bleUtils : undefined;
62
+ };
63
+ const isBleKeyMissingSupported = () => getCapableBleUtils$1() !== undefined;
64
+ const startBleKeyMissingTracking = () => {
65
+ if (unsubscribeNative$1)
66
+ return true;
67
+ const bleUtils = getCapableBleUtils$1();
68
+ if (!(bleUtils === null || bleUtils === void 0 ? void 0 : bleUtils.onDeviceKeyMissing))
69
+ return false;
70
+ unsubscribeNative$1 = bleUtils.onDeviceKeyMissing(event => {
71
+ if (typeof (event === null || event === void 0 ? void 0 : event.id) !== 'string')
72
+ return;
73
+ const deviceId = normalizeDeviceId$1(event.id);
74
+ lastKeyMissingAt.set(deviceId, Date.now());
75
+ listeners$1.forEach(listener => listener(deviceId));
76
+ });
77
+ return true;
78
+ };
79
+ const stopBleKeyMissingTracking = () => {
80
+ unsubscribeNative$1 === null || unsubscribeNative$1 === void 0 ? void 0 : unsubscribeNative$1();
81
+ unsubscribeNative$1 = undefined;
82
+ lastKeyMissingAt.clear();
83
+ listeners$1.clear();
84
+ };
85
+ const hasBleKeyMissingSince = (deviceId, sinceMs) => {
86
+ const at = lastKeyMissingAt.get(normalizeDeviceId$1(deviceId));
87
+ return at !== undefined && at >= sinceMs;
88
+ };
89
+ const onBleKeyMissing = (deviceId, callback) => {
90
+ const target = normalizeDeviceId$1(deviceId);
91
+ const listener = id => {
92
+ if (id === target)
93
+ callback();
94
+ };
95
+ listeners$1.add(listener);
96
+ return () => {
97
+ listeners$1.delete(listener);
98
+ };
99
+ };
100
+ const waitForBleKeyMissing = (deviceId, sinceMs, timeoutMs) => {
101
+ if (!startBleKeyMissingTracking())
102
+ return Promise.resolve(false);
103
+ if (hasBleKeyMissingSince(deviceId, sinceMs))
104
+ return Promise.resolve(true);
105
+ return new Promise(resolve => {
106
+ const settle = (found) => {
107
+ clearTimeout(timer);
108
+ cleanup();
109
+ resolve(found);
110
+ };
111
+ const cleanup = onBleKeyMissing(deviceId, () => {
112
+ if (hasBleKeyMissingSince(deviceId, sinceMs))
113
+ settle(true);
114
+ });
115
+ const timer = setTimeout(() => settle(false), timeoutMs);
116
+ });
117
+ };
118
+
49
119
  let activeLogger;
50
120
  const setBleLogger = (logger) => {
51
121
  activeLogger = logger;
@@ -98,6 +168,7 @@ const onDeviceBondState = (bleMacAddress, signal, options) => new Promise((resol
98
168
  clearTimeout(pendingFailure);
99
169
  if (cleanupListener)
100
170
  cleanupListener();
171
+ cleanupKeyMissing === null || cleanupKeyMissing === void 0 ? void 0 : cleanupKeyMissing();
101
172
  signal === null || signal === void 0 ? void 0 : signal.removeEventListener('abort', onAbort);
102
173
  };
103
174
  const onAbort = () => {
@@ -112,6 +183,15 @@ const onDeviceBondState = (bleMacAddress, signal, options) => new Promise((resol
112
183
  reason: 'timeout',
113
184
  }));
114
185
  }, 60 * 1000);
186
+ const cleanupKeyMissing = startBleKeyMissingTracking()
187
+ ? onBleKeyMissing(bleMacAddress, () => {
188
+ cleanup();
189
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleBondInvalid, undefined, {
190
+ phase: 'bond',
191
+ reason: 'key_missing',
192
+ }));
193
+ })
194
+ : undefined;
115
195
  const cleanupListener = BleUtils__default["default"].onDeviceBondState(peripheral => {
116
196
  var _a;
117
197
  if (((_a = peripheral.id) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== bleMacAddress.toLowerCase()) {
@@ -228,53 +308,252 @@ const timer = process.env.NODE_ENV === 'development'
228
308
  },
229
309
  };
230
310
 
231
- const subscribeBleOn = (bleManager, ms = 2000) => new Promise((resolve, reject) => {
311
+ const subscribeBleOn = (bleManager, ms = 1000) => new Promise((resolve, reject) => {
232
312
  let done = false;
233
- let pendingError;
234
- let cancelTimeout = () => undefined;
235
- let removeSubscription = () => undefined;
236
- const finish = (error) => {
237
- if (done)
238
- return;
239
- done = true;
240
- cancelTimeout();
241
- removeSubscription();
242
- if (error)
243
- reject(error);
244
- else
245
- resolve();
246
- };
247
313
  const subscription = bleManager.onStateChange(state => {
248
314
  if (state === 'PoweredOn') {
249
- finish();
250
- }
251
- else if (state === 'PoweredOff') {
252
- pendingError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
315
+ if (done)
316
+ return;
317
+ clearTimeout();
318
+ done = true;
319
+ subscription.remove();
320
+ resolve();
253
321
  }
254
- else if (state === 'Unsupported') {
255
- pendingError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
322
+ }, true);
323
+ const clearTimeout = timer.timeout(() => {
324
+ if (done)
325
+ return;
326
+ subscription.remove();
327
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError));
328
+ }, ms);
329
+ });
330
+
331
+ const HCI_PIN_OR_KEY_MISSING = 0x06;
332
+ const lastEncryption = new Map();
333
+ const lastAclDisconnectAt = new Map();
334
+ const listeners = new Set();
335
+ let unsubscribeNative;
336
+ const normalizeDeviceId = (deviceId) => deviceId.toLowerCase();
337
+ const isEncrypted = (record) => record.status === 0 && record.enabled;
338
+ const getCapableBleUtils = () => {
339
+ if (reactNative.Platform.OS !== 'android')
340
+ return undefined;
341
+ const bleUtils = BleUtils__default["default"];
342
+ if (typeof bleUtils.supportsDeviceEncryptionChange !== 'function' ||
343
+ typeof bleUtils.onDeviceEncryptionChange !== 'function' ||
344
+ typeof bleUtils.onDeviceAclDisconnected !== 'function') {
345
+ return undefined;
346
+ }
347
+ return bleUtils.supportsDeviceEncryptionChange() ? bleUtils : undefined;
348
+ };
349
+ const notify = (deviceId) => {
350
+ listeners.forEach(listener => listener(deviceId));
351
+ };
352
+ const startBleEncryptionTracking = () => {
353
+ if (unsubscribeNative)
354
+ return true;
355
+ const bleUtils = getCapableBleUtils();
356
+ if (!(bleUtils === null || bleUtils === void 0 ? void 0 : bleUtils.onDeviceEncryptionChange) || !bleUtils.onDeviceAclDisconnected)
357
+ return false;
358
+ unsubscribeNative = [
359
+ bleUtils.onDeviceEncryptionChange(event => {
360
+ if (typeof (event === null || event === void 0 ? void 0 : event.id) !== 'string')
361
+ return;
362
+ const deviceId = normalizeDeviceId(event.id);
363
+ lastEncryption.set(deviceId, {
364
+ at: Date.now(),
365
+ status: Number(event.status),
366
+ enabled: event.enabled === true,
367
+ });
368
+ notify(deviceId);
369
+ }),
370
+ bleUtils.onDeviceAclDisconnected(event => {
371
+ if (typeof (event === null || event === void 0 ? void 0 : event.id) !== 'string')
372
+ return;
373
+ const deviceId = normalizeDeviceId(event.id);
374
+ lastAclDisconnectAt.set(deviceId, Date.now());
375
+ notify(deviceId);
376
+ }),
377
+ ];
378
+ return true;
379
+ };
380
+ const stopBleEncryptionTracking = () => {
381
+ unsubscribeNative === null || unsubscribeNative === void 0 ? void 0 : unsubscribeNative.forEach(unsubscribe => unsubscribe());
382
+ unsubscribeNative = undefined;
383
+ lastEncryption.clear();
384
+ lastAclDisconnectAt.clear();
385
+ listeners.clear();
386
+ };
387
+ const markBleLinkEncrypted = (deviceId) => {
388
+ if (!unsubscribeNative)
389
+ return;
390
+ lastEncryption.set(normalizeDeviceId(deviceId), { at: Date.now(), status: 0, enabled: true });
391
+ };
392
+ const waitForAndroidLinkEncryption = ({ deviceId, linkStartedAt, resultTimeoutMs, repairTimeoutMs, keyMissingGraceMs, signal, onRepairStarted, }) => {
393
+ if (!startBleEncryptionTracking())
394
+ return Promise.resolve('unresolved');
395
+ startBleKeyMissingTracking();
396
+ const target = normalizeDeviceId(deviceId);
397
+ const previous = lastEncryption.get(target);
398
+ const previousDropAt = lastAclDisconnectAt.get(target);
399
+ if (previous &&
400
+ previous.at < linkStartedAt &&
401
+ isEncrypted(previous) &&
402
+ (previousDropAt === undefined || previousDropAt < previous.at)) {
403
+ return Promise.resolve('already-encrypted');
404
+ }
405
+ return new Promise((resolve, reject) => {
406
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
407
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected));
408
+ return;
256
409
  }
257
- else if (state === 'Unauthorized') {
258
- pendingError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationError);
410
+ let repairStartedAt;
411
+ let droppedDuringRepair = false;
412
+ let timer;
413
+ let settled = false;
414
+ const cleanup = () => {
415
+ settled = true;
416
+ if (timer)
417
+ clearTimeout(timer);
418
+ listeners.delete(onLinkEvent);
419
+ cleanupKeyMissing();
420
+ signal === null || signal === void 0 ? void 0 : signal.removeEventListener('abort', onAbort);
421
+ };
422
+ const finish = (result) => {
423
+ cleanup();
424
+ resolve(result);
425
+ };
426
+ const fail = (error) => {
427
+ cleanup();
428
+ reject(error);
429
+ };
430
+ const arm = (ms, onExpire) => {
431
+ if (timer)
432
+ clearTimeout(timer);
433
+ timer = setTimeout(onExpire, ms);
434
+ };
435
+ const bondInvalid = () => hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleBondInvalid, undefined, {
436
+ phase: 'connect',
437
+ reason: 'key_missing',
438
+ });
439
+ const evaluate = () => {
440
+ if (settled)
441
+ return;
442
+ const record = lastEncryption.get(target);
443
+ const dropAt = lastAclDisconnectAt.get(target);
444
+ if (repairStartedAt === undefined) {
445
+ if (hasBleKeyMissingSince(target, linkStartedAt)) {
446
+ fail(bondInvalid());
447
+ return;
448
+ }
449
+ if (dropAt !== undefined && dropAt >= linkStartedAt) {
450
+ finish('unresolved');
451
+ return;
452
+ }
453
+ if (!record || record.at < linkStartedAt)
454
+ return;
455
+ if (isEncrypted(record)) {
456
+ finish('encrypted');
457
+ return;
458
+ }
459
+ if (record.status !== HCI_PIN_OR_KEY_MISSING) {
460
+ finish('unresolved');
461
+ return;
462
+ }
463
+ repairStartedAt = record.at;
464
+ onRepairStarted === null || onRepairStarted === void 0 ? void 0 : onRepairStarted();
465
+ arm(repairTimeoutMs, () => fail(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded, 'Bluetooth pairing timed out', {
466
+ phase: 'bond',
467
+ reason: 'timeout',
468
+ })));
469
+ return;
470
+ }
471
+ if (hasBleKeyMissingSince(target, repairStartedAt)) {
472
+ fail(bondInvalid());
473
+ return;
474
+ }
475
+ if (record && record.at > repairStartedAt && isEncrypted(record)) {
476
+ finish('re-paired');
477
+ return;
478
+ }
479
+ if (!droppedDuringRepair && dropAt !== undefined && dropAt >= repairStartedAt) {
480
+ droppedDuringRepair = true;
481
+ arm(keyMissingGraceMs, () => fail(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected)));
482
+ }
483
+ };
484
+ const onLinkEvent = id => {
485
+ if (id === target)
486
+ evaluate();
487
+ };
488
+ const onAbort = () => fail(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected));
489
+ listeners.add(onLinkEvent);
490
+ const cleanupKeyMissing = onBleKeyMissing(target, evaluate);
491
+ signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', onAbort, { once: true });
492
+ arm(resultTimeoutMs, () => finish('unresolved'));
493
+ evaluate();
494
+ });
495
+ };
496
+
497
+ const IOS_PERIPHERAL_DISCONNECTED$1 = 7;
498
+ const IOS_PEER_TERMINATION_LINK_WINDOW_MS = 10000;
499
+ const IOS_PEER_TERMINATION_REPEAT_WINDOW_MS = 60000;
500
+ const isIosPeerTerminationError = (error) => (error === null || error === void 0 ? void 0 : error.iosErrorCode) ===
501
+ IOS_PERIPHERAL_DISCONNECTED$1;
502
+ class IosPeerTerminationTracker {
503
+ constructor() {
504
+ this.linkStartedAt = new Map();
505
+ this.terminatedAt = new Map();
506
+ this.evidenceAt = new Map();
507
+ }
508
+ attemptStarted(deviceId) {
509
+ this.linkStartedAt.delete(deviceId);
510
+ this.terminatedAt.delete(deviceId);
511
+ }
512
+ linkStarted(deviceId) {
513
+ this.linkStartedAt.set(deviceId, Date.now());
514
+ this.terminatedAt.delete(deviceId);
515
+ }
516
+ note(deviceId, error) {
517
+ if (!isIosPeerTerminationError(error))
518
+ return;
519
+ if (!this.terminatedAt.has(deviceId))
520
+ this.terminatedAt.set(deviceId, Date.now());
521
+ }
522
+ linkProven(deviceId) {
523
+ this.forget(deviceId);
524
+ }
525
+ forget(deviceId) {
526
+ this.linkStartedAt.delete(deviceId);
527
+ this.terminatedAt.delete(deviceId);
528
+ this.evidenceAt.delete(deviceId);
529
+ }
530
+ attemptFailed(deviceId) {
531
+ const startedAt = this.linkStartedAt.get(deviceId);
532
+ const terminatedAt = this.terminatedAt.get(deviceId);
533
+ const previous = this.evidenceAt.get(deviceId);
534
+ this.forget(deviceId);
535
+ if (startedAt === undefined ||
536
+ terminatedAt === undefined ||
537
+ terminatedAt - startedAt > IOS_PEER_TERMINATION_LINK_WINDOW_MS) {
538
+ return false;
259
539
  }
260
- else {
261
- pendingError = undefined;
540
+ if (previous !== undefined &&
541
+ terminatedAt - previous <= IOS_PEER_TERMINATION_REPEAT_WINDOW_MS) {
542
+ return true;
262
543
  }
263
- }, true);
264
- removeSubscription = () => subscription.remove();
265
- if (done) {
266
- removeSubscription();
544
+ this.evidenceAt.set(deviceId, terminatedAt);
545
+ return false;
267
546
  }
268
- else {
269
- cancelTimeout = timer.timeout(() => {
270
- finish(pendingError !== null && pendingError !== void 0 ? pendingError : hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleScanError));
271
- }, ms);
547
+ reset() {
548
+ this.linkStartedAt.clear();
549
+ this.terminatedAt.clear();
550
+ this.evidenceAt.clear();
272
551
  }
273
- });
552
+ }
274
553
 
275
554
  const BLE_PLX_DEVICE_DISCONNECTED = 201;
276
555
  const IOS_PERIPHERAL_DISCONNECTED = 7;
277
- const nativeErrorText = (error) => [error.reason, error.message]
556
+ const nativeErrorText$1 = (error) => [error.reason, error.message]
278
557
  .filter((value) => typeof value === 'string')
279
558
  .join(' ');
280
559
  const isNativeBleDisconnectError = (error) => {
@@ -283,15 +562,48 @@ const isNativeBleDisconnectError = (error) => {
283
562
  }
284
563
  const nativeError = error;
285
564
  return (nativeError.errorCode === BLE_PLX_DEVICE_DISCONNECTED ||
286
- nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED ||
287
- nativeErrorText(nativeError).toLowerCase().includes('was disconnected'));
565
+ nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED);
288
566
  };
289
567
  const toBleDisconnectHardwareError = (error) => {
290
568
  if ((error === null || error === void 0 ? void 0 : error.errorCode) === hdShared.HardwareErrorCode.BleDeviceDisconnected) {
291
569
  return error;
292
570
  }
293
571
  const nativeError = (error !== null && error !== void 0 ? error : {});
294
- return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected, nativeErrorText(nativeError) || undefined);
572
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected, nativeErrorText$1(nativeError) || undefined);
573
+ };
574
+
575
+ const ATT_INSUFFICIENT_AUTHENTICATION = 5;
576
+ const ATT_INSUFFICIENT_ENCRYPTION = 15;
577
+ const IOS_PEER_REMOVED_PAIRING_INFORMATION = 14;
578
+ const nativeErrorText = (error) => [error.reason, error.message]
579
+ .filter((value) => typeof value === 'string')
580
+ .join(' ');
581
+ const isNativeBleStaleBondError = (error) => {
582
+ if (!error || typeof error !== 'object') {
583
+ return typeof error === 'string' ? hdShared.isBleStaleBondErrorText(error) : false;
584
+ }
585
+ const nativeError = error;
586
+ if (nativeError.attErrorCode === ATT_INSUFFICIENT_AUTHENTICATION ||
587
+ nativeError.attErrorCode === ATT_INSUFFICIENT_ENCRYPTION ||
588
+ nativeError.androidErrorCode === ATT_INSUFFICIENT_AUTHENTICATION ||
589
+ nativeError.androidErrorCode === ATT_INSUFFICIENT_ENCRYPTION ||
590
+ nativeError.iosErrorCode === IOS_PEER_REMOVED_PAIRING_INFORMATION) {
591
+ return true;
592
+ }
593
+ return hdShared.isBleStaleBondErrorText(nativeErrorText(nativeError));
594
+ };
595
+ const toBleStaleBondHardwareError = (error) => {
596
+ if (hdShared.isBleStaleBondHardwareError(error)) {
597
+ return error;
598
+ }
599
+ const nativeError = (error !== null && error !== void 0 ? error : {});
600
+ const text = nativeErrorText(nativeError);
601
+ const normalizedText = text.toLowerCase();
602
+ const peerRemoved = nativeError.iosErrorCode === IOS_PEER_REMOVED_PAIRING_INFORMATION ||
603
+ normalizedText.includes('peer removed pairing information');
604
+ return hdShared.ERRORS.TypedError(peerRemoved
605
+ ? hdShared.HardwareErrorCode.BlePeerRemovedPairingInformation
606
+ : hdShared.HardwareErrorCode.BleDeviceBondError, text || undefined);
295
607
  };
296
608
 
297
609
  const isHeaderChunk = (chunk) => {
@@ -385,7 +697,8 @@ const isWedgedWriteError = (error) => (error === null || error === void 0 ? void
385
697
  error.message.startsWith(WEDGED_WRITE_MESSAGE);
386
698
  const shouldRethrowProtocolProbeError = (error) => {
387
699
  const code = error === null || error === void 0 ? void 0 : error.errorCode;
388
- return (isNativeBleDisconnectError(error) ||
700
+ return (hdShared.isBleStaleBondHardwareError(error) ||
701
+ isNativeBleDisconnectError(error) ||
389
702
  code === hdShared.HardwareErrorCode.BleDeviceNotBonded ||
390
703
  code === hdShared.HardwareErrorCode.BleDeviceBondedCanceled ||
391
704
  code === hdShared.HardwareErrorCode.BleDeviceDisconnected ||
@@ -393,6 +706,18 @@ const shouldRethrowProtocolProbeError = (error) => {
393
706
  code === hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure ||
394
707
  code === hdShared.HardwareErrorCode.BleWriteCharacteristicError);
395
708
  };
709
+ const ANDROID_KEY_MISSING_LINK_WINDOW_MS = 10000;
710
+ const ANDROID_KEY_MISSING_GRACE_MS = 500;
711
+ const ANDROID_ENCRYPTION_RESULT_TIMEOUT_MS = 1500;
712
+ const ANDROID_SYSTEM_REPAIR_TIMEOUT_MS = 35000;
713
+ const isAndroidLinkLossError = (error) => {
714
+ const code = error === null || error === void 0 ? void 0 : error.errorCode;
715
+ return (isNativeBleDisconnectError(error) ||
716
+ code === hdShared.HardwareErrorCode.BleDeviceNotBonded ||
717
+ code === hdShared.HardwareErrorCode.BleDeviceDisconnected ||
718
+ code === hdShared.HardwareErrorCode.BleCharacteristicNotifyError ||
719
+ code === hdShared.HardwareErrorCode.BleConnectedError);
720
+ };
396
721
  const BLE_WRITE_TIMEOUT_MANAGER_RESET_THRESHOLD = 2;
397
722
  const DEVICE_SCAN_TIMEOUT_MS = 3000;
398
723
  const IOS_NOTIFY_READY_DELAY_MS = 150;
@@ -552,14 +877,21 @@ const resolveNegotiatedMtu = (device, cancelTransaction) => shouldRefreshNegotia
552
877
  ? requestNegotiatedMtu(device, 'connected', 0, cancelTransaction)
553
878
  : Promise.resolve({ device, timedOut: false });
554
879
  function remapError(error) {
555
- var _a, _b;
556
- if (isNativeBleDisconnectError(error)) {
557
- throw toBleDisconnectHardwareError(error);
880
+ var _a;
881
+ if (error instanceof reactNativeBlePlx.BleError) {
882
+ if (isNativeBleStaleBondError(error)) {
883
+ throw toBleStaleBondHardwareError(error);
884
+ }
885
+ if ((error === null || error === void 0 ? void 0 : error.attErrorCode) === 22) {
886
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceBondError);
887
+ }
558
888
  }
559
- if (error instanceof Error && ((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('not found'))) {
889
+ if (error instanceof Error &&
890
+ error.message &&
891
+ (error.message.includes('was disconnected') || error.message.includes('not found'))) {
560
892
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
561
893
  }
562
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_b = error.reason) !== null && _b !== void 0 ? _b : error);
894
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_a = error.reason) !== null && _a !== void 0 ? _a : error);
563
895
  }
564
896
  class ReactNativeBleTransport {
565
897
  constructor(options) {
@@ -582,6 +914,10 @@ class ReactNativeBleTransport {
582
914
  this.protocolReprobeFailures = new Map();
583
915
  this.silentDetections = new Map();
584
916
  this.androidGattCacheRefreshes = new Set();
917
+ this.staleBondErrors = new Map();
918
+ this.androidLinkStartedAt = new Map();
919
+ this.iosPeerTermination = new IosPeerTerminationTracker();
920
+ this.acquiringProtocolV2 = new Set();
585
921
  this.protocolV2Assemblers = new Map();
586
922
  this.protocolV2FrameQueues = new Map();
587
923
  this.protocolV2FramePromises = new Map();
@@ -628,6 +964,10 @@ class ReactNativeBleTransport {
628
964
  init(logger, emitter) {
629
965
  setBleLogger(logger);
630
966
  this.emitter = emitter;
967
+ if (reactNative.Platform.OS === 'android') {
968
+ startBleKeyMissingTracking();
969
+ startBleEncryptionTracking();
970
+ }
631
971
  }
632
972
  configure(signedData) {
633
973
  const messages = parseConfigure(signedData);
@@ -850,6 +1190,7 @@ class ReactNativeBleTransport {
850
1190
  const scanStartedAt = Date.now();
851
1191
  let firstDeviceMs;
852
1192
  const blePlxManager = yield this.getPlxManager();
1193
+ yield subscribeBleOn(blePlxManager);
853
1194
  if (reactNative.Platform.OS === 'android' && reactNative.Platform.Version >= 31) {
854
1195
  Log === null || Log === void 0 ? void 0 : Log.debug('requesting permissions, please wait...');
855
1196
  const resultConnect = yield reactNative.PermissionsAndroid.requestMultiple([
@@ -859,10 +1200,9 @@ class ReactNativeBleTransport {
859
1200
  Log === null || Log === void 0 ? void 0 : Log.debug('requesting permissions, result: ', resultConnect);
860
1201
  if (resultConnect[reactNative.PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT] !== 'granted' ||
861
1202
  resultConnect[reactNative.PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN] !== 'granted') {
862
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationError);
1203
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
863
1204
  }
864
1205
  }
865
- yield subscribeBleOn(blePlxManager);
866
1206
  if (this.stopped)
867
1207
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
868
1208
  return new Promise((resolve, reject) => {
@@ -903,11 +1243,8 @@ class ReactNativeBleTransport {
903
1243
  var _a, _b;
904
1244
  if (error) {
905
1245
  Log === null || Log === void 0 ? void 0 : Log.debug('ble scan error: ', error);
906
- if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff) {
907
- finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff));
908
- }
909
- else if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothUnsupported) {
910
- finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported));
1246
+ if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
1247
+ finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError));
911
1248
  }
912
1249
  else if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothUnauthorized) {
913
1250
  finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationError));
@@ -1004,7 +1341,6 @@ class ReactNativeBleTransport {
1004
1341
  if (this.stopped)
1005
1342
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
1006
1343
  Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] BLE MTU ready', {
1007
- connectionId: monitorToken,
1008
1344
  platform: reactNative.Platform.OS,
1009
1345
  requested: getRequestedBleMtu(),
1010
1346
  actual: transport$1.mtuSize,
@@ -1018,7 +1354,14 @@ class ReactNativeBleTransport {
1018
1354
  if (!uuid) {
1019
1355
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleRequiredUUID);
1020
1356
  }
1021
- return this.runLifecycleOperation(uuid, () => this.acquireUnlocked(input));
1357
+ return this.runLifecycleOperation(uuid, () => __awaiter(this, void 0, void 0, function* () {
1358
+ try {
1359
+ return yield this.acquireUnlocked(input);
1360
+ }
1361
+ catch (error) {
1362
+ throw this.resolveIosBondInvalid(input, yield this.resolveAndroidBondInvalid(uuid, error));
1363
+ }
1364
+ }));
1022
1365
  });
1023
1366
  }
1024
1367
  acquireUnlocked(input) {
@@ -1027,6 +1370,10 @@ class ReactNativeBleTransport {
1027
1370
  if (this.stopped)
1028
1371
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
1029
1372
  const { uuid, forceCleanRunPromise, expectedProtocol, skipProtocolProbe } = input;
1373
+ this.iosPeerTermination.attemptStarted(uuid);
1374
+ const shouldMapProtocolV2StaleBond = expectedProtocol
1375
+ ? expectedProtocol === 'V2'
1376
+ : this.confirmedProtocolV2.has(uuid);
1030
1377
  const cachedTransport = transportCache[uuid];
1031
1378
  if (skipProtocolProbe && !cachedTransport && this.blePlxManager) {
1032
1379
  Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] refresh uncached BLE connection for firmware install:', uuid);
@@ -1070,6 +1417,7 @@ class ReactNativeBleTransport {
1070
1417
  }
1071
1418
  let device = null;
1072
1419
  const isAndroid = reactNative.Platform.OS === 'android';
1420
+ let androidBondedBeforeConnect = false;
1073
1421
  const refreshAndroidGattCache = isAndroid && (!!skipProtocolProbe || this.androidGattCacheRefreshes.has(uuid));
1074
1422
  let nativeConnectOptions = connectOptions;
1075
1423
  if (isAndroid) {
@@ -1097,8 +1445,12 @@ class ReactNativeBleTransport {
1097
1445
  if (this.stopped)
1098
1446
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
1099
1447
  if (reactNative.Platform.OS === 'android') {
1448
+ startBleKeyMissingTracking();
1449
+ startBleEncryptionTracking();
1450
+ this.androidLinkStartedAt.delete(uuid);
1100
1451
  try {
1101
1452
  const bondState = yield pairDevice(uuid);
1453
+ androidBondedBeforeConnect = !bondState.bonding && bondState.bonded;
1102
1454
  if (bondState.bonding) {
1103
1455
  yield onDeviceBondState(uuid, this.bondAbortController.signal, {
1104
1456
  systemInitiated: bondState.initiated === false,
@@ -1114,6 +1466,7 @@ class ReactNativeBleTransport {
1114
1466
  }));
1115
1467
  throw error;
1116
1468
  }
1469
+ this.androidLinkStartedAt.set(uuid, Date.now());
1117
1470
  }
1118
1471
  if (this.stopped)
1119
1472
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
@@ -1268,7 +1621,16 @@ class ReactNativeBleTransport {
1268
1621
  if (protocolHint) {
1269
1622
  this.deviceProtocolHints.set(uuid, protocolHint);
1270
1623
  }
1624
+ if (shouldMapProtocolV2StaleBond) {
1625
+ this.acquiringProtocolV2.add(uuid);
1626
+ }
1627
+ let linkEncryption;
1271
1628
  try {
1629
+ if (androidBondedBeforeConnect && !skipProtocolProbe) {
1630
+ linkEncryption = yield this.waitForAndroidLinkSecurity(uuid);
1631
+ if (this.stopped)
1632
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
1633
+ }
1272
1634
  yield this.installTransportForAcquire(uuid, acquiredDevice, {
1273
1635
  writeCharacteristic,
1274
1636
  notifyCharacteristic,
@@ -1302,17 +1664,24 @@ class ReactNativeBleTransport {
1302
1664
  }));
1303
1665
  if (this.stopped)
1304
1666
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
1667
+ this.iosPeerTermination.linkProven(uuid);
1305
1668
  const currentTransport = transportCache[uuid];
1306
1669
  if (!currentTransport) {
1307
1670
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotFound);
1308
1671
  }
1309
1672
  this.attachDisconnectSubscription(currentTransport, currentTransport.device, uuid);
1673
+ if (linkEncryption === 'unresolved') {
1674
+ markBleLinkEncrypted(uuid);
1675
+ }
1310
1676
  return { uuid, protocolType };
1311
1677
  }
1312
1678
  catch (error) {
1313
1679
  yield this.disconnectUnlocked(uuid);
1314
1680
  throw error;
1315
1681
  }
1682
+ finally {
1683
+ this.acquiringProtocolV2.delete(uuid);
1684
+ }
1316
1685
  });
1317
1686
  }
1318
1687
  _monitorCharacteristic(characteristic, uuid, monitorToken, notifyTransactionId) {
@@ -1322,6 +1691,8 @@ class ReactNativeBleTransport {
1322
1691
  var _a, _b, _c, _d, _e, _f;
1323
1692
  const isCurrentMonitor = this.monitorTokens.get(uuid) === monitorToken;
1324
1693
  if (error) {
1694
+ if (isCurrentMonitor)
1695
+ this.noteIosLinkError(uuid, 'notify', error);
1325
1696
  Log === null || Log === void 0 ? void 0 : Log.debug(`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${error}`);
1326
1697
  if (this.firmwareUploadWriteRecoveryIds.has(uuid)) {
1327
1698
  Log === null || Log === void 0 ? void 0 : Log.debug('notify error ignored during FirmwareUpload write recovery: ', uuid);
@@ -1331,6 +1702,11 @@ class ReactNativeBleTransport {
1331
1702
  Log === null || Log === void 0 ? void 0 : Log.debug('monitor error ignored for stale transport: ', uuid, notifyTransactionId);
1332
1703
  return;
1333
1704
  }
1705
+ if ((this.getActiveProtocol(uuid) === 'V2' || this.acquiringProtocolV2.has(uuid)) &&
1706
+ isNativeBleStaleBondError(error)) {
1707
+ this.rememberStaleBondError(uuid, toBleStaleBondHardwareError(error));
1708
+ return;
1709
+ }
1334
1710
  if (reactNative.Platform.OS === 'android' && isStaleGattTableNotifyReason(error.reason)) {
1335
1711
  this.androidGattCacheRefreshes.add(uuid);
1336
1712
  }
@@ -1459,6 +1835,8 @@ class ReactNativeBleTransport {
1459
1835
  this.protocolV2HighVolumeLogSignatures.delete(uuid);
1460
1836
  this.deviceProtocol.delete(uuid);
1461
1837
  this.probingProtocols.delete(uuid);
1838
+ this.staleBondErrors.delete(uuid);
1839
+ this.acquiringProtocolV2.delete(uuid);
1462
1840
  (_e = this.protocolV2Assemblers.get(uuid)) === null || _e === void 0 ? void 0 : _e.reset();
1463
1841
  this.protocolV2Assemblers.delete(uuid);
1464
1842
  this.resetProtocolV2Frames(uuid);
@@ -1502,9 +1880,94 @@ class ReactNativeBleTransport {
1502
1880
  if (protocol !== 'V2' && this.runPromise && !forceRun) {
1503
1881
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportCallInProgress);
1504
1882
  }
1505
- return protocol === 'V2'
1506
- ? this.callProtocolV2(uuid, name, data, options)
1507
- : this.callProtocolV1(uuid, name, data, options);
1883
+ try {
1884
+ return protocol === 'V2'
1885
+ ? yield this.callProtocolV2(uuid, name, data, options)
1886
+ : yield this.callProtocolV1(uuid, name, data, options);
1887
+ }
1888
+ catch (error) {
1889
+ throw yield this.resolveAndroidBondInvalid(uuid, error);
1890
+ }
1891
+ });
1892
+ }
1893
+ resolveAndroidBondInvalid(uuid, error) {
1894
+ return __awaiter(this, void 0, void 0, function* () {
1895
+ if (reactNative.Platform.OS !== 'android' || !isBleKeyMissingSupported())
1896
+ return error;
1897
+ if (hdShared.isBleStaleBondHardwareError(error) || !isAndroidLinkLossError(error))
1898
+ return error;
1899
+ const linkStartedAt = this.androidLinkStartedAt.get(uuid);
1900
+ if (linkStartedAt === undefined ||
1901
+ Date.now() - linkStartedAt > ANDROID_KEY_MISSING_LINK_WINDOW_MS) {
1902
+ return error;
1903
+ }
1904
+ if (!(yield waitForBleKeyMissing(uuid, linkStartedAt, ANDROID_KEY_MISSING_GRACE_MS))) {
1905
+ return error;
1906
+ }
1907
+ Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Android key missing, bond is invalid:', uuid);
1908
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleBondInvalid, undefined, {
1909
+ phase: 'connect',
1910
+ reason: 'key_missing',
1911
+ });
1912
+ });
1913
+ }
1914
+ noteIosLinkError(uuid, stage, error) {
1915
+ if (reactNative.Platform.OS !== 'ios')
1916
+ return;
1917
+ this.iosPeerTermination.note(uuid, error);
1918
+ if (stage !== 'gatt-setup' && !isNativeBleDisconnectError(error))
1919
+ return;
1920
+ const native = (error !== null && error !== void 0 ? error : {});
1921
+ const code = (value) => typeof value === 'number' || typeof value === 'string' ? value : undefined;
1922
+ Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] iOS operation failed', {
1923
+ connectIdSuffix: uuid.slice(-8),
1924
+ stage,
1925
+ errorCode: code(native.errorCode),
1926
+ iosErrorCode: code(native.iosErrorCode),
1927
+ attErrorCode: code(native.attErrorCode),
1928
+ });
1929
+ }
1930
+ resolveIosBondInvalid(input, error) {
1931
+ if (reactNative.Platform.OS !== 'ios')
1932
+ return error;
1933
+ const { uuid, skipProtocolProbe } = input;
1934
+ if (skipProtocolProbe || hdShared.isBleStaleBondHardwareError(error)) {
1935
+ this.iosPeerTermination.forget(uuid);
1936
+ return error;
1937
+ }
1938
+ if (!this.iosPeerTermination.attemptFailed(uuid))
1939
+ return error;
1940
+ Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] iOS peer ended two new links in a row, bond is invalid:', {
1941
+ connectIdSuffix: uuid.slice(-8),
1942
+ });
1943
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleBondInvalid, undefined, {
1944
+ phase: 'connect',
1945
+ reason: 'peer_disconnected',
1946
+ });
1947
+ }
1948
+ waitForAndroidLinkSecurity(uuid) {
1949
+ return __awaiter(this, void 0, void 0, function* () {
1950
+ const linkStartedAt = this.androidLinkStartedAt.get(uuid);
1951
+ if (linkStartedAt === undefined)
1952
+ return 'unresolved';
1953
+ const waitStartedAt = Date.now();
1954
+ const result = yield waitForAndroidLinkEncryption({
1955
+ deviceId: uuid,
1956
+ linkStartedAt,
1957
+ resultTimeoutMs: ANDROID_ENCRYPTION_RESULT_TIMEOUT_MS,
1958
+ repairTimeoutMs: ANDROID_SYSTEM_REPAIR_TIMEOUT_MS,
1959
+ keyMissingGraceMs: ANDROID_KEY_MISSING_GRACE_MS,
1960
+ signal: this.bondAbortController.signal,
1961
+ onRepairStarted: () => {
1962
+ Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Android bond lost, waiting for system re-pairing:', uuid);
1963
+ },
1964
+ });
1965
+ Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Android link encryption', {
1966
+ connectIdSuffix: uuid.slice(-8),
1967
+ result,
1968
+ waitedMs: Date.now() - waitStartedAt,
1969
+ });
1970
+ return result;
1508
1971
  });
1509
1972
  }
1510
1973
  callProtocolV1(uuid, name, data, options) {
@@ -1720,6 +2183,10 @@ class ReactNativeBleTransport {
1720
2183
  this.androidPriorityResetTimers.forEach(timeout => clearTimeout(timeout));
1721
2184
  this.androidPriorityResetTimers.clear();
1722
2185
  this.androidHighPriorityDevices.clear();
2186
+ this.androidLinkStartedAt.clear();
2187
+ this.iosPeerTermination.reset();
2188
+ stopBleKeyMissingTracking();
2189
+ stopBleEncryptionTracking();
1723
2190
  const error = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
1724
2191
  (_a = this.runPromise) === null || _a === void 0 ? void 0 : _a.reject(error);
1725
2192
  this.runPromise = null;
@@ -1780,6 +2247,7 @@ class ReactNativeBleTransport {
1780
2247
  }
1781
2248
  this.deviceProtocol.delete(session);
1782
2249
  this.probingProtocols.delete(session);
2250
+ this.staleBondErrors.delete(session);
1783
2251
  this.deviceProtocolHints.delete(session);
1784
2252
  this.sessionProtocols.delete(session);
1785
2253
  this.protocolReprobeFailures.delete(session);
@@ -1972,6 +2440,7 @@ class ReactNativeBleTransport {
1972
2440
  }),
1973
2441
  ]);
1974
2442
  succeeded = true;
2443
+ this.iosPeerTermination.linkStarted(uuid);
1975
2444
  return result;
1976
2445
  }
1977
2446
  catch (error) {
@@ -2018,15 +2487,16 @@ class ReactNativeBleTransport {
2018
2487
  return result;
2019
2488
  }
2020
2489
  catch (error) {
2490
+ this.noteIosLinkError(uuid, 'gatt-setup', error);
2491
+ if (reactNative.Platform.OS === 'ios' && isNativeBleStaleBondError(error)) {
2492
+ throw toBleStaleBondHardwareError(error);
2493
+ }
2021
2494
  if (timedOut || isNativeOperationTimeoutError(error)) {
2022
2495
  const resetManager = this.abandonStalledConnection(uuid, timedOut ? 'gatt-backstop' : 'gatt-native');
2023
2496
  if (resetManager) {
2024
2497
  throw this.createWedgedBleSetupError();
2025
2498
  }
2026
2499
  }
2027
- if (isNativeBleDisconnectError(error)) {
2028
- throw toBleDisconnectHardwareError(error);
2029
- }
2030
2500
  if (reactNative.Platform.OS === 'android' && isMissingGattShapeError(error)) {
2031
2501
  this.androidGattCacheRefreshes.add(uuid);
2032
2502
  }
@@ -2060,6 +2530,8 @@ class ReactNativeBleTransport {
2060
2530
  }
2061
2531
  this.deviceProtocol.delete(uuid);
2062
2532
  this.probingProtocols.delete(uuid);
2533
+ this.staleBondErrors.delete(uuid);
2534
+ this.acquiringProtocolV2.delete(uuid);
2063
2535
  this.protocolV2Assemblers.delete(uuid);
2064
2536
  this.resetProtocolV2Frames(uuid);
2065
2537
  if (timeouts >= BLE_CONNECT_TIMEOUT_MANAGER_RESET_THRESHOLD) {
@@ -2097,6 +2569,7 @@ class ReactNativeBleTransport {
2097
2569
  this.writeTimeoutCounts.delete(uuid);
2098
2570
  }
2099
2571
  catch (error) {
2572
+ this.noteIosLinkError(uuid, 'write', error);
2100
2573
  if (timedOut) {
2101
2574
  if (isCurrentOwner && !isCurrentOwner()) {
2102
2575
  Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] stale BLE write timed out, link kept:', uuid);
@@ -2129,6 +2602,8 @@ class ReactNativeBleTransport {
2129
2602
  }
2130
2603
  this.deviceProtocol.delete(uuid);
2131
2604
  this.probingProtocols.delete(uuid);
2605
+ this.staleBondErrors.delete(uuid);
2606
+ this.acquiringProtocolV2.delete(uuid);
2132
2607
  this.protocolV2Assemblers.delete(uuid);
2133
2608
  this.resetProtocolV2Frames(uuid);
2134
2609
  if (timeouts >= BLE_WRITE_TIMEOUT_MANAGER_RESET_THRESHOLD) {
@@ -2173,6 +2648,8 @@ class ReactNativeBleTransport {
2173
2648
  });
2174
2649
  this.deviceProtocol.clear();
2175
2650
  this.probingProtocols.clear();
2651
+ this.staleBondErrors.clear();
2652
+ this.acquiringProtocolV2.clear();
2176
2653
  this.sessionProtocols.clear();
2177
2654
  this.protocolReprobeFailures.clear();
2178
2655
  this.silentDetections.clear();
@@ -2225,6 +2702,9 @@ class ReactNativeBleTransport {
2225
2702
  });
2226
2703
  return 'V1';
2227
2704
  }
2705
+ if (expectedProtocol === 'V2' || this.acquiringProtocolV2.has(uuid)) {
2706
+ this.throwIfStaleBondError(uuid);
2707
+ }
2228
2708
  if (expectedProtocol === 'V2') {
2229
2709
  if (yield this.probeProtocolV2(uuid)) {
2230
2710
  this.deviceProtocol.set(uuid, 'V2');
@@ -2383,6 +2863,7 @@ class ReactNativeBleTransport {
2383
2863
  }
2384
2864
  this.probingProtocols.set(uuid, 'V2');
2385
2865
  (_a = this.protocolV2Assemblers.get(uuid)) === null || _a === void 0 ? void 0 : _a.reset();
2866
+ this.throwIfStaleBondError(uuid);
2386
2867
  const detected = yield transport.probeProtocolV2({
2387
2868
  call: (name, data, options) => this.callProtocolV2(uuid, name, data, options),
2388
2869
  timeoutMs: PROTOCOL_V2_PROBE_TIMEOUT_MS,
@@ -2454,6 +2935,19 @@ class ReactNativeBleTransport {
2454
2935
  framePromise.reject(error);
2455
2936
  }
2456
2937
  }
2938
+ rememberStaleBondError(uuid, error) {
2939
+ this.staleBondErrors.set(uuid, error);
2940
+ this.rejectProtocolV2Frames(uuid, error);
2941
+ if (this.runPromise && this.runPromiseDeviceId === uuid) {
2942
+ this.runPromise.reject(error);
2943
+ }
2944
+ }
2945
+ throwIfStaleBondError(uuid) {
2946
+ const error = this.staleBondErrors.get(uuid);
2947
+ if (error) {
2948
+ throw error;
2949
+ }
2950
+ }
2457
2951
  readProtocolV2Frame(uuid) {
2458
2952
  return __awaiter(this, void 0, void 0, function* () {
2459
2953
  const queuedFrame = this.getProtocolV2FrameQueue(uuid).shift();
@@ -2502,6 +2996,11 @@ class ReactNativeBleTransport {
2502
2996
  return;
2503
2997
  }
2504
2998
  catch (error) {
2999
+ if (isNativeBleStaleBondError(error) || hdShared.isBleStaleBondHardwareError(error)) {
3000
+ const bondError = toBleStaleBondHardwareError(error);
3001
+ this.rememberStaleBondError(uuid, bondError);
3002
+ throw bondError;
3003
+ }
2505
3004
  if (isNativeBleDisconnectError(error)) {
2506
3005
  throw toBleDisconnectHardwareError(error);
2507
3006
  }
@@ -2727,8 +3226,12 @@ class ReactNativeBleTransport {
2727
3226
  }
2728
3227
  }
2729
3228
 
3229
+ exports.ANDROID_ENCRYPTION_RESULT_TIMEOUT_MS = ANDROID_ENCRYPTION_RESULT_TIMEOUT_MS;
3230
+ exports.ANDROID_KEY_MISSING_GRACE_MS = ANDROID_KEY_MISSING_GRACE_MS;
3231
+ exports.ANDROID_KEY_MISSING_LINK_WINDOW_MS = ANDROID_KEY_MISSING_LINK_WINDOW_MS;
2730
3232
  exports.ANDROID_LINK_DROP_QUIET_MS = ANDROID_LINK_DROP_QUIET_MS;
2731
3233
  exports.ANDROID_MTU_EXCHANGE_TIMEOUT_MS = ANDROID_MTU_EXCHANGE_TIMEOUT_MS;
3234
+ exports.ANDROID_SYSTEM_REPAIR_TIMEOUT_MS = ANDROID_SYSTEM_REPAIR_TIMEOUT_MS;
2732
3235
  exports.BLE_CONNECT_TIMEOUT_MANAGER_RESET_THRESHOLD = BLE_CONNECT_TIMEOUT_MANAGER_RESET_THRESHOLD;
2733
3236
  exports.BLE_CONNECT_TIMEOUT_MS = BLE_CONNECT_TIMEOUT_MS;
2734
3237
  exports.BLE_GATT_SETUP_TIMEOUT_MS = BLE_GATT_SETUP_TIMEOUT_MS;