How to use react-native-ble-plx - 8 common examples

To help you get started, we’ve selected a few react-native-ble-plx examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LedgerHQ / ledgerjs / packages / react-native-hw-transport-ble / src / index.js View on Github external
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;
github LedgerHQ / ledger-live-mobile / src / react-native-hw-transport-ble / BleTransport.js View on Github external
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
github LedgerHQ / ledgerjs / packages / react-native-hw-transport-ble / src / BleTransport.js View on Github external
}
    }

    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 {
github LedgerHQ / ledger-live-mobile / src / screens / PairDevices / RenderError.js View on Github external
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 (
github LedgerHQ / ledger-live-mobile / src / screens / PairDevices / RenderError.js View on Github external
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()
github jolocom / smartwallet-app / src / ui / generic / bleScanner.tsx View on Github external
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,
            },
          })
        }
      },
    )
github LedgerHQ / ledgerjs / packages / react-native-hw-transport-ble / src / BleTransport.js View on Github external
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,
github LedgerHQ / ledger-live-mobile / src / react-native-hw-transport-ble / BleTransport.js View on Github external
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;