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

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,3 +1,5 @@
1
+ export declare const PERMISSION_ERROR = "Bluetooth required to be turned on";
2
+ export declare const LOCATION_ERROR = "Device is not authorized to use BluetoothLE";
1
3
  export declare const isOnekeyDevice: (name: string | null, id?: string | undefined) => boolean;
2
4
  export declare const getBluetoothServiceUuids: () => string[];
3
5
  export declare const getInfosForServiceUuid: (serviceUuid: string, deviceType: 'classic') => {
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,SAAU,MAAM,GAAG,IAAI,8BAAgB,OAYjE,CAAC;AA8BF,eAAO,MAAM,wBAAwB,gBAA0B,CAAC;AAChE,eAAO,MAAM,sBAAsB,gBAAiB,MAAM,cAAc,SAAS;iBA1BhE,MAAM;;;QAoCtB,CAAC"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,uCAAuC,CAAC;AACrE,eAAO,MAAM,cAAc,gDAAgD,CAAC;AAE5E,eAAO,MAAM,cAAc,SAAU,MAAM,GAAG,IAAI,8BAAgB,OAYjE,CAAC;AA8BF,eAAO,MAAM,wBAAwB,gBAA0B,CAAC;AAChE,eAAO,MAAM,sBAAsB,gBAAiB,MAAM,cAAc,SAAS;iBA1BhE,MAAM;;;QAoCtB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  import transport from '@onekeyfe/hd-transport';
2
2
  import { Device, Characteristic } from 'react-native-ble-plx';
3
3
 
4
+ declare const PERMISSION_ERROR = "Bluetooth required to be turned on";
5
+ declare const LOCATION_ERROR = "Device is not authorized to use BluetoothLE";
6
+
4
7
  declare type Deferred<T, I = any, D = any> = {
5
8
  id?: I;
6
9
  data?: D;
@@ -36,4 +39,4 @@ declare class ReactNativeBleTransport {
36
39
  stop(): void;
37
40
  }
38
41
 
39
- export { ReactNativeBleTransport as default };
42
+ export { LOCATION_ERROR, PERMISSION_ERROR, ReactNativeBleTransport as default };
@@ -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;AAI9B,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;IA2DT,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;IA4DpE,IAAI;CAGL"}
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"}
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
3
5
  var buffer = require('buffer');
4
6
  var transport = require('@onekeyfe/hd-transport');
5
7
  var reactNativeBlePlx = require('react-native-ble-plx');
@@ -46,6 +48,47 @@ const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0
46
48
  });
47
49
  });
48
50
 
51
+ const PERMISSION_ERROR = 'Bluetooth required to be turned on';
52
+ const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
53
+ const isOnekeyDevice = (name, id) => {
54
+ var _a;
55
+ if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
56
+ return true;
57
+ }
58
+ const re = /(BixinKey\d{10})|(K\d{4})/i;
59
+ if (name && re.exec(name)) {
60
+ return true;
61
+ }
62
+ return false;
63
+ };
64
+ const ClassicServiceUUID = '00000001-0000-1000-8000-00805f9b34fb';
65
+ const OneKeyServices = {
66
+ classic: {
67
+ [ClassicServiceUUID]: {
68
+ serviceUuid: ClassicServiceUUID,
69
+ writeUuid: '00000002-0000-1000-8000-00805f9b34fb',
70
+ notifyUuid: '00000003-0000-1000-8000-00805f9b34fb',
71
+ },
72
+ },
73
+ };
74
+ const bluetoothServices = [];
75
+ for (const deviceType of Object.keys(OneKeyServices)) {
76
+ const services = OneKeyServices[deviceType];
77
+ bluetoothServices.push(...Object.keys(services));
78
+ }
79
+ const getBluetoothServiceUuids = () => bluetoothServices;
80
+ const getInfosForServiceUuid = (serviceUuid, deviceType) => {
81
+ const services = OneKeyServices[deviceType];
82
+ if (!services) {
83
+ return null;
84
+ }
85
+ const service = services[serviceUuid];
86
+ if (!service) {
87
+ return null;
88
+ }
89
+ return service;
90
+ };
91
+
49
92
  const timer = process.env.NODE_ENV === 'development'
50
93
  ? {
51
94
  timeout: (fn, ms) => {
@@ -70,9 +113,7 @@ const timer = process.env.NODE_ENV === 'development'
70
113
 
71
114
  const subscribeBleOn = (bleManager, ms = 3000) => new Promise((resolve, reject) => {
72
115
  let done = false;
73
- let lastState = 'Unknown';
74
116
  const subscription = bleManager.onStateChange(state => {
75
- lastState = state;
76
117
  console.log('ble state -> ', state);
77
118
  if (state === 'PoweredOn') {
78
119
  if (done)
@@ -87,49 +128,10 @@ const subscribeBleOn = (bleManager, ms = 3000) => new Promise((resolve, reject)
87
128
  if (done)
88
129
  return;
89
130
  subscription.remove();
90
- reject(new Error(`Bluetooth required to be turned ${lastState}`));
131
+ reject(new Error(PERMISSION_ERROR));
91
132
  }, ms);
92
133
  });
93
134
 
94
- const isOnekeyDevice = (name, id) => {
95
- var _a;
96
- if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
97
- return true;
98
- }
99
- const re = /(BixinKey\d{10})|(K\d{4})/i;
100
- if (name && re.exec(name)) {
101
- return true;
102
- }
103
- return false;
104
- };
105
- const ClassicServiceUUID = '00000001-0000-1000-8000-00805f9b34fb';
106
- const OneKeyServices = {
107
- classic: {
108
- [ClassicServiceUUID]: {
109
- serviceUuid: ClassicServiceUUID,
110
- writeUuid: '00000002-0000-1000-8000-00805f9b34fb',
111
- notifyUuid: '00000003-0000-1000-8000-00805f9b34fb',
112
- },
113
- },
114
- };
115
- const bluetoothServices = [];
116
- for (const deviceType of Object.keys(OneKeyServices)) {
117
- const services = OneKeyServices[deviceType];
118
- bluetoothServices.push(...Object.keys(services));
119
- }
120
- const getBluetoothServiceUuids = () => bluetoothServices;
121
- const getInfosForServiceUuid = (serviceUuid, deviceType) => {
122
- const services = OneKeyServices[deviceType];
123
- if (!services) {
124
- return null;
125
- }
126
- const service = services[serviceUuid];
127
- if (!service) {
128
- return null;
129
- }
130
- return service;
131
- };
132
-
133
135
  function create(arg, data) {
134
136
  let localResolve = (_t) => { };
135
137
  let localReject = (_e) => { };
@@ -217,14 +219,14 @@ class ReactNativeBleTransport {
217
219
  }
218
220
  enumerate() {
219
221
  return __awaiter(this, void 0, void 0, function* () {
220
- return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
222
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
221
223
  const deviceList = [];
222
224
  try {
223
225
  yield subscribeBleOn(blePlxManager);
224
226
  }
225
227
  catch (error) {
226
228
  console.log('subscribeBleOn error: ', error);
227
- resolve([]);
229
+ reject(error);
228
230
  return;
229
231
  }
230
232
  blePlxManager.startDeviceScan(null, {
@@ -234,6 +236,7 @@ class ReactNativeBleTransport {
234
236
  if (error) {
235
237
  console.log('ble scan manager: ', blePlxManager);
236
238
  console.log('ble scan error: ', error);
239
+ reject(error);
237
240
  return;
238
241
  }
239
242
  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)) {
@@ -405,7 +408,6 @@ class ReactNativeBleTransport {
405
408
  }
406
409
  if (buffer$1.length >= bufferLength) {
407
410
  const value = buffer.Buffer.from(buffer$1);
408
- console.log('[hd-transport-react-native] Received a complete packet of data, resolve Promise, this.runPromise: ', this.runPromise, 'buffer: ', value);
409
411
  bufferLength = 0;
410
412
  buffer$1 = [];
411
413
  (_a = this.runPromise) === null || _a === void 0 ? void 0 : _a.resolve(value.toString('hex'));
@@ -449,7 +451,7 @@ class ReactNativeBleTransport {
449
451
  }
450
452
  this.runPromise = create();
451
453
  const messages = this._messages;
452
- console.log('transport-react-native', 'call-', 'messages: ', messages, ' name: ', name, ' data: ', data);
454
+ console.log('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
453
455
  const buffers = buildBuffers(messages, name, data);
454
456
  for (const o of buffers) {
455
457
  const outData = o.toString('base64');
@@ -486,4 +488,6 @@ class ReactNativeBleTransport {
486
488
  }
487
489
  }
488
490
 
489
- module.exports = ReactNativeBleTransport;
491
+ exports.LOCATION_ERROR = LOCATION_ERROR;
492
+ exports.PERMISSION_ERROR = PERMISSION_ERROR;
493
+ exports["default"] = ReactNativeBleTransport;
@@ -1 +1 @@
1
- {"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGxC,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAwB9E,CAAC"}
1
+ {"version":3,"file":"subscribeBleOn.d.ts","sourceRoot":"","sources":["../src/subscribeBleOn.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAGxC,eAAO,MAAM,cAAc,eAAgB,aAAa,kBAAc,QAAQ,IAAI,CAqB9E,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "0.0.3",
3
+ "version": "0.0.6",
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.3",
23
+ "@onekeyfe/hd-transport": "^0.0.6",
24
24
  "react-native-ble-manager": "^8.1.0",
25
25
  "react-native-ble-plx": "^2.0.3"
26
26
  },
27
- "gitHead": "179c9f232b1cc52aa60af42f6daec76564f20e50"
27
+ "gitHead": "b37ddf4e02812f40a01ec38ffef2ff6cdcad4bbc"
28
28
  }
package/src/constants.ts CHANGED
@@ -1,3 +1,6 @@
1
+ export const PERMISSION_ERROR = 'Bluetooth required to be turned on';
2
+ export const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
3
+
1
4
  export const isOnekeyDevice = (name: string | null, id?: string): boolean => {
2
5
  if (id?.startsWith?.('MI')) {
3
6
  return true;
package/src/index.ts CHANGED
@@ -9,7 +9,13 @@ import {
9
9
  } from 'react-native-ble-plx';
10
10
  import { initializeBleManager, getConnectedDeviceIds } from './BleManager';
11
11
  import { subscribeBleOn } from './subscribeBleOn';
12
- import { isOnekeyDevice, getBluetoothServiceUuids, getInfosForServiceUuid } from './constants';
12
+ import {
13
+ PERMISSION_ERROR,
14
+ LOCATION_ERROR,
15
+ isOnekeyDevice,
16
+ getBluetoothServiceUuids,
17
+ getInfosForServiceUuid,
18
+ } from './constants';
13
19
  import { Deferred, create as createDeferred } from './utils/deferred';
14
20
  import { isHeaderChunk } from './utils/validateNotify';
15
21
  import BleTransport from './BleTransport';
@@ -71,14 +77,14 @@ export default class ReactNativeBleTransport {
71
77
  */
72
78
  async enumerate() {
73
79
  // eslint-disable-next-line no-async-promise-executor
74
- return new Promise<Device[]>(async resolve => {
80
+ return new Promise<Device[]>(async (resolve, reject) => {
75
81
  const deviceList: Device[] = [];
76
82
 
77
83
  try {
78
84
  await subscribeBleOn(blePlxManager);
79
85
  } catch (error) {
80
86
  console.log('subscribeBleOn error: ', error);
81
- resolve([]);
87
+ reject(error);
82
88
  return;
83
89
  }
84
90
 
@@ -91,6 +97,7 @@ export default class ReactNativeBleTransport {
91
97
  if (error) {
92
98
  console.log('ble scan manager: ', blePlxManager);
93
99
  console.log('ble scan error: ', error);
100
+ reject(error);
94
101
  return;
95
102
  }
96
103
 
@@ -297,12 +304,12 @@ export default class ReactNativeBleTransport {
297
304
 
298
305
  if (buffer.length >= bufferLength) {
299
306
  const value = Buffer.from(buffer);
300
- console.log(
301
- '[hd-transport-react-native] Received a complete packet of data, resolve Promise, this.runPromise: ',
302
- this.runPromise,
303
- 'buffer: ',
304
- value
305
- );
307
+ // console.log(
308
+ // '[hd-transport-react-native] Received a complete packet of data, resolve Promise, this.runPromise: ',
309
+ // this.runPromise,
310
+ // 'buffer: ',
311
+ // value
312
+ // );
306
313
  bufferLength = 0;
307
314
  buffer = [];
308
315
  this.runPromise?.resolve(value.toString('hex'));
@@ -354,16 +361,7 @@ export default class ReactNativeBleTransport {
354
361
 
355
362
  this.runPromise = createDeferred();
356
363
  const messages = this._messages;
357
- console.log(
358
- 'transport-react-native',
359
- 'call-',
360
- 'messages: ',
361
- messages,
362
- ' name: ',
363
- name,
364
- ' data: ',
365
- data
366
- );
364
+ console.log('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
367
365
  const buffers = buildBuffers(messages, name, data);
368
366
  for (const o of buffers) {
369
367
  const outData = o.toString('base64');
@@ -398,3 +396,5 @@ export default class ReactNativeBleTransport {
398
396
  this.stopped = true;
399
397
  }
400
398
  }
399
+
400
+ export { PERMISSION_ERROR, LOCATION_ERROR };
@@ -1,14 +1,12 @@
1
+ import { PERMISSION_ERROR } from './constants';
1
2
  import { BlePlxManager } from './types';
2
3
  import timer from './utils/timer';
3
4
 
4
5
  export const subscribeBleOn = (bleManager: BlePlxManager, ms = 3000): Promise<void> =>
5
6
  new Promise((resolve, reject) => {
6
7
  let done = false;
7
- let lastState = 'Unknown';
8
8
 
9
9
  const subscription = bleManager.onStateChange(state => {
10
- lastState = state;
11
-
12
10
  console.log('ble state -> ', state);
13
11
 
14
12
  if (state === 'PoweredOn') {
@@ -23,6 +21,6 @@ export const subscribeBleOn = (bleManager: BlePlxManager, ms = 3000): Promise<vo
23
21
  const clearTimeout = timer.timeout(() => {
24
22
  if (done) return;
25
23
  subscription.remove();
26
- reject(new Error(`Bluetooth required to be turned ${lastState}`));
24
+ reject(new Error(PERMISSION_ERROR));
27
25
  }, ms);
28
26
  });