boskokg / flutter_blue_plus

Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android and iOS
Other
661 stars 417 forks source link

[Help] Android: UNKNOWN_SCAN_ERROR when scanning in background #925

Open summeryi opened 4 days ago

summeryi commented 4 days ago

Requirements

Have you checked this problem on the example app?

Yes

FlutterBluePlus Version

1.32.8

Flutter Version

3.19.5

What OS?

Android

OS Version

Android 13

Bluetooth Module

null

What is your problem?

When my phone turns off the screen and triggers a startScan(), it will report an error(UNKNOWN_SCAN_ERROR). If I search again, it will report an error(SCAN_FAILED_ALREADY_STARTED)

Logs

error1:
I/flutter ( 9962): [FBP] <startScan> result: true
E/[FBP-Android]( 9962): [FBP] onScanFailed: UNKNOWN_SCAN_ERROR (100)
I/flutter ( 9962): [FBP] [[ OnScanResponse ]] result: {error_string: UNKNOWN_SCAN_ERROR (100), advertisements: [], success: 0, error_code: 100}

error2:
I/flutter ( 9962): [FBP] <startScan> args: {with_services: [], with_remote_ids: [], with_names: [], with_keywords: [], with_msd: [], with_service_data: [], continuous_updates: false, continuous_divisor: 1, android_scan_mode: 2, android_uses_fine_location: false}
D/[FBP-Android]( 9962): [FBP] onMethodCall: startScan
D/BluetoothAdapter( 9962): isLeEnabled(): ON
E/[FBP-Android]( 9962): [FBP] onScanFailed: SCAN_FAILED_ALREADY_STARTED
I/flutter ( 9962): [FBP] <startScan> result: true
I/flutter ( 9962): [FBP] [[ OnScanResponse ]] result: {error_string: SCAN_FAILED_ALREADY_STARTED, advertisements: [], success: 0, error_code: 1}
chipweinberger commented 4 days ago

how are you triggering the scan when the screen is off? this is not something FBP really supports

but this sounds like an android bug, not FBP

summeryi commented 3 days ago

After turning off the screen on my end, I wrote a timer that triggered it in 1 second. The problem occurred 100% of the time, and after the problem occurred, the search continued to fail

summeryi commented 3 days ago

Future onScanPressed() async { Future.delayed(Duration(seconds: 1),() async { try { _systemDevices = await FlutterBluePlus.systemDevices; } catch (e) { Snackbar.show(ABC.b, prettyException("System Devices Error:", e), success: false); } try { await FlutterBluePlus.startScan(timeout: const Duration(seconds: 15)); } catch (e) { Snackbar.show(ABC.b, prettyException("Start Scan Error:", e), success: false); } if (mounted) { setState(() {}); } }); }