-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 (rn-ble): Extract ble plx in a static instance to fix ble connnecti…
…vity
- Loading branch information
1 parent
0e6ee7d
commit 10f3990
Showing
7 changed files
with
912 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
libs/ledgerjs/packages/react-native-hw-transport-ble/src/BlePlxManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.