@onekeyfe/hd-transport-react-native 1.2.6-alpha.1 → 1.2.6-alpha.2

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":"bleNativeDisconnect.d.ts","sourceRoot":"","sources":["../src/bleNativeDisconnect.ts"],"names":[],"mappings":"AAiBA,eAAO,MAAM,0BAA0B,UAAW,OAAO,KAAG,OAW3D,CAAC;AAEF,eAAO,MAAM,4BAA4B,UAAW,OAAO,UAU1D,CAAC"}
1
+ {"version":3,"file":"bleNativeDisconnect.d.ts","sourceRoot":"","sources":["../src/bleNativeDisconnect.ts"],"names":[],"mappings":"AAiBA,eAAO,MAAM,0BAA0B,UAAW,OAAO,KAAG,OAU3D,CAAC;AAEF,eAAO,MAAM,4BAA4B,UAAW,OAAO,UAU1D,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,EAGL,UAAU,IAAI,aAAa,EAG5B,MAAM,sBAAsB,CAAC;AAE9B,OAAO,SAAS,EAAE,EAChB,KAAK,oBAAoB,EAIzB,KAAK,YAAY,EAKjB,KAAK,oBAAoB,EAI1B,MAAM,wBAAwB,CAAC;AA6ChC,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAI1C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACjF,OAAO,KAAK,YAAY,MAAM,QAAQ,CAAC;AACvC,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAGjE,KAAK,8BAA8B,GAAG,eAAe,GAAG;IAMtD,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B,CAAC;AAkBF,KAAK,4BAA4B,GAAG,WAAW,CAAC;AA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cAAc;YAmFd,iCAAiC;IAsB/C,OAAO,CAAC,8BAA8B;YAQxB,mCAAmC;IAsBjD,OAAO,CAAC,yCAAyC;YAanC,gCAAgC;IAoB9C,OAAO,CAAC,uBAAuB;IAmD/B,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS;CAGxD"}
package/dist/index.js CHANGED
@@ -308,48 +308,24 @@ const timer = process.env.NODE_ENV === 'development'
308
308
  },
309
309
  };
310
310
 
311
- const subscribeBleOn = (bleManager, ms = 2000) => new Promise((resolve, reject) => {
311
+ const subscribeBleOn = (bleManager, ms = 1000) => new Promise((resolve, reject) => {
312
312
  let done = false;
313
- let pendingError;
314
- let cancelTimeout = () => undefined;
315
- let removeSubscription = () => undefined;
316
- const finish = (error) => {
317
- if (done)
318
- return;
319
- done = true;
320
- cancelTimeout();
321
- removeSubscription();
322
- if (error)
323
- reject(error);
324
- else
325
- resolve();
326
- };
327
313
  const subscription = bleManager.onStateChange(state => {
328
314
  if (state === 'PoweredOn') {
329
- finish();
330
- }
331
- else if (state === 'PoweredOff') {
332
- pendingError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff);
333
- }
334
- else if (state === 'Unsupported') {
335
- pendingError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleUnsupported);
336
- }
337
- else if (state === 'Unauthorized') {
338
- pendingError = hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
339
- }
340
- else {
341
- pendingError = undefined;
315
+ if (done)
316
+ return;
317
+ clearTimeout();
318
+ done = true;
319
+ subscription.remove();
320
+ resolve();
342
321
  }
343
322
  }, true);
344
- removeSubscription = () => subscription.remove();
345
- if (done) {
346
- removeSubscription();
347
- }
348
- else {
349
- cancelTimeout = timer.timeout(() => {
350
- finish(pendingError !== null && pendingError !== void 0 ? pendingError : hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleScanError));
351
- }, ms);
352
- }
323
+ const clearTimeout = timer.timeout(() => {
324
+ if (done)
325
+ return;
326
+ subscription.remove();
327
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError));
328
+ }, ms);
353
329
  });
354
330
 
355
331
  const HCI_PIN_OR_KEY_MISSING = 0x06;
@@ -586,8 +562,7 @@ const isNativeBleDisconnectError = (error) => {
586
562
  }
587
563
  const nativeError = error;
588
564
  return (nativeError.errorCode === BLE_PLX_DEVICE_DISCONNECTED ||
589
- nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED ||
590
- nativeErrorText$1(nativeError).toLowerCase().includes('was disconnected'));
565
+ nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED);
591
566
  };
592
567
  const toBleDisconnectHardwareError = (error) => {
593
568
  if ((error === null || error === void 0 ? void 0 : error.errorCode) === hdShared.HardwareErrorCode.BleDeviceDisconnected) {
@@ -902,7 +877,7 @@ const resolveNegotiatedMtu = (device, cancelTransaction) => shouldRefreshNegotia
902
877
  ? requestNegotiatedMtu(device, 'connected', 0, cancelTransaction)
903
878
  : Promise.resolve({ device, timedOut: false });
904
879
  function remapError(error) {
905
- var _a, _b;
880
+ var _a;
906
881
  if (error instanceof reactNativeBlePlx.BleError) {
907
882
  if (isNativeBleStaleBondError(error)) {
908
883
  throw toBleStaleBondHardwareError(error);
@@ -911,13 +886,12 @@ function remapError(error) {
911
886
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceBondError);
912
887
  }
913
888
  }
914
- if (isNativeBleDisconnectError(error)) {
915
- throw toBleDisconnectHardwareError(error);
916
- }
917
- 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'))) {
918
892
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
919
893
  }
920
- 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);
921
895
  }
922
896
  class ReactNativeBleTransport {
923
897
  constructor(options) {
@@ -1216,6 +1190,7 @@ class ReactNativeBleTransport {
1216
1190
  const scanStartedAt = Date.now();
1217
1191
  let firstDeviceMs;
1218
1192
  const blePlxManager = yield this.getPlxManager();
1193
+ yield subscribeBleOn(blePlxManager);
1219
1194
  if (reactNative.Platform.OS === 'android' && reactNative.Platform.Version >= 31) {
1220
1195
  Log === null || Log === void 0 ? void 0 : Log.debug('requesting permissions, please wait...');
1221
1196
  const resultConnect = yield reactNative.PermissionsAndroid.requestMultiple([
@@ -1228,7 +1203,6 @@ class ReactNativeBleTransport {
1228
1203
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError);
1229
1204
  }
1230
1205
  }
1231
- yield subscribeBleOn(blePlxManager);
1232
1206
  if (this.stopped)
1233
1207
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
1234
1208
  return new Promise((resolve, reject) => {
@@ -1269,14 +1243,11 @@ class ReactNativeBleTransport {
1269
1243
  var _a, _b;
1270
1244
  if (error) {
1271
1245
  Log === null || Log === void 0 ? void 0 : Log.debug('ble scan error: ', error);
1272
- if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff) {
1273
- finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePoweredOff));
1274
- }
1275
- else if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothUnsupported) {
1276
- 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));
1277
1248
  }
1278
1249
  else if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothUnauthorized) {
1279
- finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError));
1250
+ finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationError));
1280
1251
  }
1281
1252
  else if (error.errorCode === reactNativeBlePlx.BleErrorCode.LocationServicesDisabled) {
1282
1253
  finishScan(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationServicesDisabled));
@@ -2526,15 +2497,6 @@ class ReactNativeBleTransport {
2526
2497
  throw this.createWedgedBleSetupError();
2527
2498
  }
2528
2499
  }
2529
- if (isNativeBleStaleBondError(error) || hdShared.isBleStaleBondHardwareError(error)) {
2530
- throw toBleStaleBondHardwareError(error);
2531
- }
2532
- if (isNativeBleDisconnectError(error)) {
2533
- if (reactNative.Platform.OS === 'ios' && isIosPeerTerminationError(error)) {
2534
- throw error;
2535
- }
2536
- throw toBleDisconnectHardwareError(error);
2537
- }
2538
2500
  if (reactNative.Platform.OS === 'android' && isMissingGattShapeError(error)) {
2539
2501
  this.androidGattCacheRefreshes.add(uuid);
2540
2502
  }
@@ -1 +1 @@
1
- {"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAsC9E,CAAC"}
1
+ {"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAE7C,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAmB9E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "1.2.6-alpha.1",
3
+ "version": "1.2.6-alpha.2",
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.6-alpha.1",
24
- "@onekeyfe/hd-shared": "1.2.6-alpha.1",
25
- "@onekeyfe/hd-transport": "1.2.6-alpha.1",
23
+ "@onekeyfe/hd-core": "1.2.6-alpha.2",
24
+ "@onekeyfe/hd-shared": "1.2.6-alpha.2",
25
+ "@onekeyfe/hd-transport": "1.2.6-alpha.2",
26
26
  "@onekeyfe/react-native-ble-utils": "0.1.9",
27
27
  "react-native-ble-plx": "3.5.1"
28
28
  },
29
- "gitHead": "32f41a09724c316ccefe39daae56c22f539d76bd"
29
+ "gitHead": "c01014c750c5c0bc4f6bff2e07b2fee985893f59"
30
30
  }
@@ -30,25 +30,4 @@ describe('native BLE disconnect mapping', () => {
30
30
  })
31
31
  ).toBe(false);
32
32
  });
33
-
34
- test('recognizes the unstructured ble-plx disconnect observed during GATT discovery', () => {
35
- const nativeError = {
36
- errorCode: 0,
37
- message: 'BleError: Device stalled-connect-device was disconnected',
38
- };
39
-
40
- expect(isNativeBleDisconnectError(nativeError)).toBe(true);
41
- expect(toBleDisconnectHardwareError(nativeError)).toMatchObject({
42
- errorCode: HardwareErrorCode.BleDeviceDisconnected,
43
- });
44
- });
45
-
46
- test('does not infer a disconnect from an unrelated discovery failure', () => {
47
- expect(
48
- isNativeBleDisconnectError({
49
- errorCode: 0,
50
- message: 'Known OneKey service UUID not found',
51
- })
52
- ).toBe(false);
53
- });
54
33
  });
@@ -623,44 +623,6 @@ describe('BLE connect timeout', () => {
623
623
  expect(bleManager.cancelDeviceConnection).toHaveBeenCalledWith(UUID);
624
624
  });
625
625
 
626
- test.each([
627
- {
628
- nativeFields: { errorCode: BleErrorCode.DeviceDisconnected, iosErrorCode: 14 },
629
- reason: 'Peer removed pairing information',
630
- errorCode: HardwareErrorCode.BlePeerRemovedPairingInformation,
631
- },
632
- {
633
- nativeFields: { errorCode: 0, attErrorCode: 5 },
634
- reason: 'GATT_INSUF_AUTHENTICATION',
635
- errorCode: HardwareErrorCode.BleDeviceBondError,
636
- },
637
- ])(
638
- 'maps a stale bond during GATT discovery to canonical error $errorCode',
639
- async ({ nativeFields, reason, errorCode }) => {
640
- const { transport, device } = createHarness(() => Promise.resolve());
641
- const nativeError = Object.assign(new Error(reason), nativeFields, { reason });
642
- device.discoverAllServicesAndCharacteristics.mockRejectedValueOnce(nativeError);
643
-
644
- await expect(
645
- (transport as any).resolveCharacteristicsWithTimeout(UUID, device)
646
- ).rejects.toMatchObject({ errorCode });
647
- }
648
- );
649
-
650
- test('normalizes an unstructured GATT disconnect instead of leaking an unknown error', async () => {
651
- const { transport, device } = createHarness(() => Promise.resolve());
652
- const nativeError = Object.assign(new Error(`BleError: Device ${UUID} was disconnected`), {
653
- errorCode: 0,
654
- });
655
- device.discoverAllServicesAndCharacteristics.mockRejectedValueOnce(nativeError);
656
-
657
- await expect(
658
- (transport as any).resolveCharacteristicsWithTimeout(UUID, device)
659
- ).rejects.toMatchObject({
660
- errorCode: HardwareErrorCode.BleDeviceDisconnected,
661
- });
662
- });
663
-
664
626
  test('a successful GATT retry clears the timeout budget before an abandoned call settles', async () => {
665
627
  const { transport, device, bleManager } = createHarness(() => Promise.resolve());
666
628
  let resolveAbandonedDiscovery: (() => void) | undefined;
@@ -1,22 +1,12 @@
1
1
  import { EventEmitter } from 'events';
2
- import { PermissionsAndroid, Platform } from 'react-native';
3
- import { BleErrorCode } from 'react-native-ble-plx';
4
- import { HardwareErrorCode } from '@onekeyfe/hd-shared';
5
2
 
6
3
  import { getConnectedDeviceIds } from '../BleManager';
7
4
  import ReactNativeBleTransport from '../index';
8
- import { subscribeBleOn } from '../subscribeBleOn';
9
5
 
10
6
  jest.mock(
11
7
  'react-native',
12
8
  () => ({
13
- PermissionsAndroid: {
14
- PERMISSIONS: {
15
- BLUETOOTH_CONNECT: 'android.permission.BLUETOOTH_CONNECT',
16
- BLUETOOTH_SCAN: 'android.permission.BLUETOOTH_SCAN',
17
- },
18
- requestMultiple: jest.fn(),
19
- },
9
+ PermissionsAndroid: {},
20
10
  Platform: { OS: 'ios' },
21
11
  }),
22
12
  { virtual: true }
@@ -24,14 +14,7 @@ jest.mock(
24
14
 
25
15
  jest.mock('react-native-ble-plx', () => ({
26
16
  BleError: class BleError extends Error {},
27
- BleErrorCode: {
28
- BluetoothUnsupported: 100,
29
- BluetoothUnauthorized: 101,
30
- BluetoothPoweredOff: 102,
31
- BluetoothInUnknownState: 103,
32
- ScanStartFailed: 600,
33
- LocationServicesDisabled: 601,
34
- },
17
+ BleErrorCode: {},
35
18
  BleManager: jest.fn(),
36
19
  ScanMode: { LowLatency: 2 },
37
20
  }));
@@ -63,55 +46,6 @@ jest.mock('../subscribeBleOn', () => ({
63
46
 
64
47
  const ONEKEY_SERVICE_UUID = '00000001-0000-1000-8000-00805f9b34fb';
65
48
 
66
- describe('ReactNativeBleTransport scan error mapping', () => {
67
- beforeEach(() => {
68
- jest.clearAllMocks();
69
- });
70
-
71
- afterEach(() => {
72
- Object.assign(Platform, { OS: 'ios', Version: undefined });
73
- });
74
-
75
- test('checks Android Bluetooth permissions before waiting for adapter state', async () => {
76
- Object.assign(Platform, { OS: 'android', Version: 31 });
77
- jest.mocked(PermissionsAndroid).requestMultiple.mockResolvedValueOnce({
78
- [PermissionsAndroid.PERMISSIONS.BLUETOOTH_CONNECT]: 'denied',
79
- [PermissionsAndroid.PERMISSIONS.BLUETOOTH_SCAN]: 'denied',
80
- });
81
- const transport = new ReactNativeBleTransport({});
82
- transport.blePlxManager = {} as never;
83
- transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
84
-
85
- await expect(transport.enumerate()).rejects.toMatchObject({
86
- errorCode: HardwareErrorCode.BlePermissionError,
87
- });
88
- expect(subscribeBleOn).not.toHaveBeenCalled();
89
- });
90
-
91
- test.each([
92
- [BleErrorCode.BluetoothPoweredOff, HardwareErrorCode.BlePoweredOff],
93
- [BleErrorCode.BluetoothUnsupported, HardwareErrorCode.BleUnsupported],
94
- [BleErrorCode.BluetoothInUnknownState, HardwareErrorCode.BleScanError],
95
- [BleErrorCode.BluetoothUnauthorized, HardwareErrorCode.BlePermissionError],
96
- ])('maps native BLE error %s to hardware error %s', async (nativeCode, errorCode) => {
97
- jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([]);
98
- const blePlxManager = {
99
- startDeviceScan: jest.fn((_serviceUUIDs, _options, listener) => {
100
- queueMicrotask(() => {
101
- listener({ errorCode: nativeCode, reason: 'native scan failure' }, null);
102
- });
103
- }),
104
- stopDeviceScan: jest.fn(() => Promise.resolve()),
105
- };
106
- const transport = new ReactNativeBleTransport({ scanTimeout: 10_000 });
107
- transport.blePlxManager = blePlxManager as never;
108
- transport.init({ debug: jest.fn(), error: jest.fn() }, new EventEmitter());
109
-
110
- await expect(transport.enumerate()).rejects.toMatchObject({ errorCode });
111
- expect(blePlxManager.stopDeviceScan).toHaveBeenCalledTimes(1);
112
- });
113
- });
114
-
115
49
  describe('ReactNativeBleTransport iOS discovery', () => {
116
50
  test('keeps a bonded Pro2 communication peripheral after Find My changes its name', async () => {
117
51
  jest.mocked(getConnectedDeviceIds).mockResolvedValueOnce([
@@ -215,9 +215,7 @@ describe('iOS link ended by the device right after connecting', () => {
215
215
  .mockImplementation(() => Promise.reject(peerEndedLink()));
216
216
 
217
217
  await acquire(transport);
218
- await expect(acquire(transport)).resolves.toMatchObject({
219
- code: HardwareErrorCode.BleDeviceDisconnected,
220
- });
218
+ await expect(acquire(transport)).resolves.toEqual({ native: 6 });
221
219
  await expect(acquire(transport)).resolves.toEqual({ native: 7 });
222
220
  });
223
221
 
@@ -23,8 +23,7 @@ export const isNativeBleDisconnectError = (error: unknown): boolean => {
23
23
  const nativeError = error as NativeBleErrorFields;
24
24
  return (
25
25
  nativeError.errorCode === BLE_PLX_DEVICE_DISCONNECTED ||
26
- nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED ||
27
- nativeErrorText(nativeError).toLowerCase().includes('was disconnected')
26
+ nativeError.iosErrorCode === IOS_PERIPHERAL_DISCONNECTED
28
27
  );
29
28
  };
30
29
 
package/src/index.ts CHANGED
@@ -59,7 +59,7 @@ import {
59
59
  stopBleEncryptionTracking,
60
60
  waitForAndroidLinkEncryption,
61
61
  } from './bleEncryption';
62
- import { IosPeerTerminationTracker, isIosPeerTerminationError } from './bleIosStaleBond';
62
+ import { IosPeerTerminationTracker } from './bleIosStaleBond';
63
63
  import { isNativeBleDisconnectError, toBleDisconnectHardwareError } from './bleNativeDisconnect';
64
64
  import {
65
65
  isBleStaleBondHardwareError,
@@ -189,8 +189,8 @@ const shouldRethrowProtocolProbeError = (error: unknown): boolean => {
189
189
  const code = (error as { errorCode?: unknown })?.errorCode;
190
190
  // Bonding and GATT failures are not evidence of a protocol mismatch. Preserve
191
191
  // them instead of probing another protocol on an unusable connection.
192
- // Native PLX disconnects (errorCode 201 / iOS 7, or the unstructured
193
- // "was disconnected" fallback) must match before Protocol V2 probing.
192
+ // Native PLX disconnects (errorCode 201 / iOS 7) must match before they are
193
+ // mapped: Protocol V2 writes rethrow them unchanged unless normalized first.
194
194
  return (
195
195
  isBleStaleBondHardwareError(error) ||
196
196
  isNativeBleDisconnectError(error) ||
@@ -506,11 +506,11 @@ function remapError(error: IOBleErrorRemap) {
506
506
  }
507
507
  }
508
508
 
509
- if (isNativeBleDisconnectError(error)) {
510
- throw toBleDisconnectHardwareError(error);
511
- }
512
-
513
- if (error instanceof Error && error.message?.includes('not found')) {
509
+ if (
510
+ error instanceof Error &&
511
+ error.message &&
512
+ (error.message.includes('was disconnected') || error.message.includes('not found'))
513
+ ) {
514
514
  throw ERRORS.TypedError(HardwareErrorCode.BleDeviceDisconnected);
515
515
  }
516
516
 
@@ -908,6 +908,7 @@ export default class ReactNativeBleTransport {
908
908
  const scanStartedAt = Date.now();
909
909
  let firstDeviceMs: number | undefined;
910
910
  const blePlxManager = await this.getPlxManager();
911
+ await subscribeBleOn(blePlxManager);
911
912
  if (Platform.OS === 'android' && Platform.Version >= 31) {
912
913
  Log?.debug('requesting permissions, please wait...');
913
914
 
@@ -924,7 +925,6 @@ export default class ReactNativeBleTransport {
924
925
  throw ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
925
926
  }
926
927
  }
927
- await subscribeBleOn(blePlxManager);
928
928
 
929
929
  if (this.stopped) throw ERRORS.TypedError(HardwareErrorCode.BleDeviceDisconnected);
930
930
  return new Promise<IOneKeyDevice[]>((resolve, reject) => {
@@ -967,12 +967,14 @@ export default class ReactNativeBleTransport {
967
967
  (error, device) => {
968
968
  if (error) {
969
969
  Log?.debug('ble scan error: ', error);
970
- if (error.errorCode === BleErrorCode.BluetoothPoweredOff) {
971
- finishScan(ERRORS.TypedError(HardwareErrorCode.BlePoweredOff));
972
- } else if (error.errorCode === BleErrorCode.BluetoothUnsupported) {
973
- finishScan(ERRORS.TypedError(HardwareErrorCode.BleUnsupported));
974
- } else if (error.errorCode === BleErrorCode.BluetoothUnauthorized) {
970
+ if (
971
+ [BleErrorCode.BluetoothPoweredOff, BleErrorCode.BluetoothInUnknownState].includes(
972
+ error.errorCode
973
+ )
974
+ ) {
975
975
  finishScan(ERRORS.TypedError(HardwareErrorCode.BlePermissionError));
976
+ } else if (error.errorCode === BleErrorCode.BluetoothUnauthorized) {
977
+ finishScan(ERRORS.TypedError(HardwareErrorCode.BleLocationError));
976
978
  } else if (error.errorCode === BleErrorCode.LocationServicesDisabled) {
977
979
  finishScan(ERRORS.TypedError(HardwareErrorCode.BleLocationServicesDisabled));
978
980
  } else if (error.errorCode === BleErrorCode.ScanStartFailed) {
@@ -2550,15 +2552,6 @@ export default class ReactNativeBleTransport {
2550
2552
  throw this.createWedgedBleSetupError();
2551
2553
  }
2552
2554
  }
2553
- if (isNativeBleStaleBondError(error) || isBleStaleBondHardwareError(error)) {
2554
- throw toBleStaleBondHardwareError(error);
2555
- }
2556
- if (isNativeBleDisconnectError(error)) {
2557
- if (Platform.OS === 'ios' && isIosPeerTerminationError(error)) {
2558
- throw error;
2559
- }
2560
- throw toBleDisconnectHardwareError(error);
2561
- }
2562
2555
  if (Platform.OS === 'android' && isMissingGattShapeError(error)) {
2563
2556
  this.androidGattCacheRefreshes.add(uuid);
2564
2557
  }
@@ -4,42 +4,23 @@ import timer from './utils/timer';
4
4
 
5
5
  import type { BlePlxManager } from './types';
6
6
 
7
- export const subscribeBleOn = (bleManager: BlePlxManager, ms = 2000): Promise<void> =>
7
+ export const subscribeBleOn = (bleManager: BlePlxManager, ms = 1000): Promise<void> =>
8
8
  new Promise((resolve, reject) => {
9
9
  let done = false;
10
- let pendingError: Error | undefined;
11
- let cancelTimeout: () => void = () => undefined;
12
- let removeSubscription: () => void = () => undefined;
13
-
14
- const finish = (error?: Error) => {
15
- if (done) return;
16
- done = true;
17
- cancelTimeout();
18
- removeSubscription();
19
- if (error) reject(error);
20
- else resolve();
21
- };
22
10
 
23
11
  const subscription = bleManager.onStateChange(state => {
24
12
  if (state === 'PoweredOn') {
25
- finish();
26
- } else if (state === 'PoweredOff') {
27
- pendingError = ERRORS.TypedError(HardwareErrorCode.BlePoweredOff);
28
- } else if (state === 'Unsupported') {
29
- pendingError = ERRORS.TypedError(HardwareErrorCode.BleUnsupported);
30
- } else if (state === 'Unauthorized') {
31
- pendingError = ERRORS.TypedError(HardwareErrorCode.BlePermissionError);
32
- } else {
33
- pendingError = undefined;
13
+ if (done) return;
14
+ clearTimeout();
15
+ done = true;
16
+ subscription.remove();
17
+ resolve();
34
18
  }
35
19
  }, true);
36
- removeSubscription = () => subscription.remove();
37
20
 
38
- if (done) {
39
- removeSubscription();
40
- } else {
41
- cancelTimeout = timer.timeout(() => {
42
- finish(pendingError ?? ERRORS.TypedError(HardwareErrorCode.BleScanError));
43
- }, ms);
44
- }
21
+ const clearTimeout = timer.timeout(() => {
22
+ if (done) return;
23
+ subscription.remove();
24
+ reject(ERRORS.TypedError(HardwareErrorCode.BlePermissionError));
25
+ }, ms);
45
26
  });
@@ -1,114 +0,0 @@
1
- import { HardwareErrorCode } from '@onekeyfe/hd-shared';
2
-
3
- import { subscribeBleOn } from '../subscribeBleOn';
4
-
5
- import type { BlePlxManager } from '../types';
6
-
7
- type BleState = Parameters<Parameters<BlePlxManager['onStateChange']>[0]>[0];
8
-
9
- const createBleManager = () => {
10
- let stateListener: ((state: BleState) => void) | undefined;
11
- const remove = jest.fn();
12
- const bleManager = {
13
- onStateChange: jest.fn(listener => {
14
- stateListener = listener;
15
- return { remove };
16
- }),
17
- } as unknown as BlePlxManager;
18
-
19
- return {
20
- bleManager,
21
- emitState: (state: BleState) => stateListener?.(state),
22
- remove,
23
- };
24
- };
25
-
26
- describe('subscribeBleOn', () => {
27
- beforeAll(() => {
28
- jest.useFakeTimers({ doNotFake: ['performance'] });
29
- });
30
-
31
- afterAll(() => {
32
- jest.useRealTimers();
33
- });
34
-
35
- afterEach(() => {
36
- jest.clearAllTimers();
37
- });
38
-
39
- test('resolves when Bluetooth is powered on', async () => {
40
- const { bleManager, emitState, remove } = createBleManager();
41
- const result = subscribeBleOn(bleManager);
42
-
43
- emitState('PoweredOn');
44
-
45
- await expect(result).resolves.toBeUndefined();
46
- expect(remove).toHaveBeenCalledTimes(1);
47
- expect(jest.getTimerCount()).toBe(0);
48
- });
49
-
50
- test('cleans up when the current state is emitted synchronously', async () => {
51
- const remove = jest.fn();
52
- const bleManager = {
53
- onStateChange: jest.fn(listener => {
54
- listener('PoweredOn');
55
- return { remove };
56
- }),
57
- } as unknown as BlePlxManager;
58
-
59
- await expect(subscribeBleOn(bleManager)).resolves.toBeUndefined();
60
- expect(remove).toHaveBeenCalledTimes(1);
61
- expect(jest.getTimerCount()).toBe(0);
62
- });
63
-
64
- test.each([
65
- ['PoweredOff', HardwareErrorCode.BlePoweredOff],
66
- ['Unsupported', HardwareErrorCode.BleUnsupported],
67
- ['Unauthorized', HardwareErrorCode.BlePermissionError],
68
- ] as const)('maps %s to hardware error %s', async (state, errorCode) => {
69
- const { bleManager, emitState, remove } = createBleManager();
70
- const result = subscribeBleOn(bleManager, 2000);
71
- const rejection = expect(result).rejects.toMatchObject({ errorCode });
72
-
73
- emitState(state);
74
- jest.advanceTimersByTime(1999);
75
- expect(remove).not.toHaveBeenCalled();
76
- jest.advanceTimersByTime(1);
77
-
78
- await rejection;
79
- expect(remove).toHaveBeenCalledTimes(1);
80
- expect(jest.getTimerCount()).toBe(0);
81
- });
82
-
83
- test('resolves if Bluetooth powers on during the readiness window', async () => {
84
- const { bleManager, emitState, remove } = createBleManager();
85
- const result = subscribeBleOn(bleManager);
86
-
87
- emitState('PoweredOff');
88
- jest.advanceTimersByTime(1999);
89
- expect(remove).not.toHaveBeenCalled();
90
- emitState('PoweredOn');
91
-
92
- await expect(result).resolves.toBeUndefined();
93
- expect(remove).toHaveBeenCalledTimes(1);
94
- expect(jest.getTimerCount()).toBe(0);
95
- });
96
-
97
- test.each(['Unknown', 'Resetting'] as const)(
98
- 'reports %s as a scan error if no stable state arrives before timeout',
99
- async state => {
100
- const { bleManager, emitState, remove } = createBleManager();
101
- const result = subscribeBleOn(bleManager, 1000);
102
- const rejection = expect(result).rejects.toMatchObject({
103
- errorCode: HardwareErrorCode.BleScanError,
104
- });
105
-
106
- emitState('PoweredOff');
107
- emitState(state);
108
- jest.advanceTimersByTime(1000);
109
-
110
- await rejection;
111
- expect(remove).toHaveBeenCalledTimes(1);
112
- }
113
- );
114
- });