chipweinberger/flutter_blue_plus

[Help]: exceptions in startScan() are not propagated to Flutter code

Closed this issue · 4 comments

Requirements

  • I've looked at the README 'Common Problems' section

Have you checked this problem on the example app?

Yes

FlutterBluePlus Version

1.34.2

Flutter Version

3.24.3

What OS?

Android

OS Version

12

Bluetooth Module

unknown

What is your problem?

When starting a scan is not possible, i.e. due to Bluetooth turned off or refused permissions, startScan() doesn't throw any exception, also no error is added to scanResults.
Logs are from the example app after commenting out the systemDevices() call.

Logs

I/flutter (26249): [FBP] <setLogLevel> args: 5
D/[FBP-Android](26249): [FBP] onMethodCall: setLogLevel
D/[FBP-Android](26249): [FBP] initializing BluetoothAdapter
I/BluetoothAdapter(26249): BluetoothAdapter() : com.lib.flutter_blue_plus_example
I/flutter (26249): [FBP] (setLogLevel) result: true
D/[FBP-Android](26249): [FBP] onMethodCall: flutterRestart
I/BluetoothAdapter(26249): STATE_ON
D/[FBP-Android](26249): [FBP] disconnectAllDevices(flutterRestart)
D/[FBP-Android](26249): [FBP] connectedPeripherals: 0
I/flutter (26249): [FBP] <getAdapterState> args: null
D/[FBP-Android](26249): [FBP] onMethodCall: getAdapterState
I/flutter (26249): [FBP] (getAdapterState) result: {adapter_state: 4}
I/flutter (26249): [FBP] <startScan> args: {with_services: [], with_remote_ids: [], with_names: [], with_keywords: [], with_msd: [], with_service_data: [], continuous_updates: false, continuous_divisor: 1, android_legacy: false, android_scan_mode: 2, android_uses_fine_location: false}
D/[FBP-Android](26249): [FBP] onMethodCall: startScan
I/flutter (26249): [FBP] stopScan: already stopped
I/flutter (26249): [FBP] <startScan> args: {with_services: [], with_remote_ids: [], with_names: [], with_keywords: [], with_msd: [], with_service_data: [], continuous_updates: false, continuous_divisor: 1, android_legacy: false, android_scan_mode: 2, android_uses_fine_location: false}
D/[FBP-Android](26249): [FBP] onMethodCall: startScan
I/flutter (26249): [FBP] stopScan: already stopped

please open a PR.

I think you are mistaken. We check for bluetooth on and permissions here.

                case "startScan":
                {

                    // .......

                    ensurePermissions(permissions, (granted, perm) -> {

                        if (granted == false) {
                            result.error("startScan",
                                String.format("FlutterBluePlus requires %s permission", perm), null);
                            return;
                        }

                        // check adapter
                        if (isAdapterOn() == false) {
                            result.error("startScan", String.format("bluetooth must be turned on"), null);
                            return;
                        }

                        // get scanner
                        BluetoothLeScanner scanner = mBluetoothAdapter.getBluetoothLeScanner();
                        if(scanner == null) {
                            result.error("startScan", String.format("getBluetoothLeScanner() is null. Is the Adapter on?"), null);
                            return;
                        }

Sorry, I meant the Flutter user code.
Exceptions are caught in onError and not propagated further. Please see my PR.

thanks. fixed in 1.34.3