@onekeyfe/hd-transport-react-native
Advanced tools
Comparing version 0.1.40 to 0.1.41
@@ -1,17 +0,9 @@ | ||
'use strict'; | ||
import { Platform } from 'react-native'; | ||
import { Buffer } from 'buffer'; | ||
import { BleManager as BleManager$1, BleErrorCode, ScanMode } from 'react-native-ble-plx'; | ||
import ByteBuffer from 'bytebuffer'; | ||
import transport, { MESSAGE_TOP_CHAR, MESSAGE_HEADER_BYTE, COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport'; | ||
import { ERRORS, HardwareErrorCode, createDeferred } from '@onekeyfe/hd-shared'; | ||
import BleManager from 'react-native-ble-manager'; | ||
var reactNative = require('react-native'); | ||
var buffer = require('buffer'); | ||
var reactNativeBlePlx = require('react-native-ble-plx'); | ||
var ByteBuffer = require('bytebuffer'); | ||
var transport = require('@onekeyfe/hd-transport'); | ||
var hdShared = require('@onekeyfe/hd-shared'); | ||
var BleManager = require('react-native-ble-manager'); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var ByteBuffer__default = /*#__PURE__*/_interopDefaultLegacy(ByteBuffer); | ||
var transport__default = /*#__PURE__*/_interopDefaultLegacy(transport); | ||
var BleManager__default = /*#__PURE__*/_interopDefaultLegacy(BleManager); | ||
/****************************************************************************** | ||
@@ -43,6 +35,6 @@ Copyright (c) Microsoft Corporation. | ||
const initializeBleManager = () => { | ||
BleManager__default["default"].start({ showAlert: false }); | ||
BleManager.start({ showAlert: false }); | ||
}; | ||
const getConnectedDeviceIds = (serviceUuids) => __awaiter(void 0, void 0, void 0, function* () { | ||
const connectedPeripherals = yield BleManager__default["default"].getConnectedPeripherals(serviceUuids); | ||
const connectedPeripherals = yield BleManager.getConnectedPeripherals(serviceUuids); | ||
return connectedPeripherals.map(peripheral => { | ||
@@ -54,3 +46,3 @@ const { id, name, advertising = {} } = peripheral; | ||
const getBondedDevices = () => __awaiter(void 0, void 0, void 0, function* () { | ||
const peripherals = yield BleManager__default["default"].getBondedPeripherals(); | ||
const peripherals = yield BleManager.getBondedPeripherals(); | ||
return peripherals.map(peripheral => { | ||
@@ -101,3 +93,3 @@ const { id, name, advertising = {} } = peripheral; | ||
subscription.remove(); | ||
reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError)); | ||
reject(ERRORS.TypedError(HardwareErrorCode.BlePermissionError)); | ||
}, ms); | ||
@@ -151,5 +143,5 @@ }); | ||
const [MagicQuestionMark, sharp1, sharp2] = chunk; | ||
if (String.fromCharCode(MagicQuestionMark) === String.fromCharCode(transport.MESSAGE_TOP_CHAR) && | ||
String.fromCharCode(sharp1) === String.fromCharCode(transport.MESSAGE_HEADER_BYTE) && | ||
String.fromCharCode(sharp2) === String.fromCharCode(transport.MESSAGE_HEADER_BYTE)) { | ||
if (String.fromCharCode(MagicQuestionMark) === String.fromCharCode(MESSAGE_TOP_CHAR) && | ||
String.fromCharCode(sharp1) === String.fromCharCode(MESSAGE_HEADER_BYTE) && | ||
String.fromCharCode(sharp2) === String.fromCharCode(MESSAGE_HEADER_BYTE)) { | ||
return true; | ||
@@ -171,3 +163,3 @@ } | ||
const { check, buildBuffers, receiveOne, parseConfigure } = transport__default["default"]; | ||
const { check, buildBuffers, receiveOne, parseConfigure } = transport; | ||
const transportCache = {}; | ||
@@ -210,3 +202,3 @@ let connectOptions = { | ||
return Promise.resolve(this.blePlxManager); | ||
this.blePlxManager = new reactNativeBlePlx.BleManager(); | ||
this.blePlxManager = new BleManager$1(); | ||
initializeBleManager(); | ||
@@ -229,3 +221,3 @@ return Promise.resolve(this.blePlxManager); | ||
blePlxManager.startDeviceScan(null, { | ||
scanMode: reactNativeBlePlx.ScanMode.LowLatency, | ||
scanMode: ScanMode.LowLatency, | ||
}, (error, device) => { | ||
@@ -236,13 +228,13 @@ var _a, _b; | ||
this.Log.debug('ble scan error: ', error); | ||
if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) { | ||
reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError)); | ||
if ([BleErrorCode.BluetoothPoweredOff, BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) { | ||
reject(ERRORS.TypedError(HardwareErrorCode.BlePermissionError)); | ||
} | ||
else if (error.errorCode === reactNativeBlePlx.BleErrorCode.BluetoothUnauthorized) { | ||
reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleLocationError)); | ||
else if (error.errorCode === BleErrorCode.BluetoothUnauthorized) { | ||
reject(ERRORS.TypedError(HardwareErrorCode.BleLocationError)); | ||
} | ||
else if (error.errorCode === reactNativeBlePlx.BleErrorCode.ScanStartFailed) { | ||
else if (error.errorCode === BleErrorCode.ScanStartFailed) { | ||
timer.timeout(() => { }, this.scanTimeout); | ||
} | ||
else { | ||
reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleScanError, (_a = error.reason) !== null && _a !== void 0 ? _a : '')); | ||
reject(ERRORS.TypedError(HardwareErrorCode.BleScanError, (_a = error.reason) !== null && _a !== void 0 ? _a : '')); | ||
} | ||
@@ -282,3 +274,3 @@ return; | ||
if (!uuid) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleRequiredUUID); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleRequiredUUID); | ||
} | ||
@@ -315,4 +307,4 @@ let device = null; | ||
this.Log.debug('try to connect to device has error: ', e); | ||
if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed || | ||
e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) { | ||
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed || | ||
e.errorCode === BleErrorCode.OperationCancelled) { | ||
connectOptions = {}; | ||
@@ -322,8 +314,8 @@ this.Log.debug('first try to reconnect without params'); | ||
} | ||
else if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceAlreadyConnected) { | ||
else if (e.errorCode === BleErrorCode.DeviceAlreadyConnected) { | ||
this.Log.debug('device already connected'); | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleAlreadyConnected); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleAlreadyConnected); | ||
} | ||
else { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_a = e.reason) !== null && _a !== void 0 ? _a : e); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, (_a = e.reason) !== null && _a !== void 0 ? _a : e); | ||
} | ||
@@ -333,3 +325,3 @@ } | ||
if (!device) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, 'unable to connect to device'); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, 'unable to connect to device'); | ||
} | ||
@@ -343,4 +335,4 @@ if (!(yield device.isConnected())) { | ||
this.Log.debug('not connected, try to connect to device has error: ', e); | ||
if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed || | ||
e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) { | ||
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed || | ||
e.errorCode === BleErrorCode.OperationCancelled) { | ||
connectOptions = {}; | ||
@@ -353,3 +345,3 @@ this.Log.debug('second try to reconnect without params'); | ||
this.Log.debug('last try to reconnect error: ', e); | ||
if (e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) { | ||
if (e.errorCode === BleErrorCode.OperationCancelled) { | ||
this.Log.debug('last try to reconnect'); | ||
@@ -362,11 +354,11 @@ yield device.cancelConnection(); | ||
else { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleConnectedError, (_b = e.reason) !== null && _b !== void 0 ? _b : e); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleConnectedError, (_b = e.reason) !== null && _b !== void 0 ? _b : e); | ||
} | ||
} | ||
} | ||
if (reactNative.Platform.OS === 'android') { | ||
if (Platform.OS === 'android') { | ||
const bondedDevices = yield getBondedDevices(); | ||
const hasBonded = !!bondedDevices.find(bondedDevice => bondedDevice.id === (device === null || device === void 0 ? void 0 : device.id)); | ||
if (!hasBonded) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded'); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded, 'device is not bonded'); | ||
} | ||
@@ -397,3 +389,3 @@ } | ||
} | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleServiceNotFound); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleServiceNotFound); | ||
} | ||
@@ -405,3 +397,3 @@ const { serviceUuid, writeUuid, notifyUuid } = infos; | ||
if (!characteristics) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotFound); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotFound); | ||
} | ||
@@ -419,12 +411,12 @@ let writeCharacteristic; | ||
if (!writeCharacteristic) { | ||
throw hdShared.ERRORS.TypedError('BLECharacteristicNotFound: write characteristic not found'); | ||
throw ERRORS.TypedError('BLECharacteristicNotFound: write characteristic not found'); | ||
} | ||
if (!notifyCharacteristic) { | ||
throw hdShared.ERRORS.TypedError('BLECharacteristicNotFound: notify characteristic not found'); | ||
throw ERRORS.TypedError('BLECharacteristicNotFound: notify characteristic not found'); | ||
} | ||
if (!writeCharacteristic.isWritableWithResponse) { | ||
throw hdShared.ERRORS.TypedError('BLECharacteristicNotWritable: write characteristic not writable'); | ||
throw ERRORS.TypedError('BLECharacteristicNotWritable: write characteristic not writable'); | ||
} | ||
if (!notifyCharacteristic.isNotifiable) { | ||
throw hdShared.ERRORS.TypedError('BLECharacteristicNotNotifiable: notify characteristic not notifiable'); | ||
throw ERRORS.TypedError('BLECharacteristicNotNotifiable: notify characteristic not notifiable'); | ||
} | ||
@@ -455,3 +447,3 @@ const transport = new BleTransport(device, writeCharacteristic, notifyCharacteristic); | ||
let bufferLength = 0; | ||
let buffer$1 = []; | ||
let buffer = []; | ||
const subscription = characteristic.monitor((error, c) => { | ||
@@ -462,3 +454,3 @@ var _a, _b, _c; | ||
if (this.runPromise) { | ||
this.runPromise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyError, (_a = error.reason) !== null && _a !== void 0 ? _a : error.message)); | ||
this.runPromise.reject(ERRORS.TypedError(HardwareErrorCode.BleCharacteristicNotifyError, (_a = error.reason) !== null && _a !== void 0 ? _a : error.message)); | ||
this.Log.debug(': monitor notify error, and has unreleased Promise'); | ||
@@ -469,17 +461,17 @@ } | ||
if (!c) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleMonitorError); | ||
throw ERRORS.TypedError(HardwareErrorCode.BleMonitorError); | ||
} | ||
try { | ||
const data = buffer.Buffer.from(c.value, 'base64'); | ||
const data = Buffer.from(c.value, 'base64'); | ||
if (isHeaderChunk(data)) { | ||
bufferLength = data.readInt32BE(5); | ||
buffer$1 = [...data.subarray(3)]; | ||
buffer = [...data.subarray(3)]; | ||
} | ||
else { | ||
buffer$1 = buffer$1.concat([...data]); | ||
buffer = buffer.concat([...data]); | ||
} | ||
if (buffer$1.length - transport.COMMON_HEADER_SIZE >= bufferLength) { | ||
const value = buffer.Buffer.from(buffer$1); | ||
if (buffer.length - COMMON_HEADER_SIZE >= bufferLength) { | ||
const value = Buffer.from(buffer); | ||
bufferLength = 0; | ||
buffer$1 = []; | ||
buffer = []; | ||
(_b = this.runPromise) === null || _b === void 0 ? void 0 : _b.resolve(value.toString('hex')); | ||
@@ -505,3 +497,3 @@ } | ||
(_a = transport.nofitySubscription) === null || _a === void 0 ? void 0 : _a.call(transport); | ||
if (reactNative.Platform.OS === 'android') ; | ||
if (Platform.OS === 'android') ; | ||
} | ||
@@ -514,15 +506,15 @@ return Promise.resolve(true); | ||
if (this.stopped) { | ||
return Promise.reject(hdShared.ERRORS.TypedError('Transport stopped.')); | ||
return Promise.reject(ERRORS.TypedError('Transport stopped.')); | ||
} | ||
if (this._messages == null) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotConfigured); | ||
throw ERRORS.TypedError(HardwareErrorCode.TransportNotConfigured); | ||
} | ||
if (this.runPromise) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportCallInProgress); | ||
throw ERRORS.TypedError(HardwareErrorCode.TransportCallInProgress); | ||
} | ||
const transport = transportCache[uuid]; | ||
if (!transport) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.TransportNotFound); | ||
throw ERRORS.TypedError(HardwareErrorCode.TransportNotFound); | ||
} | ||
this.runPromise = hdShared.createDeferred(); | ||
this.runPromise = createDeferred(); | ||
const messages = this._messages; | ||
@@ -532,5 +524,5 @@ this.Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data); | ||
if (name === 'FirmwareUpload') { | ||
const packetCapacity = reactNative.Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH; | ||
const packetCapacity = Platform.OS === 'ios' ? IOS_PACKET_LENGTH : ANDROID_PACKET_LENGTH; | ||
let index = 0; | ||
let chunk = ByteBuffer__default["default"].allocate(packetCapacity); | ||
let chunk = ByteBuffer.allocate(packetCapacity); | ||
while (index < buffers.length) { | ||
@@ -544,3 +536,3 @@ const buffer = buffers[index].toBuffer(); | ||
yield transport.writeCharacteristic.writeWithoutResponse(chunk.toString('base64')); | ||
chunk = ByteBuffer__default["default"].allocate(packetCapacity); | ||
chunk = ByteBuffer.allocate(packetCapacity); | ||
} | ||
@@ -565,7 +557,7 @@ catch (e) { | ||
this.runPromise = null; | ||
if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceDisconnected) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleDeviceNotBonded); | ||
if (e.errorCode === BleErrorCode.DeviceDisconnected) { | ||
throw ERRORS.TypedError(HardwareErrorCode.BleDeviceNotBonded); | ||
} | ||
if (e.errorCode === reactNativeBlePlx.BleErrorCode.OperationStartFailed) { | ||
throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleWriteCharacteristicError, e.reason); | ||
if (e.errorCode === BleErrorCode.OperationStartFailed) { | ||
throw ERRORS.TypedError(HardwareErrorCode.BleWriteCharacteristicError, e.reason); | ||
} | ||
@@ -604,2 +596,2 @@ return; | ||
module.exports = ReactNativeBleTransport; | ||
export { ReactNativeBleTransport as default }; |
{ | ||
"name": "@onekeyfe/hd-transport-react-native", | ||
"version": "0.1.40", | ||
"version": "0.1.41", | ||
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme", | ||
@@ -16,3 +16,2 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "echo \"Error: run tests from root\" && exit 1", | ||
"dev": "rimraf dist && rollup -c ../../build/rollup.config.js -w", | ||
@@ -24,8 +23,8 @@ "build": "rimraf dist && rollup -c ../../build/rollup.config.js", | ||
"dependencies": { | ||
"@onekeyfe/hd-shared": "^0.1.40", | ||
"@onekeyfe/hd-transport": "^0.1.40", | ||
"@onekeyfe/hd-shared": "^0.1.41", | ||
"@onekeyfe/hd-transport": "^0.1.41", | ||
"react-native-ble-manager": "^8.1.0", | ||
"react-native-ble-plx": "^2.0.3" | ||
}, | ||
"gitHead": "70962d6f531e16695ad953c9a6c700876a94e20d" | ||
"gitHead": "0c5bf8f29b26ee02a5587aa716db4dfd7b17defd" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55184
1281