@onekeyfe/hd-transport-react-native 0.0.8 → 0.1.0

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"}
@@ -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,CAA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AAU,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
  /******************************************************************************
@@ -47,9 +50,18 @@ const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0
47
50
  return Object.assign({ id, name }, advertising);
48
51
  });
49
52
  });
53
+ const getBondedDevices = () => __awaiter(void 0, void 0, void 0, function* () {
54
+ const peripherals = yield BleManager__default["default"].getBondedPeripherals();
55
+ return peripherals.map(peripheral => {
56
+ const { id, name, advertising = {} } = peripheral;
57
+ return Object.assign({ id, name }, advertising);
58
+ });
59
+ });
50
60
 
51
61
  const PERMISSION_ERROR = 'Bluetooth required to be turned on';
52
62
  const LOCATION_ERROR = 'Device is not authorized to use BluetoothLE';
63
+ const IOS_PACKET_LENGTH = 128;
64
+ const ANDROID_PACKET_LENGTH = 192;
53
65
  const isOnekeyDevice = (name, id) => {
54
66
  var _a;
55
67
  if ((_a = id === null || id === void 0 ? void 0 : id.startsWith) === null || _a === void 0 ? void 0 : _a.call(id, 'MI')) {
@@ -281,7 +293,7 @@ class ReactNativeBleTransport {
281
293
  if (!uuid) {
282
294
  throw new Error('uuid is required');
283
295
  }
284
- let device;
296
+ let device = null;
285
297
  if (transportCache[uuid]) {
286
298
  console.log('@onekey/hd-ble-sdk transport not be released, will release: ', uuid);
287
299
  yield this.release(uuid);
@@ -341,6 +353,13 @@ class ReactNativeBleTransport {
341
353
  }
342
354
  }
343
355
  }
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
+ }
362
+ }
344
363
  yield device.discoverAllServicesAndCharacteristics();
345
364
  let infos = tryToGetConfiguration(device);
346
365
  let characteristics;
@@ -464,16 +483,41 @@ class ReactNativeBleTransport {
464
483
  const messages = this._messages;
465
484
  console.log('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
466
485
  const buffers = buildBuffers(messages, name, data);
467
- for (const o of buffers) {
468
- const outData = o.toString('base64');
469
- console.log('@onekey/hd-ble-sdk send hex strting: ', o.toString('hex'));
470
- try {
471
- 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
+ }
472
507
  }
473
- catch (e) {
474
- this.runPromise = null;
475
- console.log('writeCharacteristic write error: ', e);
476
- 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
+ }
477
521
  }
478
522
  }
479
523
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-transport-react-native",
3
- "version": "0.0.8",
3
+ "version": "0.1.0",
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.8",
23
+ "@onekeyfe/hd-transport": "^0.1.0",
24
24
  "react-native-ble-manager": "^8.1.0",
25
25
  "react-native-ble-plx": "^2.0.3"
26
26
  },
27
- "gitHead": "edfa30382fdf36182bc389c8ff1dd1a689096592"
27
+ "gitHead": "d50dc424865150391b7249d1010ee75dc2074a7e"
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
@@ -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,7 +8,8 @@ import {
7
8
  Characteristic,
8
9
  ScanMode,
9
10
  } from 'react-native-ble-plx';
10
- import { initializeBleManager, getConnectedDeviceIds } from './BleManager';
11
+ import ByteBuffer from 'bytebuffer';
12
+ import { initializeBleManager, getConnectedDeviceIds, getBondedDevices } from './BleManager';
11
13
  import { subscribeBleOn } from './subscribeBleOn';
12
14
  import {
13
15
  PERMISSION_ERROR,
@@ -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';
@@ -155,7 +159,7 @@ export default class ReactNativeBleTransport {
155
159
  throw new Error('uuid is required');
156
160
  }
157
161
 
158
- let device;
162
+ let device: Device | null = null;
159
163
 
160
164
  if (transportCache[uuid]) {
161
165
  /**
@@ -227,6 +231,15 @@ export default class ReactNativeBleTransport {
227
231
  }
228
232
  }
229
233
 
234
+ // check device is 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
+ }
241
+ }
242
+
230
243
  await device.discoverAllServicesAndCharacteristics();
231
244
  let infos = tryToGetConfiguration(device);
232
245
  let characteristics;
@@ -378,17 +391,42 @@ export default class ReactNativeBleTransport {
378
391
  const messages = this._messages;
379
392
  console.log('transport-react-native', 'call-', ' name: ', name, ' data: ', data);
380
393
  const buffers = buildBuffers(messages, name, data);
381
- for (const o of buffers) {
382
- const outData = o.toString('base64');
383
- console.log('@onekey/hd-ble-sdk send hex strting: ', o.toString('hex'));
384
- try {
385
- await transport.writeCharacteristic.writeWithResponse(outData);
386
- } catch (e) {
387
- this.runPromise = null;
388
- console.log('writeCharacteristic write error: ', e);
389
- 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
+ }
390
427
  }
391
428
  }
429
+
392
430
  try {
393
431
  const response = await this.runPromise.promise;
394
432