From 9170a8e2f534376579a15c18313335b3a8e5a429 Mon Sep 17 00:00:00 2001 From: Andrey Sorokin Date: Fri, 10 May 2024 00:37:47 +0500 Subject: [PATCH] fix(mobile): fix ledger on android --- .../src/ledger/useBluetoothAvailable.ts | 19 +++++++++++++---- patches/react-native-ble-plx+2.0.3.patch | 21 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 patches/react-native-ble-plx+2.0.3.patch diff --git a/packages/mobile/src/ledger/useBluetoothAvailable.ts b/packages/mobile/src/ledger/useBluetoothAvailable.ts index 6e291ec4e..cbf0a5390 100644 --- a/packages/mobile/src/ledger/useBluetoothAvailable.ts +++ b/packages/mobile/src/ledger/useBluetoothAvailable.ts @@ -4,6 +4,7 @@ import { showBluetoothPoweredOffAlert, } from '$utils/bluetoothPermissions'; import TransportBLE from '@ledgerhq/react-native-hw-transport-ble'; +import { delay } from '@tonkeeper/core'; import { isIOS } from '@tonkeeper/uikit'; import { useEffect, useState } from 'react'; import { Observable } from 'rxjs'; @@ -14,20 +15,30 @@ export const useBluetoothAvailable = () => { useEffect(() => { const subscription = new Observable(TransportBLE.observeState).subscribe({ next: async (event) => { - setAvailable(event.available); - if (event.type === 'Unauthorized') { console.log('Bluetooth Unauthorized'); if (isIOS) { showBluetoothPermissionsAlert(); - } else { - await checkAndRequestAndroidBluetooth(); } } if (event.type === 'PoweredOff') { console.log('Bluetooth Powered Off'); showBluetoothPoweredOffAlert(); } + + if (event.type === 'PoweredOn') { + if (isIOS) { + setAvailable(event.available); + } else { + try { + await delay(1000); + const granted = await checkAndRequestAndroidBluetooth(); + setAvailable(granted); + } catch { + setAvailable(false); + } + } + } }, complete: () => {}, error: () => {}, diff --git a/patches/react-native-ble-plx+2.0.3.patch b/patches/react-native-ble-plx+2.0.3.patch new file mode 100644 index 000000000..b4d6ce5fa --- /dev/null +++ b/patches/react-native-ble-plx+2.0.3.patch @@ -0,0 +1,21 @@ +diff --git a/node_modules/react-native-ble-plx/android/src/main/java/com/polidea/reactnativeble/BleClientManager.java b/node_modules/react-native-ble-plx/android/src/main/java/com/polidea/reactnativeble/BleClientManager.java +index 48f946d..d97919d 100644 +--- a/node_modules/react-native-ble-plx/android/src/main/java/com/polidea/reactnativeble/BleClientManager.java ++++ b/node_modules/react-native-ble-plx/android/src/main/java/com/polidea/reactnativeble/BleClientManager.java +@@ -925,6 +925,16 @@ public class BleClientManager extends ReactContextBaseJavaModule { + ); + } + ++ @ReactMethod ++ public void addListener(String eventName) { ++ // Keep: Required for RN built in Event Emitter Calls. ++ } ++ ++ @ReactMethod ++ public void removeListeners(int count) { ++ // Keep: Required for RN built in Event Emitter Calls. ++ } ++ + private void sendEvent(@NonNull Event event, @Nullable Object params) { + getReactApplicationContext() + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)