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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.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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U,EAAE,GAAG;IAMzB,MAAM;IAIN,aAAa,IAAI,OAAO,CAAC,aAAa,CAAC;IAYjC,SAAS;IAoET,OAAO,CAAC,KAAK,EAAE,eAAe;;;IAkJpC,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
@@ -55,7 +55,7 @@ const isOnekeyDevice = (name, id) => {
55
55
  if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
56
56
  return true;
57
57
  }
58
- const re = /(BixinKey\d{10})|(K\d{4})/i;
58
+ const re = /(BixinKey\d{10})|(K\d{4})|(T\d{4})/i;
59
59
  if (name && re.exec(name)) {
60
60
  return true;
61
61
  }
@@ -111,7 +111,7 @@ const timer = process.env.NODE_ENV === 'development'
111
111
  },
112
112
  };
113
113
 
114
- const subscribeBleOn = (bleManager, ms = 3000) => new Promise((resolve, reject) => {
114
+ const subscribeBleOn = (bleManager, ms = 1000) => new Promise((resolve, reject) => {
115
115
  let done = false;
116
116
  const subscription = bleManager.onStateChange(state => {
117
117
  console.log('ble state -> ', state);
@@ -183,10 +183,9 @@ class BleTransport {
183
183
  }
184
184
 
185
185
  const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"];
186
- const blePlxManager = new reactNativeBlePlx.BleManager();
187
186
  const transportCache = {};
188
187
  let connectOptions = {
189
- requestMTU: 1500,
188
+ requestMTU: 512,
190
189
  timeout: 3000,
191
190
  };
192
191
  const tryToGetConfiguration = (device) => {
@@ -207,9 +206,7 @@ class ReactNativeBleTransport {
207
206
  this.runPromise = null;
208
207
  this.scanTimeout = (_a = options.scanTimeout) !== null && _a !== void 0 ? _a : 3000;
209
208
  }
210
- init() {
211
- initializeBleManager();
212
- }
209
+ init() { }
213
210
  configure(signedData) {
214
211
  const messages = parseConfigure(signedData);
215
212
  this.configured = true;
@@ -217,10 +214,18 @@ class ReactNativeBleTransport {
217
214
  }
218
215
  listen() {
219
216
  }
217
+ getPlxManager() {
218
+ if (this.blePlxManager)
219
+ return Promise.resolve(this.blePlxManager);
220
+ this.blePlxManager = new reactNativeBlePlx.BleManager();
221
+ initializeBleManager();
222
+ return Promise.resolve(this.blePlxManager);
223
+ }
220
224
  enumerate() {
221
225
  return __awaiter(this, void 0, void 0, function* () {
222
226
  return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
223
227
  const deviceList = [];
228
+ const blePlxManager = yield this.getPlxManager();
224
229
  try {
225
230
  yield subscribeBleOn(blePlxManager);
226
231
  }
@@ -236,7 +241,12 @@ class ReactNativeBleTransport {
236
241
  if (error) {
237
242
  console.log('ble scan manager: ', blePlxManager);
238
243
  console.log('ble scan error: ', error);
239
- reject(error);
244
+ if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
245
+ reject(new Error(PERMISSION_ERROR));
246
+ }
247
+ else {
248
+ reject(error);
249
+ }
240
250
  return;
241
251
  }
242
252
  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)) {
@@ -276,6 +286,7 @@ class ReactNativeBleTransport {
276
286
  console.log('@onekey/hd-ble-sdk transport not be released, will release: ', uuid);
277
287
  yield this.release(uuid);
278
288
  }
289
+ const blePlxManager = yield this.getPlxManager();
279
290
  try {
280
291
  yield subscribeBleOn(blePlxManager);
281
292
  }
@@ -406,7 +417,7 @@ class ReactNativeBleTransport {
406
417
  else {
407
418
  buffer$1 = buffer$1.concat([...data]);
408
419
  }
409
- if (buffer$1.length >= bufferLength) {
420
+ if (buffer$1.length - transport.COMMON_HEADER_SIZE >= bufferLength) {
410
421
  const value = buffer.Buffer.from(buffer$1);
411
422
  bufferLength = 0;
412
423
  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.7",
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.7",
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": "7be7677368fcb50f411891f009367424ea77df33"
28
28
  }
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,
@@ -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
 
@@ -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) {
@@ -295,6 +309,7 @@ export default class ReactNativeBleTransport {
295
309
 
296
310
  try {
297
311
  const data = Buffer.from(c.value as string, 'base64');
312
+ // console.log('[hd-transport-react-native] Received a packet, ', 'buffer: ', data);
298
313
  if (isHeaderChunk(data)) {
299
314
  bufferLength = data.readInt32BE(5);
300
315
  buffer = [...data.subarray(3)];
@@ -302,7 +317,7 @@ export default class ReactNativeBleTransport {
302
317
  buffer = buffer.concat([...data]);
303
318
  }
304
319
 
305
- if (buffer.length >= bufferLength) {
320
+ if (buffer.length - COMMON_HEADER_SIZE >= bufferLength) {
306
321
  const value = Buffer.from(buffer);
307
322
  // console.log(
308
323
  // '[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