Skip to content

Commit

Permalink
🐛 (rn-ble): Extract ble plx in a static instance to fix ble connnecti…
Browse files Browse the repository at this point in the history
…vity
  • Loading branch information
jdabbech-ledger committed Jan 2, 2025
1 parent 0e6ee7d commit acea6e8
Show file tree
Hide file tree
Showing 7 changed files with 905 additions and 186 deletions.
6 changes: 6 additions & 0 deletions .changeset/clever-tables-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ledgerhq/react-native-hw-transport-ble": patch
---

Extract Ble plx instance in a specific class
Update react-native-ble-plx to 3.2.1
5 changes: 5 additions & 0 deletions .changeset/young-bottles-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": patch
---

Update react-native-ble-plx to 3.2.1
2 changes: 1 addition & 1 deletion apps/ledger-live-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"react-native-android-location-services-dialog-box": "2.8.2",
"react-native-animatable": "1.4.0",
"react-native-biometrics": "3.0.1",
"react-native-ble-plx": "3.1.2",
"react-native-ble-plx": "3.2.1",
"react-native-config": "1.5.3",
"react-native-device-info": "11.1.0",
"react-native-easy-markdown": "2.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@ledgerhq/errors": "workspace:^",
"@ledgerhq/hw-transport": "workspace:^",
"@ledgerhq/logs": "workspace:^",
"react-native-ble-plx": "3.1.2",
"react-native-ble-plx": "3.2.1",
"rxjs": "^7.8.1",
"uuid": "^9.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { BleManager as RNBleManager, LogLevel, Device, BleError } from "react-native-ble-plx";
import { awaitsBleOn } from "./awaitsBleOn";
import { getBluetoothServiceUuids } from "@ledgerhq/devices";

export class BlePlxManager {
/**
* Returns the instance of the Bluetooth Low Energy Manager. It initializes it only
* when it's first needed, preventing the permission prompt happening prematurely.
* Important: Do NOT access the _bleManager variable directly.
* Use this function instead.
* @returns {BleManager} - The instance of the BleManager.
*/
static _instance: RNBleManager;

static get instance(): RNBleManager {
if (!this._instance) {
this._instance = new RNBleManager();
}
return this._instance;
}

static waitOn() {
return awaitsBleOn(BlePlxManager.instance);
}

static async getKnownDevice(identifier: string) {
const devices = await this.instance.devices([identifier]);
return devices[0];
}

static getConnectedDevices() {
return this.instance.connectedDevices(getBluetoothServiceUuids());
}

static connect(identifier: string, options: Record<string, unknown> = {}) {
return this.instance.connectToDevice(identifier, options);
}
/**
* Exposed method from the ble-plx library
* Sets new log level for native module's logging mechanism.
* @param logLevel
*/
static async setLogLevel(logLevel: string) {
if (Object.values<string>(LogLevel).includes(logLevel)) {
await this.instance.setLogLevel(logLevel as LogLevel);
} else {
throw new Error(`${logLevel} is not a valid LogLevel`);
}
}

static onStateChange(listener: (state: any) => void, emitCurrentState?: boolean) {
return this.instance.onStateChange(listener, emitCurrentState);
}

static async startScan(callback: (error: BleError | null, device: Device | null) => void) {
await this.instance.startDeviceScan(getBluetoothServiceUuids(), null, (error, device) => {
callback(error, device);
});
}

static async stopScan() {
await this.instance.stopDeviceScan();
}

static async disconnectDevice(deviceIdentifier: string) {
await this.instance.cancelDeviceConnection(deviceIdentifier);
}

static async cancelTransaction(transactionId: string) {
await this.instance.cancelTransaction(transactionId);
}
}
Loading

0 comments on commit acea6e8

Please sign in to comment.