@onekeyfe/hd-transport-react-native
Advanced tools
Comparing version 0.1.14 to 0.1.16
@@ -19,4 +19,5 @@ import { BleManager, Device, Characteristic } from 'react-native-ble-plx'; | ||
runPromise: Deferred<any> | null; | ||
Log?: any; | ||
constructor(options: TransportOptions); | ||
init(): void; | ||
init(logger: any): void; | ||
configure(signedData: any): void; | ||
@@ -23,0 +24,0 @@ listen(): void; |
@@ -169,3 +169,2 @@ 'use strict'; | ||
const transportCache = {}; | ||
const Log = hdShared.initLog('@onekey/hd-ble-sdk'); | ||
let connectOptions = { | ||
@@ -192,5 +191,6 @@ requestMTU: 512, | ||
this.scanTimeout = (_a = options.scanTimeout) !== null && _a !== void 0 ? _a : 3000; | ||
hdShared.enableLog(true); | ||
} | ||
init() { } | ||
init(logger) { | ||
this.Log = logger; | ||
} | ||
configure(signedData) { | ||
@@ -219,3 +219,3 @@ const messages = parseConfigure(signedData); | ||
catch (error) { | ||
Log.debug('subscribeBleOn error: ', error); | ||
this.Log.debug('subscribeBleOn error: ', error); | ||
reject(error); | ||
@@ -229,4 +229,4 @@ return; | ||
if (error) { | ||
Log.debug('ble scan manager: ', blePlxManager); | ||
Log.debug('ble scan error: ', error); | ||
this.Log.debug('ble scan manager: ', blePlxManager); | ||
this.Log.debug('ble scan error: ', error); | ||
if ([reactNativeBlePlx.BleErrorCode.BluetoothPoweredOff, reactNativeBlePlx.BleErrorCode.BluetoothInUnknownState].includes(error.errorCode)) { | ||
@@ -244,7 +244,7 @@ reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BlePermissionError)); | ||
if (isOnekeyDevice((_b = device === null || device === void 0 ? void 0 : device.name) !== null && _b !== void 0 ? _b : null, device === null || device === void 0 ? void 0 : device.id)) { | ||
Log.debug('search device start ======================'); | ||
this.Log.debug('search device start ======================'); | ||
const { name, localName, id } = device !== null && device !== void 0 ? device : {}; | ||
Log.debug(`device name: ${name !== null && name !== void 0 ? name : ''}\nlocalName: ${localName !== null && localName !== void 0 ? localName : ''}\nid: ${id !== null && id !== void 0 ? id : ''}`); | ||
this.Log.debug(`device name: ${name !== null && name !== void 0 ? name : ''}\nlocalName: ${localName !== null && localName !== void 0 ? localName : ''}\nid: ${id !== null && id !== void 0 ? id : ''}`); | ||
addDevice(device); | ||
Log.debug('search device end ======================\n'); | ||
this.Log.debug('search device end ======================\n'); | ||
} | ||
@@ -254,3 +254,3 @@ }); | ||
for (const device of devices) { | ||
Log.debug('search connected peripheral: ', device.id); | ||
this.Log.debug('search connected peripheral: ', device.id); | ||
addDevice(device); | ||
@@ -280,3 +280,3 @@ } | ||
if (transportCache[uuid]) { | ||
Log.debug('transport not be released, will release: ', uuid); | ||
this.Log.debug('transport not be released, will release: ', uuid); | ||
yield this.release(uuid); | ||
@@ -289,3 +289,3 @@ } | ||
catch (error) { | ||
Log.debug('subscribeBleOn error: ', error); | ||
this.Log.debug('subscribeBleOn error: ', error); | ||
throw error; | ||
@@ -300,7 +300,7 @@ } | ||
const deviceFilter = connectedDevice.filter(device => device.id === uuid); | ||
Log.debug(`found connected device count: ${deviceFilter.length}`); | ||
this.Log.debug(`found connected device count: ${deviceFilter.length}`); | ||
[device] = deviceFilter; | ||
} | ||
if (!device) { | ||
Log.debug('try to connect to device: ', uuid); | ||
this.Log.debug('try to connect to device: ', uuid); | ||
try { | ||
@@ -310,3 +310,3 @@ device = yield blePlxManager.connectToDevice(uuid, connectOptions); | ||
catch (e) { | ||
Log.debug('try to connect to device has error: ', e); | ||
this.Log.debug('try to connect to device has error: ', e); | ||
if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed || | ||
@@ -326,3 +326,3 @@ e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) { | ||
if (!(yield device.isConnected())) { | ||
Log.debug('not connected, try to connect to device: ', uuid); | ||
this.Log.debug('not connected, try to connect to device: ', uuid); | ||
try { | ||
@@ -332,3 +332,3 @@ yield device.connect(connectOptions); | ||
catch (e) { | ||
Log.debug('try to connect to device has error: ', e); | ||
this.Log.debug('try to connect to device has error: ', e); | ||
if (e.errorCode === reactNativeBlePlx.BleErrorCode.DeviceMTUChangeFailed || | ||
@@ -362,3 +362,3 @@ e.errorCode === reactNativeBlePlx.BleErrorCode.OperationCancelled) { | ||
catch (e) { | ||
Log.error(e); | ||
this.Log.error(e); | ||
} | ||
@@ -403,3 +403,3 @@ } | ||
const disconnectSubscription = device.onDisconnected(() => { | ||
Log.debug('device disconnect: ', device === null || device === void 0 ? void 0 : device.id); | ||
this.Log.debug('device disconnect: ', device === null || device === void 0 ? void 0 : device.id); | ||
this.release(uuid); | ||
@@ -417,6 +417,6 @@ disconnectSubscription === null || disconnectSubscription === void 0 ? void 0 : disconnectSubscription.remove(); | ||
if (error) { | ||
Log.debug(`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${error}`); | ||
this.Log.debug(`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${error}`); | ||
if (this.runPromise) { | ||
this.runPromise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.BleCharacteristicNotifyError, (_a = error.reason) !== null && _a !== void 0 ? _a : error.message)); | ||
Log.debug(': monitor notify error, and has unreleased Promise'); | ||
this.Log.debug(': monitor notify error, and has unreleased Promise'); | ||
} | ||
@@ -445,3 +445,3 @@ return; | ||
catch (error) { | ||
Log.debug('monitor data error: ', error); | ||
this.Log.debug('monitor data error: ', error); | ||
(_c = this.runPromise) === null || _c === void 0 ? void 0 : _c.reject(error); | ||
@@ -451,3 +451,3 @@ } | ||
return () => { | ||
Log.debug('remove characteristic monitor: ', characteristic.uuid); | ||
this.Log.debug('remove characteristic monitor: ', characteristic.uuid); | ||
subscription.remove(); | ||
@@ -487,3 +487,3 @@ }; | ||
const messages = this._messages; | ||
Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data); | ||
this.Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data); | ||
const buffers = buildBuffers(messages, name, data); | ||
@@ -500,3 +500,3 @@ if (name === 'FirmwareUpload') { | ||
chunk.reset(); | ||
Log.debug('send more packet hex strting: ', chunk.toString('hex')); | ||
this.Log.debug('send more packet hex strting: ', chunk.toString('hex')); | ||
try { | ||
@@ -508,3 +508,3 @@ yield transport.writeCharacteristic.writeWithoutResponse(chunk.toString('base64')); | ||
this.runPromise = null; | ||
Log.error('writeCharacteristic write error: ', e); | ||
this.Log.error('writeCharacteristic write error: ', e); | ||
return; | ||
@@ -518,3 +518,3 @@ } | ||
const outData = o.toString('base64'); | ||
Log.debug('send hex strting: ', o.toString('hex')); | ||
this.Log.debug('send hex strting: ', o.toString('hex')); | ||
try { | ||
@@ -528,3 +528,3 @@ yield transport.writeCharacteristic.writeWithoutResponse(outData); | ||
this.runPromise = null; | ||
Log.debug('writeCharacteristic write error: ', e); | ||
this.Log.debug('writeCharacteristic write error: ', e); | ||
return; | ||
@@ -539,3 +539,3 @@ } | ||
} | ||
Log.debug('receive data: ', response); | ||
this.Log.debug('receive data: ', response); | ||
const jsonData = receiveOne(messages, response); | ||
@@ -545,3 +545,3 @@ return check.call(jsonData); | ||
catch (e) { | ||
Log.error('call error: ', e); | ||
this.Log.error('call error: ', e); | ||
return e; | ||
@@ -558,3 +558,3 @@ } | ||
cancel() { | ||
Log.debug('transport-react-native canceled'); | ||
this.Log.debug('transport-react-native canceled'); | ||
if (this.runPromise) ; | ||
@@ -561,0 +561,0 @@ this.runPromise = null; |
{ | ||
"name": "@onekeyfe/hd-transport-react-native", | ||
"version": "0.1.14", | ||
"version": "0.1.16", | ||
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme", | ||
@@ -23,8 +23,8 @@ "license": "MIT", | ||
"dependencies": { | ||
"@onekeyfe/hd-shared": "^0.1.14", | ||
"@onekeyfe/hd-transport": "^0.1.14", | ||
"@onekeyfe/hd-shared": "^0.1.16", | ||
"@onekeyfe/hd-transport": "^0.1.16", | ||
"react-native-ble-manager": "^8.1.0", | ||
"react-native-ble-plx": "^2.0.3" | ||
}, | ||
"gitHead": "216fee1795067013da0d68f4fc25f583509bb509" | ||
"gitHead": "e87fdbab30d6daa4f32e28259bd221d75b5d6aad" | ||
} |
@@ -12,10 +12,3 @@ import { Platform } from 'react-native'; | ||
import transport, { COMMON_HEADER_SIZE } from '@onekeyfe/hd-transport'; | ||
import { | ||
createDeferred, | ||
Deferred, | ||
ERRORS, | ||
HardwareErrorCode, | ||
enableLog, | ||
initLog, | ||
} from '@onekeyfe/hd-shared'; | ||
import { createDeferred, Deferred, ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared'; | ||
import { initializeBleManager, getConnectedDeviceIds, getBondedDevices } from './BleManager'; | ||
@@ -39,4 +32,2 @@ import { subscribeBleOn } from './subscribeBleOn'; | ||
const Log = initLog('@onekey/hd-ble-sdk'); | ||
let connectOptions: Record<string, unknown> = { | ||
@@ -68,8 +59,11 @@ requestMTU: 512, | ||
Log?: any; | ||
constructor(options: TransportOptions) { | ||
this.scanTimeout = options.scanTimeout ?? 3000; | ||
enableLog(true); | ||
} | ||
init() {} | ||
init(logger: any) { | ||
this.Log = logger; | ||
} | ||
@@ -106,3 +100,3 @@ configure(signedData: any) { | ||
} catch (error) { | ||
Log.debug('subscribeBleOn error: ', error); | ||
this.Log.debug('subscribeBleOn error: ', error); | ||
reject(error); | ||
@@ -119,4 +113,4 @@ return; | ||
if (error) { | ||
Log.debug('ble scan manager: ', blePlxManager); | ||
Log.debug('ble scan error: ', error); | ||
this.Log.debug('ble scan manager: ', blePlxManager); | ||
this.Log.debug('ble scan error: ', error); | ||
if ( | ||
@@ -137,9 +131,11 @@ [BleErrorCode.BluetoothPoweredOff, BleErrorCode.BluetoothInUnknownState].includes( | ||
if (isOnekeyDevice(device?.name ?? null, device?.id)) { | ||
Log.debug('search device start ======================'); | ||
this.Log.debug('search device start ======================'); | ||
const { name, localName, id } = device ?? {}; | ||
Log.debug(`device name: ${name ?? ''}\nlocalName: ${localName ?? ''}\nid: ${id ?? ''}`); | ||
this.Log.debug( | ||
`device name: ${name ?? ''}\nlocalName: ${localName ?? ''}\nid: ${id ?? ''}` | ||
); | ||
addDevice(device as unknown as Device); | ||
Log.debug('search device end ======================\n'); | ||
this.Log.debug('search device end ======================\n'); | ||
} | ||
@@ -151,3 +147,3 @@ } | ||
for (const device of devices) { | ||
Log.debug('search connected peripheral: ', device.id); | ||
this.Log.debug('search connected peripheral: ', device.id); | ||
addDevice(device as unknown as Device); | ||
@@ -184,3 +180,3 @@ } | ||
*/ | ||
Log.debug('transport not be released, will release: ', uuid); | ||
this.Log.debug('transport not be released, will release: ', uuid); | ||
await this.release(uuid); | ||
@@ -193,3 +189,3 @@ } | ||
} catch (error) { | ||
Log.debug('subscribeBleOn error: ', error); | ||
this.Log.debug('subscribeBleOn error: ', error); | ||
throw error; | ||
@@ -206,3 +202,3 @@ } | ||
const deviceFilter = connectedDevice.filter(device => device.id === uuid); | ||
Log.debug(`found connected device count: ${deviceFilter.length}`); | ||
this.Log.debug(`found connected device count: ${deviceFilter.length}`); | ||
[device] = deviceFilter; | ||
@@ -212,7 +208,7 @@ } | ||
if (!device) { | ||
Log.debug('try to connect to device: ', uuid); | ||
this.Log.debug('try to connect to device: ', uuid); | ||
try { | ||
device = await blePlxManager.connectToDevice(uuid, connectOptions); | ||
} catch (e) { | ||
Log.debug('try to connect to device has error: ', e); | ||
this.Log.debug('try to connect to device has error: ', e); | ||
if ( | ||
@@ -235,3 +231,3 @@ e.errorCode === BleErrorCode.DeviceMTUChangeFailed || | ||
if (!(await device.isConnected())) { | ||
Log.debug('not connected, try to connect to device: ', uuid); | ||
this.Log.debug('not connected, try to connect to device: ', uuid); | ||
@@ -241,3 +237,3 @@ try { | ||
} catch (e) { | ||
Log.debug('try to connect to device has error: ', e); | ||
this.Log.debug('try to connect to device has error: ', e); | ||
if ( | ||
@@ -275,3 +271,3 @@ e.errorCode === BleErrorCode.DeviceMTUChangeFailed || | ||
} catch (e) { | ||
Log.error(e); | ||
this.Log.error(e); | ||
} | ||
@@ -328,3 +324,3 @@ } | ||
const disconnectSubscription = device.onDisconnected(() => { | ||
Log.debug('device disconnect: ', device?.id); | ||
this.Log.debug('device disconnect: ', device?.id); | ||
this.release(uuid); | ||
@@ -342,3 +338,3 @@ disconnectSubscription?.remove(); | ||
if (error) { | ||
Log.debug( | ||
this.Log.debug( | ||
`error monitor ${characteristic.uuid}, deviceId: ${characteristic.deviceID}: ${ | ||
@@ -355,3 +351,3 @@ error as unknown as string | ||
); | ||
Log.debug(': monitor notify error, and has unreleased Promise'); | ||
this.Log.debug(': monitor notify error, and has unreleased Promise'); | ||
} | ||
@@ -388,3 +384,3 @@ return; | ||
} catch (error) { | ||
Log.debug('monitor data error: ', error); | ||
this.Log.debug('monitor data error: ', error); | ||
this.runPromise?.reject(error); | ||
@@ -395,3 +391,3 @@ } | ||
return () => { | ||
Log.debug('remove characteristic monitor: ', characteristic.uuid); | ||
this.Log.debug('remove characteristic monitor: ', characteristic.uuid); | ||
subscription.remove(); | ||
@@ -435,3 +431,3 @@ }; | ||
const messages = this._messages; | ||
Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data); | ||
this.Log.debug('transport-react-native', 'call-', ' name: ', name, ' data: ', data); | ||
const buffers = buildBuffers(messages, name, data); | ||
@@ -449,3 +445,3 @@ | ||
chunk.reset(); | ||
Log.debug('send more packet hex strting: ', chunk.toString('hex')); | ||
this.Log.debug('send more packet hex strting: ', chunk.toString('hex')); | ||
try { | ||
@@ -456,3 +452,3 @@ await transport.writeCharacteristic.writeWithoutResponse(chunk.toString('base64')); | ||
this.runPromise = null; | ||
Log.error('writeCharacteristic write error: ', e); | ||
this.Log.error('writeCharacteristic write error: ', e); | ||
return; | ||
@@ -465,3 +461,3 @@ } | ||
const outData = o.toString('base64'); | ||
Log.debug('send hex strting: ', o.toString('hex')); | ||
this.Log.debug('send hex strting: ', o.toString('hex')); | ||
try { | ||
@@ -474,3 +470,3 @@ await transport.writeCharacteristic.writeWithoutResponse(outData); | ||
this.runPromise = null; | ||
Log.debug('writeCharacteristic write error: ', e); | ||
this.Log.debug('writeCharacteristic write error: ', e); | ||
return; | ||
@@ -488,7 +484,7 @@ } | ||
Log.debug('receive data: ', response); | ||
this.Log.debug('receive data: ', response); | ||
const jsonData = receiveOne(messages, response); | ||
return check.call(jsonData); | ||
} catch (e) { | ||
Log.error('call error: ', e); | ||
this.Log.error('call error: ', e); | ||
return e; | ||
@@ -505,3 +501,3 @@ } finally { | ||
cancel() { | ||
Log.debug('transport-react-native canceled'); | ||
this.Log.debug('transport-react-native canceled'); | ||
if (this.runPromise) { | ||
@@ -508,0 +504,0 @@ // this.runPromise.reject(new Error('Transport_CallCanceled')); |
Sorry, the diff of this file is not supported yet
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
51250
1189