@onekeyfe/hd-transport-react-native 1.2.0-alpha.34 → 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.
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,iBAAiB,MAAM,CAAC;AACrC,eAAO,MAAM,qBAAqB,MAAM,CAAC;AACzC,eAAO,MAAM,mBAAmB,KAAK,CAAC;AA8BtC,eAAO,MAAM,wBAAwB,gBAA0B,CAAC;AAChE,eAAO,MAAM,sBAAsB,gBAAiB,MAAM,cAAc,SAAS;iBA1BhE,MAAM;;;QA2CtB,CAAC;AAEF,eAAO,MAAM,gBAAgB,UAAW,MAAM,GAAG,IAAI,WACP,CAAC;AAE/C,eAAO,MAAM,aAAa,UAAW,MAAM,GAAG,IAAI,UAAU,MAAM,GAAG,IAAI,YAGxE,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,iBAAiB,MAAM,CAAC;AACrC,eAAO,MAAM,qBAAqB,MAAM,CAAC;AACzC,eAAO,MAAM,mBAAmB,KAAK,CAAC;AA8BtC,eAAO,MAAM,wBAAwB,gBAA0B,CAAC;AAChE,eAAO,MAAM,sBAAsB,gBAAiB,MAAM,cAAc,SAAS;iBA1BhE,MAAM;;;QA2CtB,CAAC;AAEF,eAAO,MAAM,gBAAgB,UAAW,MAAM,GAAG,IAAI,WACP,CAAC;AAE/C,eAAO,MAAM,aAAa,UAAW,MAAM,GAAG,IAAI,UAAU,MAAM,GAAG,IAAI,YAGxE,CAAC"}
package/dist/index.d.ts CHANGED
@@ -85,6 +85,7 @@ declare class ReactNativeBleTransport {
85
85
  * @returns
86
86
  */
87
87
  enumerate(): Promise<IOneKeyDevice[]>;
88
+ private installTransportForAcquire;
88
89
  acquire(input: BleAcquireInput):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AM,CAAC,MAAM,EAAE,OAAO,CAAC;IAIjE,IAAI,CACR,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,OAAO,CAAC,EAAE,oBAAoB;YA+BlB,cAAc;IAqM5B,IAAI;IAIE,UAAU,CAAC,OAAO,EAAE,MAAM;IA8EhC,MAAM;IASN,OAAO,CAAC,kBAAkB;IAQ1B,OAAO,CAAC,2BAA2B;IAOnC,OAAO,CAAC,4BAA4B;IAOpC,OAAO,CAAC,kBAAkB;YAMZ,cAAc;YAkEd,iCAAiC;YAoDjC,eAAe;YAkBf,eAAe;IAwB7B,OAAO,CAAC,4BAA4B;IA2BpC,OAAO,CAAC,uBAAuB;IAS/B,OAAO,CAAC,sBAAsB;IAU9B,OAAO,CAAC,qBAAqB;IAI7B,OAAO,CAAC,sBAAsB;YAShB,mBAAmB;YAiBnB,qBAAqB;YAmCrB,oBAAoB;YAiCpB,cAAc;IAoC5B,OAAO,CAAC,uBAAuB;IA4C/B,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
package/dist/index.js CHANGED
@@ -215,18 +215,6 @@ class BleTransport {
215
215
  }
216
216
  }
217
217
 
218
- function createTransportCallLog(name, protocol, data) {
219
- if (name === 'ResourceUpdate' || name === 'ResourceAck') {
220
- return {
221
- name,
222
- protocol,
223
- file_name: data.file_name,
224
- hash: data.hash,
225
- };
226
- }
227
- return { name, protocol };
228
- }
229
-
230
218
  const { check, ProtocolV1, parseConfigure } = transport__default["default"];
231
219
  const Log = bleLogger;
232
220
  const transportCache = {};
@@ -684,6 +672,33 @@ class ReactNativeBleTransport {
684
672
  }));
685
673
  });
686
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
+ }
687
702
  acquire(input) {
688
703
  var _a, _b;
689
704
  return __awaiter(this, void 0, void 0, function* () {
@@ -791,38 +806,34 @@ class ReactNativeBleTransport {
791
806
  }
792
807
  }
793
808
  device = yield requestAndroidMtu(device);
794
- const { writeCharacteristic, notifyCharacteristic } = yield this.resolveCharacteristics(device);
809
+ const acquiredDevice = device;
810
+ const { writeCharacteristic, notifyCharacteristic } = yield this.resolveCharacteristics(acquiredDevice);
795
811
  const protocolHint = expectedProtocol
796
812
  ? undefined
797
- : (_b = (_a = input.protocolHint) !== null && _a !== void 0 ? _a : this.deviceProtocolHints.get(uuid)) !== null && _b !== void 0 ? _b : 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));
798
814
  yield this.release(uuid, true);
799
815
  if (protocolHint) {
800
816
  this.deviceProtocolHints.set(uuid, protocolHint);
801
817
  }
802
- const transport$1 = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
803
- if (reactNative.Platform.OS === 'android') {
804
- transport$1.mtuSize = typeof device.mtu === 'number' ? device.mtu : transport$1.mtuSize;
805
- }
806
- const monitorToken = this.nextMonitorToken;
807
- this.nextMonitorToken += 1;
808
- const notifyTransactionId = `${uuid}:notify:${monitorToken}`;
809
- transport$1.monitorToken = monitorToken;
810
- transport$1.notifyTransactionId = notifyTransactionId;
811
- this.monitorTokens.set(uuid, monitorToken);
812
- transport$1.notifySubscription = this._monitorCharacteristic(transport$1.notifyCharacteristic, uuid, monitorToken, notifyTransactionId);
813
- transportCache[uuid] = transport$1;
814
- this.protocolV2Assemblers.set(uuid, new transport.ProtocolV2FrameAssembler(transport.PROTOCOL_V2_BLE_FRAME_MAX_BYTES));
815
- if (reactNative.Platform.OS === 'ios') {
816
- yield new Promise(resolve => {
817
- setTimeout(resolve, IOS_NOTIFY_READY_DELAY_MS);
818
- });
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 };
819
832
  }
820
- else if (reactNative.Platform.OS === 'android') {
821
- yield delay(ANDROID_NOTIFY_READY_DELAY_MS);
833
+ catch (error) {
834
+ yield this.release(uuid, true);
835
+ throw error;
822
836
  }
823
- const protocolType = yield this.detectProtocol(uuid, expectedProtocol, protocolHint);
824
- this.attachDisconnectSubscription(transport$1, device, uuid);
825
- return { uuid, protocolType };
826
837
  });
827
838
  }
828
839
  _monitorCharacteristic(characteristic, uuid, monitorToken, notifyTransactionId) {
@@ -1005,7 +1016,7 @@ class ReactNativeBleTransport {
1005
1016
  if (!protocol) {
1006
1017
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `Device protocol has not been detected for ${uuid}`);
1007
1018
  }
1008
- 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));
1009
1020
  if (protocol === 'V2') {
1010
1021
  return this.callProtocolV2(uuid, name, data, options);
1011
1022
  }
@@ -1281,7 +1292,7 @@ class ReactNativeBleTransport {
1281
1292
  this.deviceProtocol.delete(uuid);
1282
1293
  }
1283
1294
  }
1284
- detectProtocol(uuid, expectedProtocol, protocolHint) {
1295
+ detectProtocol(uuid, expectedProtocol, protocolHint, rebuildTransport) {
1285
1296
  return __awaiter(this, void 0, void 0, function* () {
1286
1297
  if (expectedProtocol === 'V1') {
1287
1298
  if (yield this.probeProtocolV1(uuid)) {
@@ -1313,11 +1324,10 @@ class ReactNativeBleTransport {
1313
1324
  if (i > 0) {
1314
1325
  yield this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
1315
1326
  if (!transportCache[uuid]) {
1316
- const reacquired = yield this.acquire({
1317
- uuid,
1318
- expectedProtocol: protocol,
1319
- });
1320
- return reacquired.protocolType;
1327
+ if (!rebuildTransport) {
1328
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotFound);
1329
+ }
1330
+ yield rebuildTransport();
1321
1331
  }
1322
1332
  }
1323
1333
  const detected = protocol === 'V1' ? yield this.probeProtocolV1(uuid) : yield this.probeProtocolV2(uuid);
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "1.2.0-alpha.34",
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.34",
24
- "@onekeyfe/hd-shared": "1.2.0-alpha.34",
25
- "@onekeyfe/hd-transport": "1.2.0-alpha.34",
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": "07f10b4c9fab177da7c36d976ec18b075492b57f"
29
+ "gitHead": "638bd33030686c4a3321e4feb4924c3f9f5c81a8"
30
30
  }
@@ -186,6 +186,8 @@ const createHarness = () => {
186
186
 
187
187
  const createV1Harness = () => {
188
188
  const uuid = 'rn-classic-id';
189
+ const notifySubscriptionRemovers: jest.Mock[] = [];
190
+ const disconnectSubscriptionRemovers: jest.Mock[] = [];
189
191
  let notifyCallback:
190
192
  | ((error: Error | null, characteristic: { value: string } | null) => void)
191
193
  | undefined;
@@ -195,7 +197,9 @@ const createV1Harness = () => {
195
197
  isNotifiable: true,
196
198
  monitor: jest.fn(callback => {
197
199
  notifyCallback = callback;
198
- return { remove: jest.fn() };
200
+ const remove = jest.fn();
201
+ notifySubscriptionRemovers.push(remove);
202
+ return { remove };
199
203
  }),
200
204
  };
201
205
  let writeCount = 0;
@@ -221,21 +225,33 @@ const createV1Harness = () => {
221
225
  serviceUUIDs: ['00000001-0000-1000-8000-00805f9b34fb'],
222
226
  isConnected: jest.fn(() => Promise.resolve(true)),
223
227
  cancelConnection: jest.fn(() => Promise.resolve()),
224
- onDisconnected: jest.fn(() => ({ remove: jest.fn() })),
228
+ onDisconnected: jest.fn(() => {
229
+ const remove = jest.fn();
230
+ disconnectSubscriptionRemovers.push(remove);
231
+ return { remove };
232
+ }),
225
233
  };
226
234
  const transport = new ReactNativeBleTransport({ scanTimeout: 1 });
227
- transport.blePlxManager = {
235
+ const bleManager = {
228
236
  devices: jest.fn(() => Promise.resolve([device])),
229
237
  connectedDevices: jest.fn(() => Promise.resolve([])),
230
238
  cancelTransaction: jest.fn(() => Promise.resolve()),
231
- } as any;
239
+ };
240
+ transport.blePlxManager = bleManager as any;
232
241
  transport.resolveCharacteristics = jest.fn(() =>
233
242
  Promise.resolve({ writeCharacteristic, notifyCharacteristic })
234
243
  );
235
244
  transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
236
245
  transport.configure(protocolV1Schema);
237
246
  transport.configureProtocolV2(protocolV2Schema);
238
- return { transport, uuid, device };
247
+ return {
248
+ transport,
249
+ uuid,
250
+ device,
251
+ bleManager,
252
+ notifySubscriptionRemovers,
253
+ disconnectSubscriptionRemovers,
254
+ };
239
255
  };
240
256
 
241
257
  describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
@@ -287,7 +303,8 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
287
303
  });
288
304
 
289
305
  test('reconnects before falling back to Protocol V1 after a fatal V2 probe failure', async () => {
290
- const { transport, uuid } = createV1Harness();
306
+ const { transport, uuid, device, notifySubscriptionRemovers, disconnectSubscriptionRemovers } =
307
+ createV1Harness();
291
308
  const probeProtocolV2 = jest
292
309
  .spyOn(transport as any, 'probeProtocolV2')
293
310
  .mockImplementationOnce(async () => {
@@ -304,6 +321,35 @@ describe('ReactNativeBleTransport Protocol V2 link lifecycle', () => {
304
321
  expect(probeProtocolV2).toHaveBeenCalledTimes(1);
305
322
  expect(resolveCharacteristics).toHaveBeenCalledTimes(2);
306
323
  expect(transport.getProtocolType(uuid)).toBe('V1');
324
+ expect(device.onDisconnected).toHaveBeenCalledTimes(1);
325
+ expect(notifySubscriptionRemovers).toHaveLength(2);
326
+ expect(notifySubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
327
+
328
+ await transport.release(uuid, true);
329
+
330
+ expect(notifySubscriptionRemovers[1]).toHaveBeenCalledTimes(1);
331
+ expect(disconnectSubscriptionRemovers).toHaveLength(1);
332
+ expect(disconnectSubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
333
+ });
334
+
335
+ test('cleans the rebuilt transport when Protocol V1 fallback also fails', async () => {
336
+ const { transport, uuid, device, bleManager, notifySubscriptionRemovers } = createV1Harness();
337
+ jest.spyOn(transport as any, 'probeProtocolV2').mockImplementationOnce(async () => {
338
+ await (transport as any).releaseNative(uuid, true);
339
+ return false;
340
+ });
341
+ jest.spyOn(transport as any, 'probeProtocolV1').mockResolvedValue(false);
342
+
343
+ await expect(transport.acquire({ uuid, protocolHint: 'V2' })).rejects.toMatchObject({
344
+ errorCode: HardwareErrorCode.BleTimeoutError,
345
+ });
346
+
347
+ expect(device.onDisconnected).not.toHaveBeenCalled();
348
+ expect(notifySubscriptionRemovers).toHaveLength(2);
349
+ expect(notifySubscriptionRemovers[0]).toHaveBeenCalledTimes(1);
350
+ expect(notifySubscriptionRemovers[1]).toHaveBeenCalledTimes(1);
351
+ expect(bleManager.cancelTransaction).toHaveBeenCalled();
352
+ expect(transport.getProtocolType(uuid)).toBeUndefined();
307
353
  });
308
354
 
309
355
  test('disconnects and invalidates a Protocol V1 link after a response timeout', async () => {
package/src/constants.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { createKnownBleUuidAliases, matchesKnownBleUuid } from '@onekeyfe/hd-shared';
2
+
1
3
  export const IOS_PACKET_LENGTH = 128;
2
4
  export const ANDROID_PACKET_LENGTH = 192;
3
5
  export const ANDROID_DEFAULT_MTU = 23;
@@ -57,4 +59,3 @@ export const isSameBleUuid = (left?: string | null, right?: string | null) => {
57
59
  if (!left || !right) return false;
58
60
  return matchesKnownBleUuid(left, createKnownBleUuidAliases(right));
59
61
  };
60
- import { createKnownBleUuidAliases, matchesKnownBleUuid } from '@onekeyfe/hd-shared';
package/src/index.ts CHANGED
@@ -649,6 +649,46 @@ export default class ReactNativeBleTransport {
649
649
  });
650
650
  }
651
651
 
652
+ private async installTransportForAcquire(
653
+ uuid: string,
654
+ device: Device,
655
+ characteristics?: ResolvedBleCharacteristics
656
+ ) {
657
+ const { writeCharacteristic, notifyCharacteristic } =
658
+ characteristics ?? (await this.resolveCharacteristics(device));
659
+ const transport = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
660
+ if (Platform.OS === 'android') {
661
+ transport.mtuSize = typeof device.mtu === 'number' ? device.mtu : transport.mtuSize;
662
+ }
663
+ const monitorToken = this.nextMonitorToken;
664
+ this.nextMonitorToken += 1;
665
+ const notifyTransactionId = `${uuid}:notify:${monitorToken}`;
666
+ transport.monitorToken = monitorToken;
667
+ transport.notifyTransactionId = notifyTransactionId;
668
+ this.monitorTokens.set(uuid, monitorToken);
669
+ transport.notifySubscription = this._monitorCharacteristic(
670
+ transport.notifyCharacteristic,
671
+ uuid,
672
+ monitorToken,
673
+ notifyTransactionId
674
+ );
675
+ transportCache[uuid] = transport;
676
+ this.protocolV2Assemblers.set(
677
+ uuid,
678
+ new ProtocolV2FrameAssembler(PROTOCOL_V2_BLE_FRAME_MAX_BYTES)
679
+ );
680
+
681
+ if (Platform.OS === 'ios') {
682
+ await new Promise<void>(resolve => {
683
+ setTimeout(resolve, IOS_NOTIFY_READY_DELAY_MS);
684
+ });
685
+ } else if (Platform.OS === 'android') {
686
+ await delay(ANDROID_NOTIFY_READY_DELAY_MS);
687
+ }
688
+
689
+ return transport;
690
+ }
691
+
652
692
  async acquire(input: BleAcquireInput) {
653
693
  const { uuid, forceCleanRunPromise, expectedProtocol } = input;
654
694
 
@@ -772,13 +812,16 @@ export default class ReactNativeBleTransport {
772
812
  }
773
813
 
774
814
  device = await requestAndroidMtu(device);
775
- const { writeCharacteristic, notifyCharacteristic } = await this.resolveCharacteristics(device);
815
+ const acquiredDevice = device;
816
+ const { writeCharacteristic, notifyCharacteristic } = await this.resolveCharacteristics(
817
+ acquiredDevice
818
+ );
776
819
 
777
820
  const protocolHint = expectedProtocol
778
821
  ? undefined
779
822
  : input.protocolHint ??
780
823
  this.deviceProtocolHints.get(uuid) ??
781
- inferProtocolHintFromDeviceName(getDeviceDisplayName(device));
824
+ inferProtocolHintFromDeviceName(getDeviceDisplayName(acquiredDevice));
782
825
 
783
826
  // release transport before new transport instance
784
827
  await this.release(uuid, true);
@@ -786,42 +829,30 @@ export default class ReactNativeBleTransport {
786
829
  this.deviceProtocolHints.set(uuid, protocolHint);
787
830
  }
788
831
 
789
- const transport = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
790
- if (Platform.OS === 'android') {
791
- transport.mtuSize = typeof device.mtu === 'number' ? device.mtu : transport.mtuSize;
792
- }
793
- const monitorToken = this.nextMonitorToken;
794
- this.nextMonitorToken += 1;
795
- const notifyTransactionId = `${uuid}:notify:${monitorToken}`;
796
- transport.monitorToken = monitorToken;
797
- transport.notifyTransactionId = notifyTransactionId;
798
- this.monitorTokens.set(uuid, monitorToken);
799
- transport.notifySubscription = this._monitorCharacteristic(
800
- transport.notifyCharacteristic,
801
- uuid,
802
- monitorToken,
803
- notifyTransactionId
804
- );
805
- transportCache[uuid] = transport;
806
-
807
- this.protocolV2Assemblers.set(
808
- uuid,
809
- new ProtocolV2FrameAssembler(PROTOCOL_V2_BLE_FRAME_MAX_BYTES)
810
- );
832
+ await this.installTransportForAcquire(uuid, acquiredDevice, {
833
+ writeCharacteristic,
834
+ notifyCharacteristic,
835
+ });
811
836
 
812
- if (Platform.OS === 'ios') {
813
- await new Promise<void>(resolve => {
814
- setTimeout(resolve, IOS_NOTIFY_READY_DELAY_MS);
815
- });
816
- } else if (Platform.OS === 'android') {
817
- await delay(ANDROID_NOTIFY_READY_DELAY_MS);
837
+ try {
838
+ const protocolType = await this.detectProtocol(
839
+ uuid,
840
+ expectedProtocol,
841
+ protocolHint,
842
+ async () => {
843
+ await this.installTransportForAcquire(uuid, acquiredDevice);
844
+ }
845
+ );
846
+ const currentTransport = transportCache[uuid];
847
+ if (!currentTransport) {
848
+ throw ERRORS.TypedError(HardwareErrorCode.TransportNotFound);
849
+ }
850
+ this.attachDisconnectSubscription(currentTransport, acquiredDevice, uuid);
851
+ return { uuid, protocolType };
852
+ } catch (error) {
853
+ await this.release(uuid, true);
854
+ throw error;
818
855
  }
819
-
820
- const protocolType = await this.detectProtocol(uuid, expectedProtocol, protocolHint);
821
-
822
- this.attachDisconnectSubscription(transport, device, uuid);
823
-
824
- return { uuid, protocolType };
825
856
  }
826
857
 
827
858
  _monitorCharacteristic(
@@ -1388,7 +1419,8 @@ export default class ReactNativeBleTransport {
1388
1419
  private async detectProtocol(
1389
1420
  uuid: string,
1390
1421
  expectedProtocol?: ProtocolType,
1391
- protocolHint?: ProtocolType
1422
+ protocolHint?: ProtocolType,
1423
+ rebuildTransport?: () => Promise<void>
1392
1424
  ): Promise<ProtocolType> {
1393
1425
  if (expectedProtocol === 'V1') {
1394
1426
  if (await this.probeProtocolV1(uuid)) {
@@ -1427,11 +1459,10 @@ export default class ReactNativeBleTransport {
1427
1459
  // Reset subscriptions and buffers after a failed probe before trying another protocol.
1428
1460
  await this.resetProbeStateAfterProtocolProbe(uuid, probeOrder[i - 1]);
1429
1461
  if (!transportCache[uuid]) {
1430
- const reacquired = await this.acquire({
1431
- uuid,
1432
- expectedProtocol: protocol,
1433
- });
1434
- return reacquired.protocolType;
1462
+ if (!rebuildTransport) {
1463
+ throw ERRORS.TypedError(HardwareErrorCode.TransportNotFound);
1464
+ }
1465
+ await rebuildTransport();
1435
1466
  }
1436
1467
  }
1437
1468
  const detected =
@@ -1,18 +1 @@
1
- import type { ProtocolType } from '@onekeyfe/hd-transport';
2
-
3
- export function createTransportCallLog(
4
- name: string,
5
- protocol: ProtocolType,
6
- data: Record<string, unknown>
7
- ) {
8
- if (name === 'ResourceUpdate' || name === 'ResourceAck') {
9
- return {
10
- name,
11
- protocol,
12
- file_name: data.file_name,
13
- hash: data.hash,
14
- };
15
- }
16
-
17
- return { name, protocol };
18
- }
1
+ export { createTransportCallLog } from '@onekeyfe/hd-transport';