From ae332672542b7a90536e54825adf79cca4b4ba97 Mon Sep 17 00:00:00 2001 From: MoazSalem Date: Sat, 8 Jul 2023 18:33:34 +0300 Subject: [PATCH] better stability for ble scanning Bluetooth scanning could just stop working for some reason, the bug seems to be random ? --- android/app/src/main/AndroidManifest.xml | 8 ++++---- lib/Bloc/ble_bloc.dart | 5 +++-- lib/Screens/settings_page.dart | 6 +++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index c8d9561..e9a8835 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -2,10 +2,10 @@ package="com.e7na.blue"> - - - - + + + + diff --git a/lib/Bloc/ble_bloc.dart b/lib/Bloc/ble_bloc.dart index 2cd778a..3987324 100644 --- a/lib/Bloc/ble_bloc.dart +++ b/lib/Bloc/ble_bloc.dart @@ -89,7 +89,9 @@ class BleBloc extends Bloc { // if only one device is the final devices list then scan for that device only // else scan for all scanStream = ble - .scanForDevices(withServices: finalDevices.length == 1 ? finalDevices.first.uuids! : []) + .scanForDevices( + withServices: finalDevices.length == 1 ? finalDevices.first.uuids! : [], + scanMode: ScanMode.lowLatency) .listen((device) { final knownDeviceIndex = devices.indexWhere((d) => d.id == device.id); if (knownDeviceIndex >= 0) { @@ -241,7 +243,6 @@ class BleBloc extends Bloc { box.put("Keys", keys); box.put("Vectors", vectors); box.put("Uuids", uuids); - stopScan(); } // Extract selected devices from the Hive Box into a list diff --git a/lib/Screens/settings_page.dart b/lib/Screens/settings_page.dart index 25398b6..eda2b67 100644 --- a/lib/Screens/settings_page.dart +++ b/lib/Screens/settings_page.dart @@ -80,13 +80,13 @@ Widget theScaffold({ padding: const EdgeInsets.symmetric(horizontal: 60.0), child: ElevatedButton( onPressed: () => { - B.stopScan(), - B.chosenDevices = [], - B.finalDevices = [], + B.chosenDevices.clear(), + B.finalDevices.clear(), B.box.put('NumDevices', 0), B.box.put("IDs", ""), B.box.put("Names", ""), B.box.put("Uuids", ""), + B.stopScan(), Navigator.of(context).pop(), Navigator.of(context).pushReplacement( MaterialPageRoute(builder: (context) => const ScanPage()))