@onekeyfe/hd-transport-react-native 0.1.44 → 0.1.45

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.
Files changed (2) hide show
  1. package/dist/index.js +73 -65
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,10 +1,18 @@
1
- import { Platform } from 'react-native';
2
- import { Buffer } from 'buffer';
3
- import { BleManager as BleManager$1, BleErrorCode, ScanMode } from 'react-native-ble-plx';
4
- import ByteBuffer from 'bytebuffer';
5
- import transport, { MESSAGE_TOP_CHAR, MESSAGE_HEADER_BYTE, COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport';
6
- import { ERRORS, HardwareErrorCode, createDeferred } from '@onekeyfe/hd-shared';
7
- import BleManager from 'react-native-ble-manager';
1
+ 'use strict';
2
+
3
+ var reactNative = require('react-native');
4
+ var buffer = require('buffer');
5
+ var reactNativeBlePlx = require('react-native-ble-plx');
6
+ var ByteBuffer = require('bytebuffer');
7
+ var transport = require('@onekeyfe/hd-transport');
8
+ var hdShared = require('@onekeyfe/hd-shared');
9
+ var BleManager = require('react-native-ble-manager');
10
+
11
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
+
13
+ var ByteBuffer__default = /*#__PURE__*/_interopDefaultLegacy(ByteBuffer);
14
+ var transport__default = /*#__PURE__*/_interopDefaultLegacy(transport);
15
+ var BleManager__default = /*#__PURE__*/_interopDefaultLegacy(BleManager);
8
16
 
9
17
  /******************************************************************************
10
18
  Copyright (c) Microsoft Corporation.
@@ -32,17 +40,17 @@ function __awaiter(thisArg, _arguments, P, generator) {
32
40
  }
33
41
 
34
42
  const initializeBleManager = () => {
35
- BleManager.start({ showAlert: false });
43
+ BleManager__default["default"].start({ showAlert: false });
36
44
  };
37
45
  const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0, function* () {
38
- const connectedPeripherals = yield BleManager.getConnectedPeripherals(serviceUuids);
46
+ const connectedPeripherals = yield BleManager__default["default"].getConnectedPeripherals(serviceUuids);
39
47
  return connectedPeripherals.map(peripheral => {
40
48
  const { id, name, advertising = {} } = peripheral;
41
49
  return Object.assign({ id, name }, advertising);
42
50
  });
43
51
  });
44
52
  const getBondedDevices = () => __awaiter(void 0, void 0, void 0, function* () {
45
- const peripherals = yield BleManager.getBondedPeripherals();
53
+ const peripherals = yield BleManager__default["default"].getBondedPeripherals();
46
54
  return peripherals.map(peripheral => {
47
55
  const { id, name, advertising = {} } = peripheral;
48
56
  return Object.assign({ id, name }, advertising);
@@ -88,7 +96,7 @@ const subscribeBleOn = (bleManager, ms = 1000) => new Promise((resolve, reject)
88
96
  if (done)
89
97
  return;
90
98
  subscription.remove();
91
- reject(ERRORS.TypedError(HardwareErrorCode.BlePermissionError));
99
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError));
92
100
  }, ms);
93
101
  });
94
102
 
@@ -137,9 +145,9 @@ const isHeaderChunk = (chunk) => {
137
145
  if (chunk.length < 9)
138
146
  return false;
139
147
  const [MagicQuestionMark, sharp1, sharp2] = chunk;
140
- if (String.fromCharCode(MagicQuestionMark) === String.fromCharCode(MESSAGE_TOP_CHAR) &&
141
- String.fromCharCode(sharp1) === String.fromCharCode(MESSAGE_HEADER_BYTE) &&
142
- String.fromCharCode(sharp2) === String.fromCharCode(MESSAGE_HEADER_BYTE)) {
148
+ if (String.fromCharCode(MagicQuestionMark) === String.fromCharCode(transport.MESSAGE_TOP_CHAR) &&
149
+ String.fromCharCode(sharp1) === String.fromCharCode(transport.MESSAGE_HEADER_BYTE) &&
150
+ String.fromCharCode(sharp2) === String.fromCharCode(transport.MESSAGE_HEADER_BYTE)) {
143
151
  return true;
144
152
  }
145
153
  return false;
@@ -156,7 +164,7 @@ class BleTransport {
156
164
  }
157
165
  }
158
166
 
159
- const { check, buildBuffers, receiveOne, parseConfigure } = transport;
167
+ const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"];
160
168
  const transportCache = {};
161
169
  let connectOptions = {
162
170
  requestMTU: 256,
@@ -194,7 +202,7 @@ class ReactNativeBleTransport {
194
202
  getPlxManager() {
195
203
  if (this.blePlxManager)
196
204
  return Promise.resolve(this.blePlxManager);
197
- this.blePlxManager = new BleManager$1();
205
+ this.blePlxManager = new reactNativeBlePlx.BleManager();
198
206
  initializeBleManager();
199
207
  return Promise.resolve(this.blePlxManager);
200
208
  }
@@ -212,23 +220,23 @@ class ReactNativeBleTransport {
212
220
  return;
213
221
  }
214
222
  blePlxManager.startDeviceScan(null, {
215
- scanMode: ScanMode.LowLatency,
223
+ scanMode: reactNativeBlePlx.ScanMode.LowLatency,
216
224
  }, (error, device) => {
217
225
  var _a, _b;
218
226
  if (error) {
219
227
  this.Log.debug('ble scan manager: ', blePlxManager);
220
228
  this.Log.debug('ble scan error: ', error);
221
- if ([BleErrorCode.BluetoothPoweredOff, BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
222
- reject(ERRORS.TypedError(HardwareErrorCode.BlePermissionError));
229
+ if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) {
230
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError));
223
231
  }
224
- else if (error.errorCode === BleErrorCode.BluetoothUnauthorized) {
225
- reject(ERRORS.TypedError(HardwareErrorCode.BleLocationError));
232
+ else if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothUnauthorized) {
233
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationError));
226
234
  }
227
- else if (error.errorCode === BleErrorCode.ScanStartFailed) {
235
+ else if (error.errorCode === reactNativeBlePlx.BleErrorCode.ScanStartFailed) {
228
236
  timer.timeout(() => { }, this.scanTimeout);
229
237
  }
230
238
  else {
231
- reject(ERRORS.TypedError(HardwareErrorCode.BleScanError, (_a = error.reason) !== null && _a !== void 0 ? _a : ''));
239
+ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleScanError, (_a = error.reason) !== null && _a !== void 0 ? _a : ''));
232
240
  }
233
241
  return;
234
242
  }
@@ -263,7 +271,7 @@ class ReactNativeBleTransport {
263
271
  return __awaiter(this, void 0, void 0, function* () {
264
272
  const { uuid } = input;
265
273
  if (!uuid) {
266
- throw ERRORS.TypedError(HardwareErrorCode.BleRequiredUUID);
274
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleRequiredUUID);
267
275
  }
268
276
  let device = null;
269
277
  if (transportCache[uuid]) {
@@ -295,23 +303,23 @@ class ReactNativeBleTransport {
295
303
  }
296
304
  catch (e) {
297
305
  this.Log.debug('try to connect to device has error: ', e);
298
- if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed ||
299
- e.errorCode === BleErrorCode.OperationCancelled) {
306
+ if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed ||
307
+ e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) {
300
308
  connectOptions = {};
301
309
  this.Log.debug('first try to reconnect without params');
302
310
  device = yield blePlxManager.connectToDevice(uuid);
303
311
  }
304
- else if (e.errorCode === BleErrorCode.DeviceAlreadyConnected) {
312
+ else if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceAlreadyConnected) {
305
313
  this.Log.debug('device already connected');
306
- throw ERRORS.TypedError(HardwareErrorCode.BleAlreadyConnected);
314
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleAlreadyConnected);
307
315
  }
308
316
  else {
309
- throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, (_a = e.reason) !== null && _a !== void 0 ? _a : e);
317
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_a = e.reason) !== null && _a !== void 0 ? _a : e);
310
318
  }
311
319
  }
312
320
  }
313
321
  if (!device) {
314
- throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, 'unable to connect to device');
322
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, 'unable to connect to device');
315
323
  }
316
324
  if (!(yield device.isConnected())) {
317
325
  this.Log.debug('not connected, try to connect to device: ', uuid);
@@ -320,8 +328,8 @@ class ReactNativeBleTransport {
320
328
  }
321
329
  catch (e) {
322
330
  this.Log.debug('not connected, try to connect to device has error: ', e);
323
- if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed ||
324
- e.errorCode === BleErrorCode.OperationCancelled) {
331
+ if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed ||
332
+ e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) {
325
333
  connectOptions = {};
326
334
  this.Log.debug('second try to reconnect without params');
327
335
  try {
@@ -329,7 +337,7 @@ class ReactNativeBleTransport {
329
337
  }
330
338
  catch (e) {
331
339
  this.Log.debug('last try to reconnect error: ', e);
332
- if (e.errorCode === BleErrorCode.OperationCancelled) {
340
+ if (e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) {
333
341
  this.Log.debug('last try to reconnect');
334
342
  yield device.cancelConnection();
335
343
  yield device.connect();
@@ -337,15 +345,15 @@ class ReactNativeBleTransport {
337
345
  }
338
346
  }
339
347
  else {
340
- throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, (_b = e.reason) !== null && _b !== void 0 ? _b : e);
348
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_b = e.reason) !== null && _b !== void 0 ? _b : e);
341
349
  }
342
350
  }
343
351
  }
344
- if (Platform.OS === 'android') {
352
+ if (reactNative.Platform.OS === 'android') {
345
353
  const bondedDevices = yield getBondedDevices();
346
354
  const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === (device === null || device === void 0 ? void 0 : device.id));
347
355
  if (!hasBonded) {
348
- throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded');
356
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded');
349
357
  }
350
358
  }
351
359
  yield device.discoverAllServicesAndCharacteristics();
@@ -371,14 +379,14 @@ class ReactNativeBleTransport {
371
379
  catch (e) {
372
380
  this.Log.debug('cancel connection error when service not found: ', e.message || e.reason);
373
381
  }
374
- throw ERRORS.TypedError(HardwareErrorCode.BleServiceNotFound);
382
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleServiceNotFound);
375
383
  }
376
384
  const { serviceUuid, writeUuid, notifyUuid } = infos;
377
385
  if (!characteristics) {
378
386
  characteristics = yield device.characteristicsForService(serviceUuid);
379
387
  }
380
388
  if (!characteristics) {
381
- throw ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotFound);
389
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotFound);
382
390
  }
383
391
  let writeCharacteristic;
384
392
  let notifyCharacteristic;
@@ -391,16 +399,16 @@ class ReactNativeBleTransport {
391
399
  }
392
400
  }
393
401
  if (!writeCharacteristic) {
394
- throw ERRORS.TypedError('BLECharacteristicNotFound: write characteristic not found');
402
+ throw hdShared.ERRORS.TypedError('BLECharacteristicNotFound: write characteristic not found');
395
403
  }
396
404
  if (!notifyCharacteristic) {
397
- throw ERRORS.TypedError('BLECharacteristicNotFound: notify characteristic not found');
405
+ throw hdShared.ERRORS.TypedError('BLECharacteristicNotFound: notify characteristic not found');
398
406
  }
399
407
  if (!writeCharacteristic.isWritableWithResponse) {
400
- throw ERRORS.TypedError('BLECharacteristicNotWritable: write characteristic not writable');
408
+ throw hdShared.ERRORS.TypedError('BLECharacteristicNotWritable: write characteristic not writable');
401
409
  }
402
410
  if (!notifyCharacteristic.isNotifiable) {
403
- throw ERRORS.TypedError('BLECharacteristicNotNotifiable: notify characteristic not notifiable');
411
+ throw hdShared.ERRORS.TypedError('BLECharacteristicNotNotifiable: notify characteristic not notifiable');
404
412
  }
405
413
  const transport = new BleTransport(device, writeCharacteristic, notifyCharacteristic);
406
414
  transport.nofitySubscription = this._monitorCharacteristic(transport.notifyCharacteristic);
@@ -426,33 +434,33 @@ class ReactNativeBleTransport {
426
434
  }
427
435
  _monitorCharacteristic(characteristic) {
428
436
  let bufferLength = 0;
429
- let buffer = [];
437
+ let buffer$1 = [];
430
438
  const subscription = characteristic.monitor((error, c) => {
431
439
  var _a, _b, _c;
432
440
  if (error) {
433
441
  this.Log.debug(`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${error}`);
434
442
  if (this.runPromise) {
435
- this.runPromise.reject(ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotifyError, (_a = error.reason) !== null && _a !== void 0 ? _a : error.message));
443
+ this.runPromise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyError, (_a = error.reason) !== null && _a !== void 0 ? _a : error.message));
436
444
  this.Log.debug(': monitor notify error, and has unreleased Promise');
437
445
  }
438
446
  return;
439
447
  }
440
448
  if (!c) {
441
- throw ERRORS.TypedError(HardwareErrorCode.BleMonitorError);
449
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleMonitorError);
442
450
  }
443
451
  try {
444
- const data = Buffer.from(c.value, 'base64');
452
+ const data = buffer.Buffer.from(c.value, 'base64');
445
453
  if (isHeaderChunk(data)) {
446
454
  bufferLength = data.readInt32BE(5);
447
- buffer = [...data.subarray(3)];
455
+ buffer$1 = [...data.subarray(3)];
448
456
  }
449
457
  else {
450
- buffer = buffer.concat([...data]);
458
+ buffer$1 = buffer$1.concat([...data]);
451
459
  }
452
- if (buffer.length - COMMON_HEADER_SIZE >= bufferLength) {
453
- const value = Buffer.from(buffer);
460
+ if (buffer$1.length - transport.COMMON_HEADER_SIZE >= bufferLength) {
461
+ const value = buffer.Buffer.from(buffer$1);
454
462
  bufferLength = 0;
455
- buffer = [];
463
+ buffer$1 = [];
456
464
  (_b = this.runPromise) === null || _b === void 0 ? void 0 : _b.resolve(value.toString('hex'));
457
465
  }
458
466
  }
@@ -473,7 +481,7 @@ class ReactNativeBleTransport {
473
481
  if (transport) {
474
482
  delete transportCache[uuid];
475
483
  (_a = transport.nofitySubscription) === null || _a === void 0 ? void 0 : _a.call(transport);
476
- if (Platform.OS === 'android') ;
484
+ if (reactNative.Platform.OS === 'android') ;
477
485
  }
478
486
  return Promise.resolve(true);
479
487
  });
@@ -481,26 +489,26 @@ class ReactNativeBleTransport {
481
489
  call(uuid, name, data) {
482
490
  return __awaiter(this, void 0, void 0, function* () {
483
491
  if (this.stopped) {
484
- return Promise.reject(ERRORS.TypedError('Transport stopped.'));
492
+ return Promise.reject(hdShared.ERRORS.TypedError('Transport stopped.'));
485
493
  }
486
494
  if (this._messages == null) {
487
- throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured);
495
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured);
488
496
  }
489
497
  if (this.runPromise) {
490
- throw ERRORS.TypedError(HardwareErrorCode.TransportCallInProgress);
498
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportCallInProgress);
491
499
  }
492
500
  const transport = transportCache[uuid];
493
501
  if (!transport) {
494
- throw ERRORS.TypedError(HardwareErrorCode.TransportNotFound);
502
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotFound);
495
503
  }
496
- this.runPromise = createDeferred();
504
+ this.runPromise = hdShared.createDeferred();
497
505
  const messages = this._messages;
498
506
  this.Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
499
507
  const buffers = buildBuffers(messages, name, data);
500
508
  if (name === 'FirmwareUpload') {
501
- const packetCapacity = Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH;
509
+ const packetCapacity = reactNative.Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH;
502
510
  let index = 0;
503
- let chunk = ByteBuffer.allocate(packetCapacity);
511
+ let chunk = ByteBuffer__default["default"].allocate(packetCapacity);
504
512
  while (index < buffers.length) {
505
513
  const buffer = buffers[index].toBuffer();
506
514
  chunk.append(buffer);
@@ -509,7 +517,7 @@ class ReactNativeBleTransport {
509
517
  chunk.reset();
510
518
  try {
511
519
  yield transport.writeCharacteristic.writeWithoutResponse(chunk.toString('base64'));
512
- chunk = ByteBuffer.allocate(packetCapacity);
520
+ chunk = ByteBuffer__default["default"].allocate(packetCapacity);
513
521
  }
514
522
  catch (e) {
515
523
  this.runPromise = null;
@@ -529,11 +537,11 @@ class ReactNativeBleTransport {
529
537
  catch (e) {
530
538
  this.Log.debug('writeCharacteristic write error: ', e);
531
539
  this.runPromise = null;
532
- if (e.errorCode === BleErrorCode.DeviceDisconnected) {
533
- throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded);
540
+ if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceDisconnected) {
541
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded);
534
542
  }
535
- if (e.errorCode === BleErrorCode.OperationStartFailed) {
536
- throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError, e.reason);
543
+ if (e.errorCode === reactNativeBlePlx.BleErrorCode.OperationStartFailed) {
544
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleWriteCharacteristicError, e.reason);
537
545
  }
538
546
  return;
539
547
  }
@@ -567,4 +575,4 @@ class ReactNativeBleTransport {
567
575
  }
568
576
  }
569
577
 
570
- export { ReactNativeBleTransport as default };
578
+ module.exports = ReactNativeBleTransport;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "0.1.44",
3
+ "version": "0.1.45",
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": "^0.1.44",
23
- "@onekeyfe/hd-transport": "^0.1.44",
22
+ "@onekeyfe/hd-shared": "^0.1.45",
23
+ "@onekeyfe/hd-transport": "^0.1.45",
24
24
  "react-native-ble-manager": "^8.1.0",
25
25
  "react-native-ble-plx": "^2.0.3"
26
26
  },
27
- "gitHead": "fb673918587877267cbbeca6e0c6abbc56a0d0fa"
27
+ "gitHead": "0436f8ff26494933124c1c6f4586d4aeb5526c30"
28
28
  }