Skip to content

Commit

Permalink
better stability for ble scanning
Browse files Browse the repository at this point in the history
Bluetooth scanning could just stop working for some reason, the bug seems to be random ?
  • Loading branch information
MoazSalem committed Jul 8, 2023
1 parent 22b3f9b commit ae33267
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
package="com.e7na.blue">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.NEARBY_WIFI_DEVICES"
android:usesPermissionFlags="neverForLocation"/>
Expand Down
5 changes: 3 additions & 2 deletions lib/Bloc/ble_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ class BleBloc extends Bloc<BleEvent, BleState> {
// 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) {
Expand Down Expand Up @@ -241,7 +243,6 @@ class BleBloc extends Bloc<BleEvent, BleState> {
box.put("Keys", keys);
box.put("Vectors", vectors);
box.put("Uuids", uuids);
stopScan();
}

// Extract selected devices from the Hive Box into a list
Expand Down
6 changes: 3 additions & 3 deletions lib/Screens/settings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down

0 comments on commit ae33267

Please sign in to comment.