@onekeyfe/hd-transport-react-native 1.2.0-alpha.33 → 1.2.0-alpha.35

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
@@ -95,7 +95,6 @@ const IOS_PACKET_LENGTH = 128;
95
95
  const ANDROID_PACKET_LENGTH = 192;
96
96
  const ANDROID_DEFAULT_MTU = 23;
97
97
  const ClassicServiceUUID = '00000001-0000-1000-8000-00805f9b34fb';
98
- const Pro2ServiceUUID = 'fffd';
99
98
  const OneKeyServices = {
100
99
  classic: {
101
100
  [ClassicServiceUUID]: {
@@ -103,11 +102,6 @@ const OneKeyServices = {
103
102
  writeUuid: '00000002-0000-1000-8000-00805f9b34fb',
104
103
  notifyUuid: '00000003-0000-1000-8000-00805f9b34fb',
105
104
  },
106
- [Pro2ServiceUUID]: {
107
- serviceUuid: Pro2ServiceUUID,
108
- writeUuid: '00000002-0000-1000-8000-00805f9b34fb',
109
- notifyUuid: '00000003-0000-1000-8000-00805f9b34fb',
110
- },
111
105
  },
112
106
  };
113
107
  const bluetoothServices = [];
@@ -124,22 +118,17 @@ const getInfosForServiceUuid = (serviceUuid, deviceType) => {
124
118
  }
125
119
  const normalizedServiceUuid = normalizeBleUuid(serviceUuid);
126
120
  const service = (_a = services[serviceUuid]) !== null && _a !== void 0 ? _a : Object.values(services).find(item => normalizeBleUuid(item.serviceUuid) === normalizedServiceUuid ||
127
- getBleUuidKey(item.serviceUuid) === getBleUuidKey(serviceUuid));
121
+ hdShared.matchesKnownBleUuid(serviceUuid, hdShared.createKnownBleUuidAliases(item.serviceUuid)));
128
122
  if (!service) {
129
123
  return null;
130
124
  }
131
125
  return service;
132
126
  };
133
127
  const normalizeBleUuid = (uuid) => (uuid !== null && uuid !== void 0 ? uuid : '').replace(/-/g, '').toLowerCase();
134
- const getBleUuidKey = (uuid) => {
135
- const normalized = normalizeBleUuid(uuid);
136
- return normalized.length >= 8 ? normalized.substring(4, 8) : normalized;
137
- };
138
128
  const isSameBleUuid = (left, right) => {
139
- const normalizedLeft = normalizeBleUuid(left);
140
- const normalizedRight = normalizeBleUuid(right);
141
- return (normalizedLeft === normalizedRight ||
142
- (getBleUuidKey(left) !== '' && getBleUuidKey(left) === getBleUuidKey(right)));
129
+ if (!left || !right)
130
+ return false;
131
+ return hdShared.matchesKnownBleUuid(left, hdShared.createKnownBleUuidAliases(right));
143
132
  };
144
133
 
145
134
  function hasWritableCapability(characteristic) {
@@ -210,7 +199,6 @@ const isHeaderChunk = (chunk) => {
210
199
  return false;
211
200
  };
212
201
 
213
- const Log$1 = bleLogger;
214
202
  class BleTransport {
215
203
  constructor(device, writeCharacteristic, notifyCharacteristic) {
216
204
  this.name = 'ReactNativeBleTransport';
@@ -220,49 +208,12 @@ class BleTransport {
220
208
  this.writeCharacteristic = writeCharacteristic;
221
209
  this.notifyCharacteristic = notifyCharacteristic;
222
210
  }
223
- writeWithRetry(data, retryCount = BleTransport.MAX_RETRIES) {
211
+ writeWithRetry(data) {
224
212
  return __awaiter(this, void 0, void 0, function* () {
225
- try {
226
- yield this.writeCharacteristic.writeWithoutResponse(data);
227
- }
228
- catch (error) {
229
- Log$1 === null || Log$1 === void 0 ? void 0 : Log$1.debug(`Write retry attempt ${BleTransport.MAX_RETRIES - retryCount + 1}, error: ${error}`);
230
- if (retryCount > 0) {
231
- yield hdShared.wait(BleTransport.RETRY_DELAY);
232
- if (error.errorCode === reactNativeBlePlx.BleErrorCode.DeviceDisconnected ||
233
- error.errorCode === reactNativeBlePlx.BleErrorCode.CharacteristicNotFound) {
234
- try {
235
- yield this.device.connect();
236
- yield this.device.discoverAllServicesAndCharacteristics();
237
- }
238
- catch (e) {
239
- Log$1 === null || Log$1 === void 0 ? void 0 : Log$1.debug(`Connect or discoverAllServicesAndCharacteristics error: ${e}`);
240
- }
241
- }
242
- else {
243
- Log$1 === null || Log$1 === void 0 ? void 0 : Log$1.debug(`writeCharacteristic error: ${error}`);
244
- }
245
- return this.writeWithRetry(data, retryCount - 1);
246
- }
247
- throw error;
248
- }
213
+ yield this.writeCharacteristic.writeWithoutResponse(data);
249
214
  });
250
215
  }
251
216
  }
252
- BleTransport.MAX_RETRIES = 5;
253
- BleTransport.RETRY_DELAY = 2000;
254
-
255
- function createTransportCallLog(name, protocol, data) {
256
- if (name === 'ResourceUpdate' || name === 'ResourceAck') {
257
- return {
258
- name,
259
- protocol,
260
- file_name: data.file_name,
261
- hash: data.hash,
262
- };
263
- }
264
- return { name, protocol };
265
- }
266
217
 
267
218
  const { check, ProtocolV1, parseConfigure } = transport__default["default"];
268
219
  const Log = bleLogger;
@@ -271,7 +222,6 @@ const FIRMWARE_UPLOAD_WRITE_BURST_SIZE = reactNative.Platform.OS === 'ios' ? 4 :
271
222
  const FIRMWARE_UPLOAD_WRITE_PAUSE_MS = reactNative.Platform.OS === 'ios' ? 8 : 10;
272
223
  const FIRMWARE_UPLOAD_WRITE_FLUSH_DELAY_MS = reactNative.Platform.OS === 'ios' ? 24 : 30;
273
224
  const FIRMWARE_UPLOAD_WRITE_MAX_RETRIES = 8;
274
- const FIRMWARE_UPLOAD_RECONNECT_RETRY_DELAY_MS = 2000;
275
225
  const ANDROID_FIRMWARE_UPLOAD_PACKET_LENGTH = 192;
276
226
  const FIRMWARE_UPLOAD_WRITE_PACKET_CAPACITY = reactNative.Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_FIRMWARE_UPLOAD_PACKET_LENGTH;
277
227
  const ANDROID_GATT_CONGESTED_STATUS = 143;
@@ -282,10 +232,6 @@ const getFirmwareUploadWriteRetryType = (error) => {
282
232
  if (!error || typeof error !== 'object')
283
233
  return null;
284
234
  const bleWriteError = error;
285
- if (bleWriteError.errorCode === reactNativeBlePlx.BleErrorCode.DeviceDisconnected ||
286
- bleWriteError.errorCode === reactNativeBlePlx.BleErrorCode.CharacteristicNotFound) {
287
- return 'reconnectable';
288
- }
289
235
  if (bleWriteError.androidErrorCode === ANDROID_GATT_CONGESTED_STATUS ||
290
236
  bleWriteError.status === ANDROID_GATT_CONGESTED_STATUS) {
291
237
  return 'congested';
@@ -332,13 +278,6 @@ function inferProtocolHintFromDeviceName(name) {
332
278
  function getDeviceDisplayName(device) {
333
279
  return (device === null || device === void 0 ? void 0 : device.name) || (device === null || device === void 0 ? void 0 : device.localName) || null;
334
280
  }
335
- function isGenericBleService(uuid) {
336
- return ['1800', '1801', '180a'].includes(getBleUuidKey(uuid));
337
- }
338
- function hasKnownOneKeyService(device) {
339
- var _a;
340
- return ((_a = device === null || device === void 0 ? void 0 : device.serviceUUIDs) !== null && _a !== void 0 ? _a : []).some(serviceUuid => getInfosForServiceUuid(serviceUuid, 'classic'));
341
- }
342
281
  const ANDROID_REQUEST_MTU = 256;
343
282
  const connectOptions = {
344
283
  requestMTU: ANDROID_REQUEST_MTU,
@@ -399,6 +338,7 @@ class ReactNativeBleTransport {
399
338
  this.stopped = false;
400
339
  this.scanTimeout = DEVICE_SCAN_TIMEOUT_MS;
401
340
  this.runPromise = null;
341
+ this.runPromiseDeviceId = null;
402
342
  this.firmwareUploadWriteRecoveryIds = new Set();
403
343
  this.deviceProtocol = new Map();
404
344
  this.deviceProtocolHints = new Map();
@@ -463,7 +403,6 @@ class ReactNativeBleTransport {
463
403
  return Promise.resolve(this.blePlxManager);
464
404
  }
465
405
  resolveCharacteristics(device) {
466
- var _a, _b, _c, _d;
467
406
  return __awaiter(this, void 0, void 0, function* () {
468
407
  yield device.discoverAllServicesAndCharacteristics();
469
408
  let infos = tryToGetConfiguration(device);
@@ -480,19 +419,11 @@ class ReactNativeBleTransport {
480
419
  }
481
420
  }
482
421
  }
483
- let fallbackServiceUuid;
484
422
  if (!infos) {
485
423
  const services = yield device.services();
486
424
  Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Known OneKey service UUID not found, discovered services:', services === null || services === void 0 ? void 0 : services.map(service => service.uuid));
487
- const knownService = services.find(service => getInfosForServiceUuid(service.uuid, 'classic'));
488
- const fallbackService = (_a = knownService !== null && knownService !== void 0 ? knownService : services.find(service => !isGenericBleService(service.uuid))) !== null && _a !== void 0 ? _a : services[0];
489
- if (fallbackService) {
490
- fallbackServiceUuid = fallbackService.uuid;
491
- characteristics = yield device.characteristicsForService(fallbackService.uuid);
492
- Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Using fallback BLE service:', fallbackService.uuid);
493
- }
494
425
  }
495
- if (!infos && !fallbackServiceUuid) {
426
+ if (!infos) {
496
427
  try {
497
428
  Log === null || Log === void 0 ? void 0 : Log.debug('cancel connection when service not found');
498
429
  yield device.cancelConnection();
@@ -502,9 +433,7 @@ class ReactNativeBleTransport {
502
433
  }
503
434
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleServiceNotFound);
504
435
  }
505
- const serviceUuid = (_b = infos === null || infos === void 0 ? void 0 : infos.serviceUuid) !== null && _b !== void 0 ? _b : fallbackServiceUuid;
506
- const writeUuid = (_c = infos === null || infos === void 0 ? void 0 : infos.writeUuid) !== null && _c !== void 0 ? _c : '00000002-0000-1000-8000-00805f9b34fb';
507
- const notifyUuid = (_d = infos === null || infos === void 0 ? void 0 : infos.notifyUuid) !== null && _d !== void 0 ? _d : '00000003-0000-1000-8000-00805f9b34fb';
436
+ const { serviceUuid, writeUuid, notifyUuid } = infos;
508
437
  if (!serviceUuid) {
509
438
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleServiceNotFound);
510
439
  }
@@ -562,10 +491,9 @@ class ReactNativeBleTransport {
562
491
  try {
563
492
  Log === null || Log === void 0 ? void 0 : Log.debug('device disconnect: ', device === null || device === void 0 ? void 0 : device.id);
564
493
  this.emitDeviceDisconnect(uuid, device === null || device === void 0 ? void 0 : device.name, monitorToken);
565
- if (this.runPromise) {
494
+ if (this.runPromise && this.runPromiseDeviceId === uuid) {
566
495
  const error = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError);
567
496
  this.runPromise.reject(error);
568
- this.rejectAllProtocolV2Frames(error);
569
497
  }
570
498
  }
571
499
  catch (e) {
@@ -665,7 +593,7 @@ class ReactNativeBleTransport {
665
593
  allowDuplicates: true,
666
594
  scanMode: reactNativeBlePlx.ScanMode.LowLatency,
667
595
  }, (error, device) => {
668
- var _a, _b, _c;
596
+ var _a;
669
597
  if (error) {
670
598
  Log === null || Log === void 0 ? void 0 : Log.debug('ble scan error: ', error);
671
599
  if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
@@ -686,9 +614,12 @@ class ReactNativeBleTransport {
686
614
  return;
687
615
  }
688
616
  const displayName = getDeviceDisplayName(device);
689
- const isOneKey = hdShared.isOnekeyDevice((_b = device === null || device === void 0 ? void 0 : device.name) !== null && _b !== void 0 ? _b : null, device === null || device === void 0 ? void 0 : device.id) ||
690
- hdShared.isOnekeyDevice((_c = device === null || device === void 0 ? void 0 : device.localName) !== null && _c !== void 0 ? _c : null, device === null || device === void 0 ? void 0 : device.id) ||
691
- hasKnownOneKeyService(device);
617
+ const isOneKey = hdShared.isOnekeyBluetoothDevice({
618
+ id: device === null || device === void 0 ? void 0 : device.id,
619
+ name: device === null || device === void 0 ? void 0 : device.name,
620
+ localName: device === null || device === void 0 ? void 0 : device.localName,
621
+ serviceUuids: device === null || device === void 0 ? void 0 : device.serviceUUIDs,
622
+ });
692
623
  if (isOneKey) {
693
624
  addDevice(device);
694
625
  }
@@ -703,10 +634,15 @@ class ReactNativeBleTransport {
703
634
  });
704
635
  getConnectedDeviceIds(reactNative.Platform.OS === 'ios' ? getBluetoothServiceUuids() : []).then(devices => {
705
636
  for (const device of devices) {
706
- const { serviceUUIDs } = device;
707
- const hasCachedServiceUuid = Boolean(serviceUUIDs === null || serviceUUIDs === void 0 ? void 0 : serviceUUIDs.length);
708
- const keepDevice = reactNative.Platform.OS === 'ios' || hasCachedServiceUuid;
709
- if (keepDevice) {
637
+ const localName = 'localName' in device && typeof device.localName === 'string'
638
+ ? device.localName
639
+ : null;
640
+ if (hdShared.isOnekeyBluetoothDevice({
641
+ id: device.id,
642
+ name: device.name,
643
+ localName,
644
+ serviceUuids: device.serviceUUIDs,
645
+ })) {
710
646
  Log === null || Log === void 0 ? void 0 : Log.debug('search connected peripheral: ', device.id);
711
647
  addDevice(device);
712
648
  }
@@ -736,8 +672,35 @@ class ReactNativeBleTransport {
736
672
  }));
737
673
  });
738
674
  }
675
+ installTransportForAcquire(uuid, device, characteristics) {
676
+ return __awaiter(this, void 0, void 0, function* () {
677
+ const { writeCharacteristic, notifyCharacteristic } = characteristics !== null && characteristics !== void 0 ? characteristics : (yield this.resolveCharacteristics(device));
678
+ const transport$1 = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
679
+ if (reactNative.Platform.OS === 'android') {
680
+ transport$1.mtuSize = typeof device.mtu === 'number' ? device.mtu : transport$1.mtuSize;
681
+ }
682
+ const monitorToken = this.nextMonitorToken;
683
+ this.nextMonitorToken += 1;
684
+ const notifyTransactionId = `${uuid}:notify:${monitorToken}`;
685
+ transport$1.monitorToken = monitorToken;
686
+ transport$1.notifyTransactionId = notifyTransactionId;
687
+ this.monitorTokens.set(uuid, monitorToken);
688
+ transport$1.notifySubscription = this._monitorCharacteristic(transport$1.notifyCharacteristic, uuid, monitorToken, notifyTransactionId);
689
+ transportCache[uuid] = transport$1;
690
+ this.protocolV2Assemblers.set(uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
691
+ if (reactNative.Platform.OS === 'ios') {
692
+ yield new Promise(resolve => {
693
+ setTimeout(resolve, IOS_NOTIFY_READY_DELAY_MS);
694
+ });
695
+ }
696
+ else if (reactNative.Platform.OS === 'android') {
697
+ yield delay(ANDROID_NOTIFY_READY_DELAY_MS);
698
+ }
699
+ return transport$1;
700
+ });
701
+ }
739
702
  acquire(input) {
740
- var _a;
703
+ var _a, _b;
741
704
  return __awaiter(this, void 0, void 0, function* () {
742
705
  const { uuid, forceCleanRunPromise, expectedProtocol } = input;
743
706
  if (!uuid) {
@@ -760,8 +723,8 @@ class ReactNativeBleTransport {
760
723
  if (forceCleanRunPromise && this.runPromise) {
761
724
  const error = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleForceCleanRunPromise);
762
725
  this.runPromise.reject(error);
763
- this.rejectAllProtocolV2Frames(error);
764
726
  this.runPromise = null;
727
+ this.runPromiseDeviceId = null;
765
728
  Log === null || Log === void 0 ? void 0 : Log.debug('Force clean Bluetooth run promise, forceCleanRunPromise: ', forceCleanRunPromise);
766
729
  }
767
730
  const blePlxManager = yield this.getPlxManager();
@@ -843,38 +806,34 @@ class ReactNativeBleTransport {
843
806
  }
844
807
  }
845
808
  device = yield requestAndroidMtu(device);
846
- const { writeCharacteristic, notifyCharacteristic } = yield this.resolveCharacteristics(device);
809
+ const acquiredDevice = device;
810
+ const { writeCharacteristic, notifyCharacteristic } = yield this.resolveCharacteristics(acquiredDevice);
847
811
  const protocolHint = expectedProtocol
848
812
  ? undefined
849
- : (_a = this.deviceProtocolHints.get(uuid)) !== null && _a !== void 0 ? _a : inferProtocolHintFromDeviceName(getDeviceDisplayName(device));
813
+ : (_b = (_a = input.protocolHint) !== null && _a !== void 0 ? _a : this.deviceProtocolHints.get(uuid)) !== null && _b !== void 0 ? _b : inferProtocolHintFromDeviceName(getDeviceDisplayName(acquiredDevice));
850
814
  yield this.release(uuid, true);
851
815
  if (protocolHint) {
852
816
  this.deviceProtocolHints.set(uuid, protocolHint);
853
817
  }
854
- const transport$1 = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
855
- if (reactNative.Platform.OS === 'android') {
856
- transport$1.mtuSize = typeof device.mtu === 'number' ? device.mtu : transport$1.mtuSize;
857
- }
858
- const monitorToken = this.nextMonitorToken;
859
- this.nextMonitorToken += 1;
860
- const notifyTransactionId = `${uuid}:notify:${monitorToken}`;
861
- transport$1.monitorToken = monitorToken;
862
- transport$1.notifyTransactionId = notifyTransactionId;
863
- this.monitorTokens.set(uuid, monitorToken);
864
- transport$1.notifySubscription = this._monitorCharacteristic(transport$1.notifyCharacteristic, uuid, monitorToken, notifyTransactionId);
865
- transportCache[uuid] = transport$1;
866
- this.protocolV2Assemblers.set(uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
867
- if (reactNative.Platform.OS === 'ios') {
868
- yield new Promise(resolve => {
869
- setTimeout(resolve, IOS_NOTIFY_READY_DELAY_MS);
870
- });
818
+ yield this.installTransportForAcquire(uuid, acquiredDevice, {
819
+ writeCharacteristic,
820
+ notifyCharacteristic,
821
+ });
822
+ try {
823
+ const protocolType = yield this.detectProtocol(uuid, expectedProtocol, protocolHint, () => __awaiter(this, void 0, void 0, function* () {
824
+ yield this.installTransportForAcquire(uuid, acquiredDevice);
825
+ }));
826
+ const currentTransport = transportCache[uuid];
827
+ if (!currentTransport) {
828
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotFound);
829
+ }
830
+ this.attachDisconnectSubscription(currentTransport, acquiredDevice, uuid);
831
+ return { uuid, protocolType };
871
832
  }
872
- else if (reactNative.Platform.OS === 'android') {
873
- yield delay(ANDROID_NOTIFY_READY_DELAY_MS);
833
+ catch (error) {
834
+ yield this.release(uuid, true);
835
+ throw error;
874
836
  }
875
- const protocolType = yield this.detectProtocol(uuid, expectedProtocol, protocolHint);
876
- this.attachDisconnectSubscription(transport$1, device, uuid);
877
- return { uuid, protocolType };
878
837
  });
879
838
  }
880
839
  _monitorCharacteristic(characteristic, uuid, monitorToken, notifyTransactionId) {
@@ -911,7 +870,7 @@ class ReactNativeBleTransport {
911
870
  this.rejectProtocolV2Frames(uuid, hdShared.ERRORS.TypedError(errorCode));
912
871
  return;
913
872
  }
914
- if (this.runPromise) {
873
+ if (this.runPromise && this.runPromiseDeviceId === uuid) {
915
874
  let ERROR = hdShared.HardwareErrorCode.BleCharacteristicNotifyError;
916
875
  if ((_h = error.reason) === null || _h === void 0 ? void 0 : _h.includes('The connection has timed out unexpectedly')) {
917
876
  ERROR = hdShared.HardwareErrorCode.BleTimeoutError;
@@ -926,13 +885,11 @@ class ReactNativeBleTransport {
926
885
  ((_p = error.reason) === null || _p === void 0 ? void 0 : _p.includes('notify change failed for device'))) {
927
886
  const notifyError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure);
928
887
  this.runPromise.reject(notifyError);
929
- this.rejectAllProtocolV2Frames(notifyError);
930
888
  Log === null || Log === void 0 ? void 0 : Log.debug(`${hdShared.HardwareErrorCode.BleCharacteristicNotifyChangeFailure} ${error.message} ${error.reason}`);
931
889
  return;
932
890
  }
933
891
  const notifyError = hdShared.ERRORS.TypedError(ERROR);
934
892
  this.runPromise.reject(notifyError);
935
- this.rejectAllProtocolV2Frames(notifyError);
936
893
  Log === null || Log === void 0 ? void 0 : Log.debug(': monitor notify error, and has unreleased Promise', Error);
937
894
  }
938
895
  return;
@@ -966,7 +923,9 @@ class ReactNativeBleTransport {
966
923
  const value = buffer.Buffer.from(buffer$1);
967
924
  bufferLength = 0;
968
925
  buffer$1 = [];
969
- (_q = this.runPromise) === null || _q === void 0 ? void 0 : _q.resolve(value.toString('hex'));
926
+ if (this.runPromiseDeviceId === uuid) {
927
+ (_q = this.runPromise) === null || _q === void 0 ? void 0 : _q.resolve(value.toString('hex'));
928
+ }
970
929
  }
971
930
  }
972
931
  catch (error) {
@@ -975,7 +934,7 @@ class ReactNativeBleTransport {
975
934
  if (this.deviceProtocol.get(uuid) === 'V2') {
976
935
  this.rejectProtocolV2Frames(uuid, notifyError);
977
936
  }
978
- else {
937
+ else if (this.runPromiseDeviceId === uuid) {
979
938
  (_r = this.runPromise) === null || _r === void 0 ? void 0 : _r.reject(notifyError);
980
939
  }
981
940
  }
@@ -992,11 +951,12 @@ class ReactNativeBleTransport {
992
951
  var _a, _b, _c, _d, _e, _f, _g;
993
952
  return __awaiter(this, void 0, void 0, function* () {
994
953
  const transport = transportCache[uuid];
995
- if (this.runPromise) {
954
+ if (this.runPromise && this.runPromiseDeviceId === uuid) {
996
955
  const error = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleForceCleanRunPromise);
997
956
  this.runPromise.reject(error);
998
957
  this.runPromise = null;
999
- this.rejectAllProtocolV2Frames(error);
958
+ this.runPromiseDeviceId = null;
959
+ this.rejectProtocolV2Frames(uuid, error);
1000
960
  }
1001
961
  else {
1002
962
  this.resetProtocolV2Frames(uuid);
@@ -1056,7 +1016,7 @@ class ReactNativeBleTransport {
1056
1016
  if (!protocol) {
1057
1017
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Device protocol has not been detected for ${uuid}`);
1058
1018
  }
1059
- Log === null || Log === void 0 ? void 0 : Log.debug('transport call', createTransportCallLog(name, protocol, data));
1019
+ Log === null || Log === void 0 ? void 0 : Log.debug('transport call', transport.createTransportCallLog(name, protocol, data));
1060
1020
  if (protocol === 'V2') {
1061
1021
  return this.callProtocolV2(uuid, name, data, options);
1062
1022
  }
@@ -1076,6 +1036,7 @@ class ReactNativeBleTransport {
1076
1036
  const runPromise = hdShared.createDeferred();
1077
1037
  runPromise.promise.catch(() => undefined);
1078
1038
  this.runPromise = runPromise;
1039
+ this.runPromiseDeviceId = uuid;
1079
1040
  const messages = this._messages;
1080
1041
  const buffers = ProtocolV1.encodeTransportPackets(messages, name, data);
1081
1042
  let timeout;
@@ -1158,32 +1119,14 @@ class ReactNativeBleTransport {
1158
1119
  if (!retryType || attempt >= FIRMWARE_UPLOAD_WRITE_MAX_RETRIES) {
1159
1120
  throw error;
1160
1121
  }
1161
- const shouldReconnect = retryType === 'reconnectable';
1162
- const delayMs = shouldReconnect
1163
- ? FIRMWARE_UPLOAD_RECONNECT_RETRY_DELAY_MS
1164
- : resolveFirmwareUploadRetryDelay(attempt);
1122
+ const delayMs = resolveFirmwareUploadRetryDelay(attempt);
1165
1123
  Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] FirmwareUpload write retry:', {
1166
1124
  attempt: attempt + 1,
1167
1125
  delayMs,
1168
- reconnect: shouldReconnect,
1169
1126
  error,
1170
1127
  });
1171
- if (shouldReconnect) {
1172
- this.firmwareUploadWriteRecoveryIds.add(uuid);
1173
- }
1174
1128
  yield delay(delayMs);
1175
1129
  attempt += 1;
1176
- if (shouldReconnect) {
1177
- try {
1178
- yield this.reconnectFirmwareUploadTransport(uuid, transport);
1179
- }
1180
- catch (e) {
1181
- Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] FirmwareUpload reconnect error:', e);
1182
- if (attempt >= FIRMWARE_UPLOAD_WRITE_MAX_RETRIES) {
1183
- throw e;
1184
- }
1185
- }
1186
- }
1187
1130
  }
1188
1131
  }
1189
1132
  }), e => {
@@ -1232,12 +1175,17 @@ class ReactNativeBleTransport {
1232
1175
  return check.call(jsonData);
1233
1176
  }
1234
1177
  catch (e) {
1235
- if (name === 'Initialize' && (options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS) {
1236
- Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Protocol V1 Initialize probe call failed:', e);
1178
+ if (name === 'GetFeatures' && (options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS) {
1179
+ Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Protocol V1 GetFeatures probe call failed:', e);
1237
1180
  }
1238
1181
  else {
1239
1182
  Log === null || Log === void 0 ? void 0 : Log.error('call error: ', e);
1240
1183
  }
1184
+ const isProbeTimeout = name === 'GetFeatures' && (options === null || options === void 0 ? void 0 : options.timeoutMs) === PROTOCOL_PROBE_TIMEOUT_MS;
1185
+ if (!isProbeTimeout &&
1186
+ (e === null || e === void 0 ? void 0 : e.errorCode) === hdShared.HardwareErrorCode.BleTimeoutError) {
1187
+ yield this.disconnect(uuid);
1188
+ }
1241
1189
  throw e;
1242
1190
  }
1243
1191
  finally {
@@ -1245,6 +1193,7 @@ class ReactNativeBleTransport {
1245
1193
  clearTimeout(timeout);
1246
1194
  if (this.runPromise === runPromise) {
1247
1195
  this.runPromise = null;
1196
+ this.runPromiseDeviceId = null;
1248
1197
  }
1249
1198
  }
1250
1199
  });
@@ -1323,6 +1272,7 @@ class ReactNativeBleTransport {
1323
1272
  Log === null || Log === void 0 ? void 0 : Log.debug('transport-react-native transport cancel');
1324
1273
  if (this.runPromise) ;
1325
1274
  this.runPromise = null;
1275
+ this.runPromiseDeviceId = null;
1326
1276
  }
1327
1277
  getCachedTransport(uuid) {
1328
1278
  const transport = transportCache[uuid];
@@ -1335,14 +1285,14 @@ class ReactNativeBleTransport {
1335
1285
  return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Device protocol mismatch: expected ${expected}, but device did not respond to expected protocol`);
1336
1286
  }
1337
1287
  createProtocolDetectionError() {
1338
- return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleTimeoutError, 'Unable to detect BLE protocol: device did not respond to Protocol V1 Initialize or Protocol V2 Ping');
1288
+ return hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleTimeoutError, 'Unable to detect BLE protocol: device did not respond to Protocol V1 GetFeatures or Protocol V2 Ping');
1339
1289
  }
1340
1290
  clearProbeProtocol(uuid, protocol) {
1341
1291
  if (this.deviceProtocol.get(uuid) === protocol) {
1342
1292
  this.deviceProtocol.delete(uuid);
1343
1293
  }
1344
1294
  }
1345
- detectProtocol(uuid, expectedProtocol, protocolHint) {
1295
+ detectProtocol(uuid, expectedProtocol, protocolHint, rebuildTransport) {
1346
1296
  return __awaiter(this, void 0, void 0, function* () {
1347
1297
  if (expectedProtocol === 'V1') {
1348
1298
  if (yield this.probeProtocolV1(uuid)) {
@@ -1373,6 +1323,12 @@ class ReactNativeBleTransport {
1373
1323
  const protocol = probeOrder[i];
1374
1324
  if (i > 0) {
1375
1325
  yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
1326
+ if (!transportCache[uuid]) {
1327
+ if (!rebuildTransport) {
1328
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotFound);
1329
+ }
1330
+ yield rebuildTransport();
1331
+ }
1376
1332
  }
1377
1333
  const detected = protocol === 'V1' ? yield this.probeProtocolV1(uuid) : yield this.probeProtocolV2(uuid);
1378
1334
  if (detected) {
@@ -1438,12 +1394,12 @@ class ReactNativeBleTransport {
1438
1394
  }
1439
1395
  try {
1440
1396
  this.deviceProtocol.set(uuid, 'V1');
1441
- yield this.callProtocolV1(uuid, 'Initialize', {}, { timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS });
1397
+ yield this.callProtocolV1(uuid, 'GetFeatures', {}, { timeoutMs: PROTOCOL_PROBE_TIMEOUT_MS });
1442
1398
  return true;
1443
1399
  }
1444
1400
  catch (error) {
1445
1401
  this.clearProbeProtocol(uuid, 'V1');
1446
- Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Protocol V1 Initialize probe failed:', error);
1402
+ Log === null || Log === void 0 ? void 0 : Log.debug('[ReactNativeBleTransport] Protocol V1 GetFeatures probe failed:', error);
1447
1403
  return false;
1448
1404
  }
1449
1405
  });
@@ -1512,16 +1468,8 @@ class ReactNativeBleTransport {
1512
1468
  }
1513
1469
  this.getProtocolV2FrameQueue(uuid).push(frame);
1514
1470
  }
1515
- rejectAllProtocolV2Frames(error) {
1516
- this.protocolV2FrameQueues.clear();
1517
- for (const framePromise of this.protocolV2FramePromises.values()) {
1518
- framePromise.reject(error);
1519
- }
1520
- this.protocolV2FramePromises.clear();
1521
- }
1522
1471
  resetProtocolV2Frames(uuid) {
1523
- this.protocolV2FrameQueues.delete(uuid);
1524
- this.protocolV2FramePromises.delete(uuid);
1472
+ this.rejectProtocolV2Frames(uuid, new Error(`Protocol V2 frame state reset for ${uuid}`));
1525
1473
  }
1526
1474
  rejectProtocolV2Frames(uuid, error) {
1527
1475
  this.protocolV2FrameQueues.delete(uuid);
@@ -1674,5 +1622,6 @@ class ReactNativeBleTransport {
1674
1622
 
1675
1623
  exports.configureProtocolV2BleTuning = configureProtocolV2BleTuning;
1676
1624
  exports["default"] = ReactNativeBleTransport;
1625
+ exports.getFirmwareUploadWriteRetryType = getFirmwareUploadWriteRetryType;
1677
1626
  exports.getProtocolV2BleTuning = getProtocolV2BleTuning;
1678
1627
  exports.resetProtocolV2BleTuning = resetProtocolV2BleTuning;
@@ -1,13 +1,2 @@
1
- import type { ProtocolType } from '@onekeyfe/hd-transport';
2
- export declare function createTransportCallLog(name: string, protocol: ProtocolType, data: Record<string, unknown>): {
3
- name: string;
4
- protocol: ProtocolType;
5
- file_name: unknown;
6
- hash: unknown;
7
- } | {
8
- name: string;
9
- protocol: ProtocolType;
10
- file_name?: undefined;
11
- hash?: undefined;
12
- };
1
+ export { createTransportCallLog } from '@onekeyfe/hd-transport';
13
2
  //# sourceMappingURL=transportLog.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transportLog.d.ts","sourceRoot":"","sources":["../src/transportLog.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,YAAY,EACtB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;;;;;;;;;;EAY9B"}
1
+ {"version":3,"file":"transportLog.d.ts","sourceRoot":"","sources":["../src/transportLog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC"}
package/dist/types.d.ts CHANGED
@@ -7,5 +7,6 @@ export type BleAcquireInput = {
7
7
  uuid: string;
8
8
  forceCleanRunPromise?: boolean;
9
9
  expectedProtocol?: ProtocolType;
10
+ protocolHint?: ProtocolType;
10
11
  };
11
12
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,YAAY,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAExE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,gBAAgB,CAAC,EAAE,YAAY,CAAC;CACjC,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAE3D,YAAY,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAExE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,gBAAgB,CAAC,EAAE,YAAY,CAAC;IAChC,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "1.2.0-alpha.33",
3
+ "version": "1.2.0-alpha.35",
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.0-alpha.33",
24
- "@onekeyfe/hd-shared": "1.2.0-alpha.33",
25
- "@onekeyfe/hd-transport": "1.2.0-alpha.33",
23
+ "@onekeyfe/hd-core": "1.2.0-alpha.35",
24
+ "@onekeyfe/hd-shared": "1.2.0-alpha.35",
25
+ "@onekeyfe/hd-transport": "1.2.0-alpha.35",
26
26
  "@onekeyfe/react-native-ble-utils": "^0.1.6",
27
27
  "react-native-ble-plx": "3.5.1"
28
28
  },
29
- "gitHead": "965e837c93fc67225ccfac28d22c3403ff96ad25"
29
+ "gitHead": "638bd33030686c4a3321e4feb4924c3f9f5c81a8"
30
30
  }