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 7, 2025
1 parent 2ea3206 commit 9b6bf19
Show file tree
Hide file tree
Showing 8 changed files with 907 additions and 188 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": minor
---

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": minor
---

Update react-native-ble-plx to 3.2.1
4 changes: 2 additions & 2 deletions apps/ledger-live-mobile/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ PODS:
- Yoga
- react-native-biometrics (3.0.1):
- React-Core
- react-native-ble-plx (3.1.2):
- react-native-ble-plx (3.2.1):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -2510,7 +2510,7 @@ SPEC CHECKSUMS:
React-Mapbuffer: b982d5bba94a8bc073bda48f0d27c9b28417fae3
React-microtasksnativemodule: 2b73e68f0462f3175f98782db08896f8501afd20
react-native-biometrics: 352e5a794bfffc46a0c86725ea7dc62deb085bdc
react-native-ble-plx: 643e9e17685a99c327c4a3564ff928bc20937653
react-native-ble-plx: 0a3f7bd84bd819bf1c32e003d083eaa3f2082ecb
react-native-config: 8f7283449bbb048902f4e764affbbf24504454af
react-native-fast-crypto: 5943c42466b86ad70be60d3a5f64bd22251e5d9e
react-native-fast-pbkdf2: 44d6ffa0346863e14100294004a1595ec76b2e9f
Expand Down
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 9b6bf19

Please sign in to comment.