@onekeyfe/hd-transport-react-native 0.0.6 → 0.0.9

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.
@@ -8,4 +8,13 @@ export declare const getConnectedDeviceIds: (serviceUuids: string[]) => Promise<
8
8
  id: string;
9
9
  name: string | undefined;
10
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
+ }[]>;
11
20
  //# 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"}
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"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import transport from '@onekeyfe/hd-transport';
2
- import { Device, Characteristic } from 'react-native-ble-plx';
2
+ import { BleManager, Device, Characteristic } from 'react-native-ble-plx';
3
3
 
4
4
  declare const PERMISSION_ERROR = "Bluetooth required to be turned on";
5
5
  declare const LOCATION_ERROR = "Device is not authorized to use BluetoothLE";
@@ -20,6 +20,7 @@ declare type BleAcquireInput = {
20
20
  };
21
21
 
22
22
  declare class ReactNativeBleTransport {
23
+ blePlxManager: BleManager | undefined;
23
24
  _messages: ReturnType<typeof transport.parseConfigure> | undefined;
24
25
  configured: boolean;
25
26
  stopped: boolean;
@@ -29,6 +30,7 @@ declare class ReactNativeBleTransport {
29
30
  init(): void;
30
31
  configure(signedData: any): void;
31
32
  listen(): void;
33
+ getPlxManager(): Promise<BleManager>;
32
34
  enumerate(): Promise<Device[]>;
33
35
  acquire(input: BleAcquireInput): Promise<{
34
36
  uuid: string;
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,wBAAwB,CAAC;AAC/C,OAAO,EAEL,MAAM,EAEN,cAAc,EAEf,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACL,gBAAgB,EAChB,cAAc,EAIf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,QAAQ,EAA4B,MAAM,kBAAkB,CAAC;AAItE,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAqBjE,MAAM,CAAC,OAAO,OAAO,uBAAuB;IAC1C,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEnE,UAAU,UAAS;IAEnB,OAAO,UAAS;IAEhB,WAAW,SAAQ;IAEnB,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAQ;gBAE5B,OAAO,EAAE,gBAAgB;IAIrC,IAAI;IAIJ,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,MAAM;IASA,SAAS;IA4DT,OAAO,CAAC,KAAK,EAAE,eAAe;;;IAiJpC,sBAAsB,CAAC,cAAc,EAAE,cAAc;IA8C/C,OAAO,CAAC,IAAI,EAAE,MAAM;IAepB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAmDpE,IAAI;CAGL;AAED,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,SAAiC,MAAM,wBAAwB,CAAC;AACvE,OAAO,EACL,UAAU,IAAI,aAAa,EAC3B,MAAM,EAEN,cAAc,EAEf,MAAM,sBAAsB,CAAC;AAG9B,OAAO,EACL,gBAAgB,EAChB,cAAc,EAIf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,QAAQ,EAA4B,MAAM,kBAAkB,CAAC;AAItE,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAmBjE,MAAM,CAAC,OAAO,OAAO,uBAAuB;IAC1C,aAAa,EAAE,aAAa,GAAG,SAAS,CAAC;IAEzC,SAAS,EAAE,UAAU,CAAC,OAAO,SAAS,CAAC,cAAc,CAAC,GAAG,SAAS,CAAC;IAEnE,UAAU,UAAS;IAEnB,OAAO,UAAS;IAEhB,WAAW,SAAQ;IAEnB,UAAU,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAI,CAAQ;gBAE5B,OAAO,EAAE,gBAAgB;IAIrC,IAAI;IAEJ,SAAS,CAAC,UAAU,EAAE,GAAG;IAMzB,MAAM;IAIN,aAAa,IAAI,OAAO,CAAC,aAAa,CAAC;IAYjC,SAAS;IAoET,OAAO,CAAC,KAAK,EAAE,eAAe;;;IAyJpC,sBAAsB,CAAC,cAAc,EAAE,cAAc;IA+C/C,OAAO,CAAC,IAAI,EAAE,MAAM;IAepB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAmDpE,IAAI;CAGL;AAED,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -47,6 +47,13 @@ const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0
47
47
  return Object.assign({ id, name }, advertising);
48
48
  });
49
49
  });
50
+ const getBondedDevices = () => __awaiter(void 0, void 0, void 0, function* () {
51
+ const peripherals = yield BleManager__default["default"].getBondedPeripherals();
52
+ return peripherals.map(peripheral => {
53
+ const { id, name, advertising = {} } = peripheral;
54
+ return Object.assign({ id, name }, advertising);
55
+ });
56
+ });
50
57
 
51
58
  const PERMISSION_ERROR = 'Bluetooth required to be turned on';
52
59
  const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
@@ -55,7 +62,7 @@ const isOnekeyDevice = (name, id) => {
55
62
  if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
56
63
  return true;
57
64
  }
58
- const re = /(BixinKey\d{10})|(K\d{4})/i;
65
+ const re = /(BixinKey\d{10})|(K\d{4})|(T\d{4})/i;
59
66
  if (name && re.exec(name)) {
60
67
  return true;
61
68
  }
@@ -111,7 +118,7 @@ const timer = process.env.NODE_ENV === 'development'
111
118
  },
112
119
  };
113
120
 
114
- const subscribeBleOn = (bleManager, ms = 3000) => new Promise((resolve, reject) => {
121
+ const subscribeBleOn = (bleManager, ms = 1000) => new Promise((resolve, reject) => {
115
122
  let done = false;
116
123
  const subscription = bleManager.onStateChange(state => {
117
124
  console.log('ble state -> ', state);
@@ -183,10 +190,9 @@ class BleTransport {
183
190
  }
184
191
 
185
192
  const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"];
186
- const blePlxManager = new reactNativeBlePlx.BleManager();
187
193
  const transportCache = {};
188
194
  let connectOptions = {
189
- requestMTU: 1500,
195
+ requestMTU: 512,
190
196
  timeout: 3000,
191
197
  };
192
198
  const tryToGetConfiguration = (device) => {
@@ -207,9 +213,7 @@ class ReactNativeBleTransport {
207
213
  this.runPromise = null;
208
214
  this.scanTimeout = (_a = options.scanTimeout) !== null && _a !== void 0 ? _a : 3000;
209
215
  }
210
- init() {
211
- initializeBleManager();
212
- }
216
+ init() { }
213
217
  configure(signedData) {
214
218
  const messages = parseConfigure(signedData);
215
219
  this.configured = true;
@@ -217,10 +221,18 @@ class ReactNativeBleTransport {
217
221
  }
218
222
  listen() {
219
223
  }
224
+ getPlxManager() {
225
+ if (this.blePlxManager)
226
+ return Promise.resolve(this.blePlxManager);
227
+ this.blePlxManager = new reactNativeBlePlx.BleManager();
228
+ initializeBleManager();
229
+ return Promise.resolve(this.blePlxManager);
230
+ }
220
231
  enumerate() {
221
232
  return __awaiter(this, void 0, void 0, function* () {
222
233
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
223
234
  const deviceList = [];
235
+ const blePlxManager = yield this.getPlxManager();
224
236
  try {
225
237
  yield subscribeBleOn(blePlxManager);
226
238
  }
@@ -236,7 +248,12 @@ class ReactNativeBleTransport {
236
248
  if (error) {
237
249
  console.log('ble scan manager: ', blePlxManager);
238
250
  console.log('ble scan error: ', error);
239
- reject(error);
251
+ if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
252
+ reject(new Error(PERMISSION_ERROR));
253
+ }
254
+ else {
255
+ reject(error);
256
+ }
240
257
  return;
241
258
  }
242
259
  if (isOnekeyDevice((_a = device === null || device === void 0 ? void 0 : device.name) !== null && _a !== void 0 ? _a : null, device === null || device === void 0 ? void 0 : device.id)) {
@@ -271,11 +288,12 @@ class ReactNativeBleTransport {
271
288
  if (!uuid) {
272
289
  throw new Error('uuid is required');
273
290
  }
274
- let device;
291
+ let device = null;
275
292
  if (transportCache[uuid]) {
276
293
  console.log('@onekey/hd-ble-sdk transport not be released, will release: ', uuid);
277
294
  yield this.release(uuid);
278
295
  }
296
+ const blePlxManager = yield this.getPlxManager();
279
297
  try {
280
298
  yield subscribeBleOn(blePlxManager);
281
299
  }
@@ -330,6 +348,11 @@ class ReactNativeBleTransport {
330
348
  }
331
349
  }
332
350
  }
351
+ const bondedDevices = yield getBondedDevices();
352
+ const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === (device === null || device === void 0 ? void 0 : device.id));
353
+ if (!hasBonded) {
354
+ throw new Error('device is not bonded');
355
+ }
333
356
  yield device.discoverAllServicesAndCharacteristics();
334
357
  let infos = tryToGetConfiguration(device);
335
358
  let characteristics;
@@ -406,7 +429,7 @@ class ReactNativeBleTransport {
406
429
  else {
407
430
  buffer$1 = buffer$1.concat([...data]);
408
431
  }
409
- if (buffer$1.length >= bufferLength) {
432
+ if (buffer$1.length - transport.COMMON_HEADER_SIZE >= bufferLength) {
410
433
  const value = buffer.Buffer.from(buffer$1);
411
434
  bufferLength = 0;
412
435
  buffer$1 = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "0.0.6",
3
+ "version": "0.0.9",
4
4
  "homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -20,9 +20,9 @@
20
20
  "lint:fix": "eslint . --fix"
21
21
  },
22
22
  "dependencies": {
23
- "@onekeyfe/hd-transport": "^0.0.6",
23
+ "@onekeyfe/hd-transport": "^0.0.9",
24
24
  "react-native-ble-manager": "^8.1.0",
25
25
  "react-native-ble-plx": "^2.0.3"
26
26
  },
27
- "gitHead": "b37ddf4e02812f40a01ec38ffef2ff6cdcad4bbc"
27
+ "gitHead": "728d72e4c7ddc10227fff49c3cb8d4a0ff205fac"
28
28
  }
package/src/BleManager.ts CHANGED
@@ -16,3 +16,11 @@ export const getConnectedDeviceIds = async (serviceUuids: string[]) => {
16
16
  return { id, name, ...advertising };
17
17
  });
18
18
  };
19
+
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 };
25
+ });
26
+ };
package/src/constants.ts CHANGED
@@ -6,9 +6,9 @@ export const isOnekeyDevice = (name: string | null, id?: string): boolean => {
6
6
  return true;
7
7
  }
8
8
 
9
- // 过滤 BixinKeyxxx 和 Kxxxx
9
+ // 过滤 BixinKeyxxx 和 Kxxxx 和 Txxxx
10
10
  // i 忽略大小写模式
11
- const re = /(BixinKey\d{10})|(K\d{4})/i;
11
+ const re = /(BixinKey\d{10})|(K\d{4})|(T\d{4})/i;
12
12
  if (name && re.exec(name)) {
13
13
  return true;
14
14
  }
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Buffer } from 'buffer';
2
- import transport from '@onekeyfe/hd-transport';
2
+ import transport, { COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport';
3
3
  import {
4
4
  BleManager as BlePlxManager,
5
5
  Device,
@@ -7,7 +7,7 @@ import {
7
7
  Characteristic,
8
8
  ScanMode,
9
9
  } from 'react-native-ble-plx';
10
- import { initializeBleManager, getConnectedDeviceIds } from './BleManager';
10
+ import { initializeBleManager, getConnectedDeviceIds, getBondedDevices } from './BleManager';
11
11
  import { subscribeBleOn } from './subscribeBleOn';
12
12
  import {
13
13
  PERMISSION_ERROR,
@@ -24,12 +24,10 @@ import type { BleAcquireInput, TransportOptions } from './types';
24
24
 
25
25
  const { check, buildBuffers, receiveOne, parseConfigure } = transport;
26
26
 
27
- const blePlxManager = new BlePlxManager();
28
-
29
27
  const transportCache: Record<string, any> = {};
30
28
 
31
29
  let connectOptions: Record<string, unknown> = {
32
- requestMTU: 1500,
30
+ requestMTU: 512,
33
31
  timeout: 3000,
34
32
  };
35
33
 
@@ -42,6 +40,8 @@ const tryToGetConfiguration = (device: Device) => {
42
40
  };
43
41
 
44
42
  export default class ReactNativeBleTransport {
43
+ blePlxManager: BlePlxManager | undefined;
44
+
45
45
  _messages: ReturnType<typeof transport.parseConfigure> | undefined;
46
46
 
47
47
  configured = false;
@@ -56,9 +56,7 @@ export default class ReactNativeBleTransport {
56
56
  this.scanTimeout = options.scanTimeout ?? 3000;
57
57
  }
58
58
 
59
- init() {
60
- initializeBleManager();
61
- }
59
+ init() {}
62
60
 
63
61
  configure(signedData: any) {
64
62
  const messages = parseConfigure(signedData);
@@ -70,6 +68,13 @@ export default class ReactNativeBleTransport {
70
68
  // empty
71
69
  }
72
70
 
71
+ getPlxManager(): Promise<BlePlxManager> {
72
+ if (this.blePlxManager) return Promise.resolve(this.blePlxManager);
73
+ this.blePlxManager = new BlePlxManager();
74
+ initializeBleManager();
75
+ return Promise.resolve(this.blePlxManager);
76
+ }
77
+
73
78
  /**
74
79
  * 获取设备列表
75
80
  * 在搜索超过超时时间或设备数量大于 5 台时,返回 OneKey 设备,
@@ -79,7 +84,7 @@ export default class ReactNativeBleTransport {
79
84
  // eslint-disable-next-line no-async-promise-executor
80
85
  return new Promise<Device[]>(async (resolve, reject) => {
81
86
  const deviceList: Device[] = [];
82
-
87
+ const blePlxManager = await this.getPlxManager();
83
88
  try {
84
89
  await subscribeBleOn(blePlxManager);
85
90
  } catch (error) {
@@ -97,7 +102,15 @@ export default class ReactNativeBleTransport {
97
102
  if (error) {
98
103
  console.log('ble scan manager: ', blePlxManager);
99
104
  console.log('ble scan error: ', error);
100
- reject(error);
105
+ if (
106
+ [BleErrorCode.BluetoothPoweredOff, BleErrorCode.BluetoothInUnknownState].includes(
107
+ error.errorCode
108
+ )
109
+ ) {
110
+ reject(new Error(PERMISSION_ERROR));
111
+ } else {
112
+ reject(error);
113
+ }
101
114
  return;
102
115
  }
103
116
 
@@ -142,7 +155,7 @@ export default class ReactNativeBleTransport {
142
155
  throw new Error('uuid is required');
143
156
  }
144
157
 
145
- let device;
158
+ let device: Device | null = null;
146
159
 
147
160
  if (transportCache[uuid]) {
148
161
  /**
@@ -153,6 +166,7 @@ export default class ReactNativeBleTransport {
153
166
  await this.release(uuid);
154
167
  }
155
168
 
169
+ const blePlxManager = await this.getPlxManager();
156
170
  try {
157
171
  await subscribeBleOn(blePlxManager);
158
172
  } catch (error) {
@@ -213,6 +227,13 @@ export default class ReactNativeBleTransport {
213
227
  }
214
228
  }
215
229
 
230
+ // check device is bonded
231
+ const bondedDevices = await getBondedDevices();
232
+ const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === device?.id);
233
+ if (!hasBonded) {
234
+ throw new Error('device is not bonded');
235
+ }
236
+
216
237
  await device.discoverAllServicesAndCharacteristics();
217
238
  let infos = tryToGetConfiguration(device);
218
239
  let characteristics;
@@ -295,6 +316,7 @@ export default class ReactNativeBleTransport {
295
316
 
296
317
  try {
297
318
  const data = Buffer.from(c.value as string, 'base64');
319
+ // console.log('[hd-transport-react-native] Received a packet, ', 'buffer: ', data);
298
320
  if (isHeaderChunk(data)) {
299
321
  bufferLength = data.readInt32BE(5);
300
322
  buffer = [...data.subarray(3)];
@@ -302,7 +324,7 @@ export default class ReactNativeBleTransport {
302
324
  buffer = buffer.concat([...data]);
303
325
  }
304
326
 
305
- if (buffer.length >= bufferLength) {
327
+ if (buffer.length - COMMON_HEADER_SIZE >= bufferLength) {
306
328
  const value = Buffer.from(buffer);
307
329
  // console.log(
308
330
  // '[hd-transport-react-native] Received a complete packet of data, resolve Promise, this.runPromise: ',
@@ -2,7 +2,7 @@ import { PERMISSION_ERROR } from './constants';
2
2
  import { BlePlxManager } from './types';
3
3
  import timer from './utils/timer';
4
4
 
5
- export const subscribeBleOn = (bleManager: BlePlxManager, ms = 3000): Promise<void> =>
5
+ export const subscribeBleOn = (bleManager: BlePlxManager, ms = 1000): Promise<void> =>
6
6
  new Promise((resolve, reject) => {
7
7
  let done = false;
8
8