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

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,5 +1,7 @@
1
1
  export declare const PERMISSION_ERROR = "Bluetooth required to be turned on";
2
2
  export declare const LOCATION_ERROR = "Device is not authorized to use BluetoothLE";
3
+ export declare const IOS_PACKET_LENGTH = 128;
4
+ export declare const ANDROID_PACKET_LENGTH = 192;
3
5
  export declare const isOnekeyDevice: (name: string | null, id?: string | undefined) => boolean;
4
6
  export declare const getBluetoothServiceUuids: () => string[];
5
7
  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,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"}
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,iBAAiB,MAAM,CAAC;AACrC,eAAO,MAAM,qBAAqB,MAAM,CAAC;AAEzC,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 +1 @@
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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,SAAiC,MAAM,wBAAwB,CAAC;AACvE,OAAO,EACL,UAAU,IAAI,aAAa,EAC3B,MAAM,EAEN,cAAc,EAEf,MAAM,sBAAsB,CAAC;AAI9B,OAAO,EACL,gBAAgB,EAChB,cAAc,EAMf,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;;;IA2JpC,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;IA4EpE,IAAI;CAGL;AAED,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -2,14 +2,17 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var reactNative = require('react-native');
5
6
  var buffer = require('buffer');
6
7
  var transport = require('@onekeyfe/hd-transport');
7
8
  var reactNativeBlePlx = require('react-native-ble-plx');
9
+ var ByteBuffer = require('bytebuffer');
8
10
  var BleManager = require('react-native-ble-manager');
9
11
 
10
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
13
 
12
14
  var transport__default = /*#__PURE__*/_interopDefaultLegacy(transport);
15
+ var ByteBuffer__default = /*#__PURE__*/_interopDefaultLegacy(ByteBuffer);
13
16
  var BleManager__default = /*#__PURE__*/_interopDefaultLegacy(BleManager);
14
17
 
15
18
  /******************************************************************************
@@ -57,6 +60,8 @@ const getBondedDevices = () => __awaiter(void 0, void 0, void 0, function* () {
57
60
 
58
61
  const PERMISSION_ERROR = 'Bluetooth required to be turned on';
59
62
  const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
63
+ const IOS_PACKET_LENGTH = 128;
64
+ const ANDROID_PACKET_LENGTH = 192;
60
65
  const isOnekeyDevice = (name, id) => {
61
66
  var _a;
62
67
  if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
@@ -348,10 +353,12 @@ class ReactNativeBleTransport {
348
353
  }
349
354
  }
350
355
  }
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');
356
+ if (reactNative.Platform.OS === 'android') {
357
+ const bondedDevices = yield getBondedDevices();
358
+ const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === (device === null || device === void 0 ? void 0 : device.id));
359
+ if (!hasBonded) {
360
+ throw new Error('device is not bonded');
361
+ }
355
362
  }
356
363
  yield device.discoverAllServicesAndCharacteristics();
357
364
  let infos = tryToGetConfiguration(device);
@@ -476,16 +483,41 @@ class ReactNativeBleTransport {
476
483
  const messages = this._messages;
477
484
  console.log('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
478
485
  const buffers = buildBuffers(messages, name, data);
479
- for (const o of buffers) {
480
- const outData = o.toString('base64');
481
- console.log('@onekey/hd-ble-sdk send hex strting: ', o.toString('hex'));
482
- try {
483
- yield transport.writeCharacteristic.writeWithResponse(outData);
486
+ if (name === 'FirmwareUpload') {
487
+ const packetCapacity = reactNative.Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH;
488
+ let index = 0;
489
+ let chunk = ByteBuffer__default["default"].allocate(packetCapacity);
490
+ while (index < buffers.length) {
491
+ const buffer = buffers[index].toBuffer();
492
+ chunk.append(buffer);
493
+ index += 1;
494
+ if (chunk.offset === packetCapacity || index >= buffers.length) {
495
+ chunk.reset();
496
+ console.log('@onekey/hd-ble-sdk send more packet hex strting: ', chunk.toString('hex'));
497
+ try {
498
+ yield transport.writeCharacteristic.writeWithoutResponse(chunk.toString('base64'));
499
+ chunk = ByteBuffer__default["default"].allocate(packetCapacity);
500
+ }
501
+ catch (e) {
502
+ this.runPromise = null;
503
+ console.log('writeCharacteristic write error: ', e);
504
+ return;
505
+ }
506
+ }
484
507
  }
485
- catch (e) {
486
- this.runPromise = null;
487
- console.log('writeCharacteristic write error: ', e);
488
- return;
508
+ }
509
+ else {
510
+ for (const o of buffers) {
511
+ const outData = o.toString('base64');
512
+ console.log('@onekey/hd-ble-sdk send hex strting: ', o.toString('hex'));
513
+ try {
514
+ yield transport.writeCharacteristic.writeWithResponse(outData);
515
+ }
516
+ catch (e) {
517
+ this.runPromise = null;
518
+ console.log('writeCharacteristic write error: ', e);
519
+ return;
520
+ }
489
521
  }
490
522
  }
491
523
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "0.0.9",
3
+ "version": "0.1.1",
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.9",
23
+ "@onekeyfe/hd-transport": "^0.1.1",
24
24
  "react-native-ble-manager": "^8.1.0",
25
25
  "react-native-ble-plx": "^2.0.3"
26
26
  },
27
- "gitHead": "728d72e4c7ddc10227fff49c3cb8d4a0ff205fac"
27
+ "gitHead": "006c48af97498aad26719d9839979ecc2c55aea6"
28
28
  }
package/src/constants.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export const PERMISSION_ERROR = 'Bluetooth required to be turned on';
2
2
  export const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
3
3
 
4
+ export const IOS_PACKET_LENGTH = 128;
5
+ export const ANDROID_PACKET_LENGTH = 192;
6
+
4
7
  export const isOnekeyDevice = (name: string | null, id?: string): boolean => {
5
8
  if (id?.startsWith?.('MI')) {
6
9
  return true;
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Platform } from 'react-native';
1
2
  import { Buffer } from 'buffer';
2
3
  import transport, { COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport';
3
4
  import {
@@ -7,6 +8,7 @@ import {
7
8
  Characteristic,
8
9
  ScanMode,
9
10
  } from 'react-native-ble-plx';
11
+ import ByteBuffer from 'bytebuffer';
10
12
  import { initializeBleManager, getConnectedDeviceIds, getBondedDevices } from './BleManager';
11
13
  import { subscribeBleOn } from './subscribeBleOn';
12
14
  import {
@@ -15,6 +17,8 @@ import {
15
17
  isOnekeyDevice,
16
18
  getBluetoothServiceUuids,
17
19
  getInfosForServiceUuid,
20
+ IOS_PACKET_LENGTH,
21
+ ANDROID_PACKET_LENGTH,
18
22
  } from './constants';
19
23
  import { Deferred, create as createDeferred } from './utils/deferred';
20
24
  import { isHeaderChunk } from './utils/validateNotify';
@@ -228,10 +232,12 @@ export default class ReactNativeBleTransport {
228
232
  }
229
233
 
230
234
  // 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
+ if (Platform.OS === 'android') {
236
+ const bondedDevices = await getBondedDevices();
237
+ const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === device?.id);
238
+ if (!hasBonded) {
239
+ throw new Error('device is not bonded');
240
+ }
235
241
  }
236
242
 
237
243
  await device.discoverAllServicesAndCharacteristics();
@@ -385,17 +391,42 @@ export default class ReactNativeBleTransport {
385
391
  const messages = this._messages;
386
392
  console.log('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
387
393
  const buffers = buildBuffers(messages, name, data);
388
- for (const o of buffers) {
389
- const outData = o.toString('base64');
390
- console.log('@onekey/hd-ble-sdk send hex strting: ', o.toString('hex'));
391
- try {
392
- await transport.writeCharacteristic.writeWithResponse(outData);
393
- } catch (e) {
394
- this.runPromise = null;
395
- console.log('writeCharacteristic write error: ', e);
396
- return;
394
+
395
+ if (name === 'FirmwareUpload') {
396
+ const packetCapacity = Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH;
397
+ let index = 0;
398
+ let chunk = ByteBuffer.allocate(packetCapacity);
399
+ while (index < buffers.length) {
400
+ const buffer = buffers[index].toBuffer();
401
+ chunk.append(buffer);
402
+ index += 1;
403
+ if (chunk.offset === packetCapacity || index >= buffers.length) {
404
+ chunk.reset();
405
+ console.log('@onekey/hd-ble-sdk send more packet hex strting: ', chunk.toString('hex'));
406
+ try {
407
+ await transport.writeCharacteristic.writeWithoutResponse(chunk.toString('base64'));
408
+ chunk = ByteBuffer.allocate(packetCapacity);
409
+ } catch (e) {
410
+ this.runPromise = null;
411
+ console.log('writeCharacteristic write error: ', e);
412
+ return;
413
+ }
414
+ }
415
+ }
416
+ } else {
417
+ for (const o of buffers) {
418
+ const outData = o.toString('base64');
419
+ console.log('@onekey/hd-ble-sdk send hex strting: ', o.toString('hex'));
420
+ try {
421
+ await transport.writeCharacteristic.writeWithResponse(outData);
422
+ } catch (e) {
423
+ this.runPromise = null;
424
+ console.log('writeCharacteristic write error: ', e);
425
+ return;
426
+ }
397
427
  }
398
428
  }
429
+
399
430
  try {
400
431
  const response = await this.runPromise.promise;
401
432