andrey-ushakov / flutter_bluetooth_basic

Flutter plugin. Allows to find bluetooth devices & send raw bytes data
BSD 3-Clause "New" or "Revised" License
39 stars 141 forks source link

Exposing isAvailable and isOn to Flutter. #10

Open Uros-V opened 4 years ago

Uros-V commented 4 years ago

If I start the scan with BT disabled then obviously no devices will be found, but the issues is that an Unhandled Exception will be thrown, which isn't graceful:

BluetoothManager bluetoothManager = BluetoothManager.instance;
//...
bluetoothManager.startScan(timeout: Duration(seconds: 2));

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(startScan, getBluetoothLeScanner() is null. Is the Adapter on?, null)
    #0      BluetoothManager.scan (package:flutter_bluetooth_basic/src/bluetooth_manager.dart:85:7)
    <asynchronous suspension>
    #1      BluetoothManager.startScan (package:flutter_bluetooth_basic/src/bluetooth_manager.dart:116:11)...

I can work around that by catching it, like so:

bluetoothManager.startScan(timeout: Duration(seconds: 2)).catchError((e){
// tell the user to check if BT is on.
// print(e);
});

But that isn't graceful either IMHO, also I don't know if the devices is missing BT (like an emulator) or it is just turned off.

I noticed that in "FlutterBluetoothBasicPlugin.java" these cases are already covered:

      case "isAvailable":
        result.success(mBluetoothAdapter != null);
        break;
      case "isOn":
        result.success(mBluetoothAdapter.isEnabled());
        break;

So it would be nice if we could call them from Flutter with bluetoothManager.isAvailable() and bluetoothManager.isOn().