@onekeyfe/hd-transport-react-native 1.0.25 → 1.0.26

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,21 +1,9 @@
1
- export declare const initializeBleManager: () => void;
2
- export declare const getConnectedDeviceIds: (serviceUuids: string[]) => Promise<{
3
- isConnectable?: boolean | undefined;
4
- localName?: string | undefined;
5
- manufacturerData?: any;
6
- serviceUUIDs?: string[] | undefined;
7
- txPowerLevel?: number | undefined;
8
- id: string;
9
- name: string | undefined;
10
- }[]>;
11
- export declare const getBondedDevices: () => Promise<{
12
- isConnectable?: boolean | undefined;
13
- localName?: string | undefined;
14
- manufacturerData?: any;
15
- serviceUUIDs?: string[] | undefined;
16
- txPowerLevel?: number | undefined;
17
- id: string;
18
- name: string | undefined;
19
- }[]>;
20
- export declare const refreshCache: (peripheralId: string) => Promise<void>;
1
+ import { Peripheral } from '@onekeyfe/react-native-ble-utils';
2
+ export declare const getConnectedDeviceIds: (serviceUuids: string[]) => Promise<Peripheral[]>;
3
+ export declare const getBondedDevices: () => Promise<Peripheral[]>;
4
+ export declare const pairDevice: (macAddress: string) => Promise<{
5
+ bonded: boolean;
6
+ bonding: boolean;
7
+ }>;
8
+ export declare const onDeviceBondState: (bleMacAddress: string) => Promise<Peripheral | undefined>;
21
9
  //# sourceMappingURL=BleManager.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BleManager.d.ts","sourceRoot":"","sources":["../src/BleManager.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,oBAAoB,YAEhC,CAAC;AAOF,eAAO,MAAM,qBAAqB,iBAAwB,MAAM,EAAE;;;;;;;;IAMjE,CAAC;AAEF,eAAO,MAAM,gBAAgB;;;;;;;;IAM5B,CAAC;AAEF,eAAO,MAAM,YAAY,iBAAwB,MAAM,kBAStD,CAAC"}
1
+ {"version":3,"file":"BleManager.d.ts","sourceRoot":"","sources":["../src/BleManager.ts"],"names":[],"mappings":"AAAA,OAAiB,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAWxE,eAAO,MAAM,qBAAqB,iBAAkB,MAAM,EAAE,0BACZ,CAAC;AAEjD,eAAO,MAAM,gBAAgB,6BAAwC,CAAC;AAEtE,eAAO,MAAM,UAAU,eAAgB,MAAM;;;EAAoC,CAAC;AAElF,eAAO,MAAM,iBAAiB,kBAAmB,MAAM,KAAG,QAAQ,UAAU,GAAG,SAAS,CAmCpF,CAAC"}
@@ -1,4 +1,4 @@
1
- import { Device, Characteristic } from '@onekeyfe/react-native-ble-plx';
1
+ import { Device, Characteristic } from 'react-native-ble-plx';
2
2
  export default class BleTransport {
3
3
  id: string;
4
4
  device: Device;
@@ -1 +1 @@
1
- {"version":3,"file":"BleTransport.d.ts","sourceRoot":"","sources":["../src/BleTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAgB,MAAM,gCAAgC,CAAC;AAMtF,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,EAAE,EAAE,MAAM,CAAC;IAEX,MAAM,EAAE,MAAM,CAAC;IAEf,OAAO,SAAM;IAEb,mBAAmB,EAAE,cAAc,CAAC;IAEpC,oBAAoB,EAAE,cAAc,CAAC;IAErC,kBAAkB,CAAC,EAAE,MAAM,IAAI,CAAC;IAEhC,MAAM,CAAC,WAAW,SAAK;IAEvB,MAAM,CAAC,WAAW,SAAQ;gBAGxB,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,cAAc,EACnC,oBAAoB,EAAE,cAAc;IAehC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,SAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;CA2BzF"}
1
+ {"version":3,"file":"BleTransport.d.ts","sourceRoot":"","sources":["../src/BleTransport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,cAAc,EAAgB,MAAM,sBAAsB,CAAC;AAM5E,MAAM,CAAC,OAAO,OAAO,YAAY;IAC/B,EAAE,EAAE,MAAM,CAAC;IAEX,MAAM,EAAE,MAAM,CAAC;IAEf,OAAO,SAAM;IAEb,mBAAmB,EAAE,cAAc,CAAC;IAEpC,oBAAoB,EAAE,cAAc,CAAC;IAErC,kBAAkB,CAAC,EAAE,MAAM,IAAI,CAAC;IAEhC,MAAM,CAAC,WAAW,SAAK;IAEvB,MAAM,CAAC,WAAW,SAAQ;gBAGxB,MAAM,EAAE,MAAM,EACd,mBAAmB,EAAE,cAAc,EACnC,oBAAoB,EAAE,cAAc;IAehC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,SAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;CA2BzF"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as transport from '@onekeyfe/hd-transport';
2
2
  import transport__default from '@onekeyfe/hd-transport';
3
- import { BleManager, Device, Characteristic } from '@onekeyfe/react-native-ble-plx';
3
+ import { BleManager, Device, Characteristic } from 'react-native-ble-plx';
4
4
  import { Deferred } from '@onekeyfe/hd-shared';
5
5
  import EventEmitter from 'events';
6
6
 
@@ -15,6 +15,7 @@ type BleAcquireInput = {
15
15
  declare class ReactNativeBleTransport {
16
16
  blePlxManager: BleManager | undefined;
17
17
  _messages: ReturnType<typeof transport__default.parseConfigure> | undefined;
18
+ name: string;
18
19
  configured: boolean;
19
20
  stopped: boolean;
20
21
  scanTimeout: number;
@@ -32,6 +33,7 @@ declare class ReactNativeBleTransport {
32
33
  }>;
33
34
  _monitorCharacteristic(characteristic: Characteristic): () => void;
34
35
  release(uuid: string): Promise<boolean>;
36
+ post(session: string, name: string, data: Record<string, unknown>): Promise<void>;
35
37
  call(uuid: string, name: string, data: Record<string, unknown>): Promise<transport.MessageFromOneKey>;
36
38
  stop(): void;
37
39
  cancel(): void;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAEA,OAAO,EACL,UAAU,IAAI,aAAa,EAC3B,M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buffer = require('buffer');
5
- var reactNativeBlePlx = require('@onekeyfe/react-native-ble-plx');
5
+ var reactNativeBlePlx = require('react-native-ble-plx');
6
6
  var ByteBuffer = require('bytebuffer');
7
7
  var transport = require('@onekeyfe/hd-transport');
8
8
  var hdShared = require('@onekeyfe/hd-shared');
9
- var BleManager = require('react-native-ble-manager');
9
+ var BleUtils = require('@onekeyfe/react-native-ble-utils');
10
10
  var hdCore = require('@onekeyfe/hd-core');
11
11
 
12
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
13
 
14
14
  var ByteBuffer__default = /*#__PURE__*/_interopDefaultLegacy(ByteBuffer);
15
15
  var transport__default = /*#__PURE__*/_interopDefaultLegacy(transport);
16
- var BleManager__default = /*#__PURE__*/_interopDefaultLegacy(BleManager);
16
+ var BleUtils__default = /*#__PURE__*/_interopDefaultLegacy(BleUtils);
17
17
 
18
18
  /******************************************************************************
19
19
  Copyright (c) Microsoft Corporation.
@@ -45,21 +45,41 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
45
45
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
46
46
  };
47
47
 
48
- const initializeBleManager = () => {
49
- BleManager__default["default"].start({ showAlert: false });
50
- };
51
- const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0, function* () {
52
- const connectedPeripherals = yield BleManager__default["default"].getConnectedPeripherals(serviceUuids);
53
- return connectedPeripherals.map(peripheral => {
54
- const { id, name, advertising = {} } = peripheral;
55
- return Object.assign({ id, name }, advertising);
56
- });
57
- });
58
- const getBondedDevices = () => __awaiter(void 0, void 0, void 0, function* () {
59
- const peripherals = yield BleManager__default["default"].getBondedPeripherals();
60
- return peripherals.map(peripheral => {
61
- const { id, name, advertising = {} } = peripheral;
62
- return Object.assign({ id, name }, advertising);
48
+ const Logger = hdCore.getLogger(hdCore.LoggerNames.HdBleTransport);
49
+ const getConnectedDeviceIds = (serviceUuids) => BleUtils__default["default"].getConnectedPeripherals(serviceUuids);
50
+ const pairDevice = (macAddress) => BleUtils__default["default"].pairDevice(macAddress);
51
+ const onDeviceBondState = (bleMacAddress) => new Promise((resolve, reject) => {
52
+ let timeout;
53
+ const cleanup = (cleanupListener) => {
54
+ if (timeout) {
55
+ clearTimeout(timeout);
56
+ }
57
+ if (cleanupListener)
58
+ cleanupListener();
59
+ };
60
+ const cleanupListener = BleUtils__default["default"].onDeviceBondState(peripheral => {
61
+ var _a;
62
+ if (((_a = peripheral.id) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== bleMacAddress.toLowerCase()) {
63
+ return;
64
+ }
65
+ const { bondState } = peripheral;
66
+ if (bondState.preState === 'BOND_NONE' && bondState.state === 'BOND_BONDING') {
67
+ timeout = setTimeout(() => {
68
+ cleanup(cleanupListener);
69
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded'));
70
+ }, 60 * 1000);
71
+ }
72
+ const hasBonded = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_BONDED';
73
+ const hasCanceled = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_NONE';
74
+ Logger.debug('onDeviceBondState bondState:', bondState);
75
+ if (hasBonded) {
76
+ cleanup(cleanupListener);
77
+ resolve(peripheral);
78
+ }
79
+ else if (hasCanceled) {
80
+ cleanup(cleanupListener);
81
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceBondedCanceled, 'bonding canceled'));
82
+ }
63
83
  });
64
84
  });
65
85
 
@@ -217,9 +237,28 @@ const tryToGetConfiguration = (device) => {
217
237
  return null;
218
238
  return infos;
219
239
  };
240
+ function remapError(error) {
241
+ var _a;
242
+ if (error instanceof reactNativeBlePlx.BleError) {
243
+ if (error.iosErrorCode === reactNativeBlePlx.BleATTErrorCode.UnlikelyError ||
244
+ error.reason === 'Peer removed pairing information') {
245
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePeerRemovedPairingInformation);
246
+ }
247
+ if ((error === null || error === void 0 ? void 0 : error.attErrorCode) === 22) {
248
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceBondError);
249
+ }
250
+ }
251
+ if (error instanceof Error &&
252
+ error.message &&
253
+ (error.message.includes('was disconnected') || error.message.includes('not found'))) {
254
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceDisconnected);
255
+ }
256
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_a = error.reason) !== null && _a !== void 0 ? _a : error);
257
+ }
220
258
  class ReactNativeBleTransport {
221
259
  constructor(options) {
222
260
  var _a;
261
+ this.name = 'ReactNativeBleTransport';
223
262
  this.configured = false;
224
263
  this.stopped = false;
225
264
  this.scanTimeout = 3000;
@@ -241,7 +280,6 @@ class ReactNativeBleTransport {
241
280
  if (this.blePlxManager)
242
281
  return Promise.resolve(this.blePlxManager);
243
282
  this.blePlxManager = new reactNativeBlePlx.BleManager();
244
- initializeBleManager();
245
283
  return Promise.resolve(this.blePlxManager);
246
284
  }
247
285
  enumerate() {
@@ -321,7 +359,7 @@ class ReactNativeBleTransport {
321
359
  });
322
360
  }
323
361
  acquire(input) {
324
- var _a, _b, _c;
362
+ var _a;
325
363
  return __awaiter(this, void 0, void 0, function* () {
326
364
  const { uuid, forceCleanRunPromise } = input;
327
365
  if (!uuid) {
@@ -344,6 +382,12 @@ class ReactNativeBleTransport {
344
382
  this.Log.debug('subscribeBleOn error: ', error);
345
383
  throw error;
346
384
  }
385
+ if (reactNative.Platform.OS === 'android') {
386
+ const bondState = yield pairDevice(uuid);
387
+ if (bondState.bonding) {
388
+ yield onDeviceBondState(uuid);
389
+ }
390
+ }
347
391
  if (!device) {
348
392
  const devices = yield blePlxManager.devices([uuid]);
349
393
  [device] = devices;
@@ -372,7 +416,7 @@ class ReactNativeBleTransport {
372
416
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleAlreadyConnected);
373
417
  }
374
418
  else {
375
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_a = e.reason) !== null && _a !== void 0 ? _a : e);
419
+ remapError(e);
376
420
  }
377
421
  }
378
422
  }
@@ -403,17 +447,10 @@ class ReactNativeBleTransport {
403
447
  }
404
448
  }
405
449
  else {
406
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_b = e.reason) !== null && _b !== void 0 ? _b : e);
450
+ remapError(e);
407
451
  }
408
452
  }
409
453
  }
410
- if (reactNative.Platform.OS === 'android') {
411
- const bondedDevices = yield getBondedDevices();
412
- const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === (device === null || device === void 0 ? void 0 : device.id));
413
- if (!hasBonded) {
414
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded');
415
- }
416
- }
417
454
  yield device.discoverAllServicesAndCharacteristics();
418
455
  let infos = tryToGetConfiguration(device);
419
456
  let characteristics;
@@ -472,7 +509,7 @@ class ReactNativeBleTransport {
472
509
  const transport = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
473
510
  transport.nofitySubscription = this._monitorCharacteristic(transport.notifyCharacteristic);
474
511
  transportCache[uuid] = transport;
475
- (_c = this.emitter) === null || _c === void 0 ? void 0 : _c.emit('device-connect', {
512
+ (_a = this.emitter) === null || _a === void 0 ? void 0 : _a.emit('device-connect', {
476
513
  name: device.name,
477
514
  id: device.id,
478
515
  connectId: device.id,
@@ -571,6 +608,11 @@ class ReactNativeBleTransport {
571
608
  return Promise.resolve(true);
572
609
  });
573
610
  }
611
+ post(session, name, data) {
612
+ return __awaiter(this, void 0, void 0, function* () {
613
+ yield this.call(session, name, data);
614
+ });
615
+ }
574
616
  call(uuid, name, data) {
575
617
  return __awaiter(this, void 0, void 0, function* () {
576
618
  if (this.stopped) {
@@ -579,7 +621,9 @@ class ReactNativeBleTransport {
579
621
  if (this._messages == null) {
580
622
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
581
623
  }
582
- if (this.runPromise) {
624
+ const forceRun = name === 'Initialize' || name === 'Cancel';
625
+ this.Log.debug('transport-react-native call this.runPromise', this.runPromise);
626
+ if (this.runPromise && !forceRun) {
583
627
  throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportCallInProgress);
584
628
  }
585
629
  const transport$1 = transportCache[uuid];
@@ -681,7 +725,7 @@ class ReactNativeBleTransport {
681
725
  this.stopped = true;
682
726
  }
683
727
  cancel() {
684
- this.Log.debug('transport-react-native canceled');
728
+ this.Log.debug('transport-react-native transport cancel');
685
729
  if (this.runPromise) ;
686
730
  this.runPromise = null;
687
731
  }
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { BleManager as BlePlxManager } from '@onekeyfe/react-native-ble-plx';
1
+ export type { BleManager as BlePlxManager } from 'react-native-ble-plx';
2
2
  export type TransportOptions = {
3
3
  scanTimeout?: number;
4
4
  };
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,IAAI,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAElF,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;CAChC,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,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;CAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "1.0.25",
3
+ "version": "1.0.26",
4
4
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -19,10 +19,10 @@
19
19
  "lint:fix": "eslint . --fix"
20
20
  },
21
21
  "dependencies": {
22
- "@onekeyfe/hd-shared": "^1.0.25",
23
- "@onekeyfe/hd-transport": "^1.0.25",
24
- "@onekeyfe/react-native-ble-plx": "3.0.1",
25
- "react-native-ble-manager": "^8.1.0"
22
+ "@onekeyfe/hd-shared": "^1.0.26",
23
+ "@onekeyfe/hd-transport": "^1.0.26",
24
+ "@onekeyfe/react-native-ble-utils": "^0.1.3",
25
+ "react-native-ble-plx": "3.5.0"
26
26
  },
27
- "gitHead": "25ac0d0194328afee6da6430c72895523e576302"
27
+ "gitHead": "080a3afa4360f2bc04368567e73ed30ba82ff2d7"
28
28
  }
package/src/BleManager.ts CHANGED
@@ -1,37 +1,54 @@
1
- import BleManager from 'react-native-ble-manager';
1
+ import BleUtils, { Peripheral } from '@onekeyfe/react-native-ble-utils';
2
+ import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
3
+ import { getLogger, LoggerNames } from '@onekeyfe/hd-core';
2
4
 
3
- export const initializeBleManager = () => {
4
- BleManager.start({ showAlert: false });
5
- };
5
+ const Logger = getLogger(LoggerNames.HdBleTransport);
6
6
 
7
7
  /**
8
8
  * get the device basic info of connected devices
9
9
  * @param serviceUuids
10
10
  * @returns {Promise<[string[]]>}
11
11
  */
12
- export const getConnectedDeviceIds = async (serviceUuids: string[]) => {
13
- const connectedPeripherals = await BleManager.getConnectedPeripherals(serviceUuids);
14
- return connectedPeripherals.map(peripheral => {
15
- const { id, name, advertising = {} } = peripheral;
16
- return { id, name, ...advertising };
17
- });
18
- };
12
+ export const getConnectedDeviceIds = (serviceUuids: string[]) =>
13
+ BleUtils.getConnectedPeripherals(serviceUuids);
14
+
15
+ export const getBondedDevices = () => BleUtils.getBondedPeripherals();
16
+
17
+ export const pairDevice = (macAddress: string) => BleUtils.pairDevice(macAddress);
18
+
19
+ export const onDeviceBondState = (bleMacAddress: string): Promise<Peripheral | undefined> =>
20
+ new Promise((resolve, reject) => {
21
+ let timeout: any | undefined;
22
+
23
+ const cleanup = (cleanupListener: (() => void) | undefined) => {
24
+ if (timeout) {
25
+ clearTimeout(timeout);
26
+ }
27
+ if (cleanupListener) cleanupListener();
28
+ };
29
+
30
+ const cleanupListener = BleUtils.onDeviceBondState(peripheral => {
31
+ if (peripheral.id?.toLowerCase() !== bleMacAddress.toLowerCase()) {
32
+ return;
33
+ }
34
+ const { bondState } = peripheral;
35
+
36
+ if (bondState.preState === 'BOND_NONE' && bondState.state === 'BOND_BONDING') {
37
+ timeout = setTimeout(() => {
38
+ cleanup(cleanupListener);
39
+ reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded'));
40
+ }, 60 * 1000);
41
+ }
19
42
 
20
- export const getBondedDevices = async () => {
21
- const peripherals = await BleManager.getBondedPeripherals();
22
- return peripherals.map(peripheral => {
23
- const { id, name, advertising = {} } = peripheral;
24
- return { id, name, ...advertising };
43
+ const hasBonded = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_BONDED';
44
+ const hasCanceled = bondState.preState === 'BOND_BONDING' && bondState.state === 'BOND_NONE';
45
+ Logger.debug('onDeviceBondState bondState:', bondState);
46
+ if (hasBonded) {
47
+ cleanup(cleanupListener);
48
+ resolve(peripheral);
49
+ } else if (hasCanceled) {
50
+ cleanup(cleanupListener);
51
+ reject(ERRORS.TypedError(HardwareErrorCode.BleDeviceBondedCanceled, 'bonding canceled'));
52
+ }
53
+ });
25
54
  });
26
- };
27
-
28
- export const refreshCache = async (peripheralId: string) => {
29
- try {
30
- await BleManager.connect(peripheralId);
31
- await BleManager.refreshCache(peripheralId);
32
- await BleManager.disconnect(peripheralId, true);
33
- console.log('refreshCache success');
34
- } catch (error) {
35
- console.log('refreshCache error: ', error);
36
- }
37
- };
@@ -1,4 +1,4 @@
1
- import { Device, Characteristic, BleErrorCode } from '@onekeyfe/react-native-ble-plx';
1
+ import { Device, Characteristic, BleErrorCode } from 'react-native-ble-plx';
2
2
  import { getLogger, LoggerNames, wait } from '@onekeyfe/hd-core';
3
3
  // import { wait } from '@onekeyfe/hd-core/src/utils';
4
4
 
package/src/index.ts CHANGED
@@ -6,12 +6,14 @@ import {
6
6
  BleErrorCode,
7
7
  Characteristic,
8
8
  ScanMode,
9
- } from '@onekeyfe/react-native-ble-plx';
9
+ BleATTErrorCode,
10
+ BleError,
11
+ } from 'react-native-ble-plx';
10
12
  import ByteBuffer from 'bytebuffer';
11
13
  import transport, { COMMON_HEADER_SIZE, LogBlockCommand } from '@onekeyfe/hd-transport';
12
14
  import { createDeferred, Deferred, ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
13
15
  import type EventEmitter from 'events';
14
- import { initializeBleManager, getConnectedDeviceIds, getBondedDevices } from './BleManager';
16
+ import { getConnectedDeviceIds, onDeviceBondState, pairDevice } from './BleManager';
15
17
  import { subscribeBleOn } from './subscribeBleOn';
16
18
  import {
17
19
  isOnekeyDevice,
@@ -43,11 +45,45 @@ const tryToGetConfiguration = (device: Device) => {
43
45
  return infos;
44
46
  };
45
47
 
48
+ type IOBleErrorRemap = Error | BleError | null | undefined;
49
+
50
+ function remapError(error: IOBleErrorRemap) {
51
+ if (error instanceof BleError) {
52
+ if (
53
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
54
+ // @ts-expect-error
55
+ error.iosErrorCode === BleATTErrorCode.UnlikelyError ||
56
+ error.reason === 'Peer removed pairing information'
57
+ ) {
58
+ throw ERRORS.TypedError(HardwareErrorCode.BlePeerRemovedPairingInformation);
59
+ }
60
+
61
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
62
+ // @ts-ignore It's not documented but seems to match a refusal on Android pairing
63
+ if (error?.attErrorCode === 22) {
64
+ throw ERRORS.TypedError(HardwareErrorCode.BleDeviceBondError);
65
+ }
66
+ }
67
+
68
+ if (
69
+ error instanceof Error &&
70
+ error.message &&
71
+ (error.message.includes('was disconnected') || error.message.includes('not found'))
72
+ ) {
73
+ throw ERRORS.TypedError(HardwareErrorCode.BleDeviceDisconnected);
74
+ }
75
+
76
+ // @ts-expect-error
77
+ throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, error.reason ?? error);
78
+ }
79
+
46
80
  export default class ReactNativeBleTransport {
47
81
  blePlxManager: BlePlxManager | undefined;
48
82
 
49
83
  _messages: ReturnType<typeof transport.parseConfigure> | undefined;
50
84
 
85
+ name = 'ReactNativeBleTransport';
86
+
51
87
  configured = false;
52
88
 
53
89
  stopped = false;
@@ -82,7 +118,6 @@ export default class ReactNativeBleTransport {
82
118
  getPlxManager(): Promise<BlePlxManager> {
83
119
  if (this.blePlxManager) return Promise.resolve(this.blePlxManager);
84
120
  this.blePlxManager = new BlePlxManager();
85
- initializeBleManager();
86
121
  return Promise.resolve(this.blePlxManager);
87
122
  }
88
123
 
@@ -218,6 +253,14 @@ export default class ReactNativeBleTransport {
218
253
  throw error;
219
254
  }
220
255
 
256
+ // check device is bonded
257
+ if (Platform.OS === 'android') {
258
+ const bondState = await pairDevice(uuid);
259
+ if (bondState.bonding) {
260
+ await onDeviceBondState(uuid);
261
+ }
262
+ }
263
+
221
264
  if (!device) {
222
265
  const devices = await blePlxManager.devices([uuid]);
223
266
  [device] = devices;
@@ -247,7 +290,7 @@ export default class ReactNativeBleTransport {
247
290
  this.Log.debug('device already connected');
248
291
  throw ERRORS.TypedError(HardwareErrorCode.BleAlreadyConnected);
249
292
  } else {
250
- throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, e.reason ?? e);
293
+ remapError(e);
251
294
  }
252
295
  }
253
296
  }
@@ -282,20 +325,11 @@ export default class ReactNativeBleTransport {
282
325
  }
283
326
  }
284
327
  } else {
285
- throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, e.reason ?? e);
328
+ remapError(e);
286
329
  }
287
330
  }
288
331
  }
289
332
 
290
- // check device is bonded
291
- if (Platform.OS === 'android') {
292
- const bondedDevices = await getBondedDevices();
293
- const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === device?.id);
294
- if (!hasBonded) {
295
- throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded');
296
- }
297
- }
298
-
299
333
  await device.discoverAllServicesAndCharacteristics();
300
334
  let infos = tryToGetConfiguration(device);
301
335
  let characteristics;
@@ -492,6 +526,10 @@ export default class ReactNativeBleTransport {
492
526
  return Promise.resolve(true);
493
527
  }
494
528
 
529
+ async post(session: string, name: string, data: Record<string, unknown>) {
530
+ await this.call(session, name, data);
531
+ }
532
+
495
533
  async call(uuid: string, name: string, data: Record<string, unknown>) {
496
534
  if (this.stopped) {
497
535
  // eslint-disable-next-line prefer-promise-reject-errors
@@ -501,7 +539,10 @@ export default class ReactNativeBleTransport {
501
539
  throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
502
540
  }
503
541
 
504
- if (this.runPromise) {
542
+ const forceRun = name === 'Initialize' || name === 'Cancel';
543
+
544
+ this.Log.debug('transport-react-native call this.runPromise', this.runPromise);
545
+ if (this.runPromise && !forceRun) {
505
546
  throw ERRORS.TypedError(HardwareErrorCode.TransportCallInProgress);
506
547
  }
507
548
 
@@ -617,7 +658,7 @@ export default class ReactNativeBleTransport {
617
658
  }
618
659
 
619
660
  cancel() {
620
- this.Log.debug('transport-react-native canceled');
661
+ this.Log.debug('transport-react-native transport cancel');
621
662
  if (this.runPromise) {
622
663
  // this.runPromise.reject(new Error('Transport_CallCanceled'));
623
664
  }
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type { BleManager as BlePlxManager } from '@onekeyfe/react-native-ble-plx';
1
+ export type { BleManager as BlePlxManager } from 'react-native-ble-plx';
2
2
 
3
3
  export type TransportOptions = {
4
4
  scanTimeout?: number;