Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
static listen(observer: *) {
let bleManager;
try {
bleManager = new BleManager();
} catch (e) {
// basically for the tests to pass
console.warn(e);
return { unsubscribe: () => {} };
}
const unsubscribe = () => {
sub.remove();
bleManager.stopDeviceScan();
};
const onBleStateChange = (state: string) => {
if (state === "PoweredOn") {
bleManager.startDeviceScan(null, null, (bleError, device) => {
if (bleError) {
observer.error(bleError);
unsubscribe();
return;
import type { Device, Characteristic } from "./types";
import { sendAPDU } from "./sendAPDU";
import { receiveAPDU } from "./receiveAPDU";
import { monitorCharacteristic } from "./monitorCharacteristic";
import { awaitsBleOn } from "./awaitsBleOn";
const ServiceUuid = "d973f2e0-b19e-11e2-9e96-0800200c9a66";
const WriteCharacteristicUuid = "d973f2e2-b19e-11e2-9e96-0800200c9a66";
const NotifyCharacteristicUuid = "d973f2e1-b19e-11e2-9e96-0800200c9a66";
let connectOptions = {
requestMTU: 156,
};
const transportsCache = {};
const bleManager = new BleManager();
if (Config.BLE_LOG_LEVEL) bleManager.setLogLevel(Config.BLE_LOG_LEVEL);
/**
* react-native bluetooth BLE implementation
* @example
* import BluetoothTransport from "@ledgerhq/react-native-hw-transport-ble";
*/
export default class BluetoothTransport extends Transport {
static isSupported = (): Promise =>
Promise.resolve(typeof BleManager === "function");
/**
* TODO could add this concept in all transports
* observe event with { available: bool, type: string } // available is generic, type is specific
* an event is emit once and then listened
}
}
if (!device) {
throw new CantOpenDevice();
}
} else {
device = deviceOrId;
}
if (!(await device.isConnected())) {
log("ble-verbose", "not connected. connecting...");
try {
await device.connect(connectOptions);
} catch (e) {
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed) {
// eslint-disable-next-line require-atomic-updates
connectOptions = {};
await device.connect();
} else {
throw e;
}
}
}
await device.discoverAllServicesAndCharacteristics();
let res = retrieveInfos(device);
let characteristics;
if (!res) {
for (const uuid of getBluetoothServiceUuids()) {
try {
render() {
const { error, status, onBypassGenuine, onRetry } = this.props;
// $FlowFixMe
if (error.errorCode === BleErrorCode.LocationServicesDisabled) {
return ;
}
// $FlowFixMe
if (error.errorCode === BleErrorCode.BluetoothUnauthorized) {
return ;
}
const isPairingStatus = status === "pairing";
const isGenuineCheckStatus = status === "genuinecheck";
const url = (isPairingStatus && urls.errors.PairingFailed) || undefined;
const outerError = isPairingStatus
? new PairingFailed()
: isGenuineCheckStatus
? new GenuineCheckFailed()
: null;
return (
render() {
const { error, status, onBypassGenuine, onRetry } = this.props;
// $FlowFixMe
if (error.errorCode === BleErrorCode.LocationServicesDisabled) {
return ;
}
// $FlowFixMe
if (error.errorCode === BleErrorCode.BluetoothUnauthorized) {
return ;
}
const isPairingStatus = status === "pairing";
const isGenuineCheckStatus = status === "genuinecheck";
const url = (isPairingStatus && urls.errors.PairingFailed) || undefined;
const outerError = isPairingStatus
? new PairingFailed()
: isGenuineCheckStatus
? new GenuineCheckFailed()
constructor(props: Props) {
super(props)
this.state = {
devices: {},
}
this.ble = new BleManager()
this.ble.startDeviceScan(
[serialUUIDs.serviceUUID],
null,
(error, device) => {
if (error) console.log(error.toString())
if (device && device.name) {
this.setState({
devices: {
...this.state.devices,
[device.id]: device.name,
},
})
}
},
)
import {
CantOpenDevice,
TransportError,
DisconnectedDeviceDuringOperation
} from "@ledgerhq/errors";
import type { Device, Characteristic } from "./types";
import { monitorCharacteristic } from "./monitorCharacteristic";
import { awaitsBleOn } from "./awaitsBleOn";
import { decoratePromiseErrors, remapError } from "./remapErrors";
let connectOptions = {
requestMTU: 156
};
const transportsCache = {};
const bleManager = new BleManager();
const retrieveInfos = device => {
if (!device || !device.serviceUUIDs) return;
const [serviceUUID] = device.serviceUUIDs;
if (!serviceUUID) return;
const infos = getInfosForServiceUuid(serviceUUID);
if (!infos) return;
return infos;
};
type ReconnectionConfig = {
pairingThreshold: number,
delayAfterFirstPairing: number
};
let reconnectionConfig: ?ReconnectionConfig = {
pairingThreshold: 1000,
if (!device) {
throw new CantOpenDevice();
}
} else {
device = deviceOrId;
}
if (!(await device.isConnected())) {
logSubject.next({
type: "verbose",
message: "not connected. connecting...",
});
try {
await device.connect(connectOptions);
} catch (e) {
if (e.errorCode === BleErrorCode.DeviceMTUChangeFailed) {
connectOptions = {};
await device.connect();
} else {
throw e;
}
}
}
await device.discoverAllServicesAndCharacteristics();
const characteristics = await device.characteristicsForService(ServiceUuid);
if (!characteristics) {
throw new TransportError("service not found", "BLEServiceNotFound");
}
let writeC;
let notifyC;