Skip to content

Commit

Permalink
Bugfix: FAT-801 bluetooth connection failed (#2376)
Browse files Browse the repository at this point in the history
* fix: incorrect usage of bleManager instance

Fix the lazy initialization of the BleManager

* chore: changeset
  • Loading branch information
alexandremgo authored and Justkant committed Jan 25, 2023
1 parent bf7484f commit e7bf251
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-news-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/react-native-hw-transport-ble": patch
---

Fix: incorrect usage of bleManager instance inside BleTransport
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ let connectOptions: Record<string, unknown> = {
connectionPriority: 1,
};
const transportsCache = {};
let bleManager;

let _bleManager: BleManager | null = null;
/**
* Allows lazy initialization of BleManager
* Useful for iOS to only ask for Bluetooth permission when needed
*
* Do not use _bleManager directly
* Only use this instance getter inside BleTransport
*/
const bleManagerInstance = (): BleManager => {
if (!bleManager) {
bleManager = new BleManager();
if (!_bleManager) {
_bleManager = new BleManager();
}

return bleManager;
return _bleManager;
};

const retrieveInfos = (device) => {
Expand Down Expand Up @@ -89,8 +96,8 @@ async function open(deviceOrId: Device | string, needsReconnect: boolean) {
return transportsCache[deviceOrId];
}

log("ble-verbose", `open(${deviceOrId})`);
await awaitsBleOn(bleManager);
log("ble-verbose", `Tries to open device: ${deviceOrId}`);
await awaitsBleOn(bleManagerInstance());

if (!device) {
// works for iOS but not Android
Expand Down

0 comments on commit e7bf251

Please sign in to comment.