dotintent / FlutterBleLib

Bluetooth Low Energy library for Flutter with support for simulating peripherals
Apache License 2.0
540 stars 199 forks source link

EnableRadio not working for Android neither? Maybe on some versions of Android? #453

Closed rdehouss closed 4 years ago

rdehouss commented 4 years ago

Good morning!

Thanks a lot already for this library ;)

I just tested the methods enableRadio and and despite the fact I see that it's unit tested and so, I understand that it must work somewhere, it throws an exception in my case.

I run the application directly on my phone, not an emulator, it fails in both debug and release build.

Best regards,

Raphaël

The version pubspec.lock

flutter_ble_lib:
    dependency: "direct main"
    description:
      name: flutter_ble_lib
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.2.3"

The code

if (Platform.isAndroid) {
  try {
    await bleManager.enableRadio();
  } catch (e) {
    print(e);
    await AppSettings.openBluetoothSettings();
  }
} else {
  await AppSettings.openAppSettings();
}

=> The error in debug build:

MissingPluginException (MissingPluginException(No implementation found for method enableRadio on channel flutter_ble_lib))

Screenshot from 2020-03-31 11-21-48

The error in release build:

I/flutter (15797): NoSuchMethodError: Class 'MissingPluginException' has no instance getter 'details'.
I/flutter (15797): Receiver: Instance of 'MissingPluginException'
I/flutter (15797): Tried calling: details

and

bleManager.observeBluetoothState().listen((btState) {
  print(btState);
});

=>

MissingPluginException (MissingPluginException(No implementation found for method getState on channel flutter_ble_lib))

Screenshot from 2020-03-31 11-52-12

while the following works correctly:

BluetoothState currentState = await bleManager.bluetoothState();
print(currentState);

Here is my flutter doctor -v

flutter doctor -v
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Linux, locale en_US.UTF-8)
    • Flutter version 1.12.13+hotfix.8 at /opt/flutter
    • Framework revision 0b8abb4724 (7 weeks ago), 2020-02-11 11:44:36 -0800
    • Engine revision e1e6ced81d
    • Dart version 2.7.0

[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /opt/android-sdk/
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 28.0.3
    • ANDROID_HOME = /opt/android-sdk
    • ANDROID_SDK_ROOT = /opt/android-sdk
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)
    • All Android licenses accepted.

[✓] Android Studio (version 3.6)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 44.0.2
    • Dart plugin version 192.7761
    • Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b4-5784211)

[✓] Connected device (1 available)
    • SM G935F • 988678454e47515938 • android-arm64 • Android 8.0.0 (API 26)

• No issues found!
rdehouss commented 4 years ago

Sorry, false alarm, it was because the code being sync, I was closing destroying the client too soon

The full faulty code

Future _scan(context) async {
    BleManager bleManager = BleManager();

    await bleManager.createClient();

    BluetoothState currentState = await bleManager.bluetoothState();
    print(currentState);

    if (currentState == BluetoothState.POWERED_OFF) {
      Scaffold.of(context).showSnackBar(SnackBar(
        content: Text(
          "Bluetooth is off, turn it on to be able to scan for Links",
        ),
        action: SnackBarAction(
          label: 'Turn on',
          onPressed: () async {
            if (Platform.isAndroid) {
              try {
                await bleManager.enableRadio();
              } catch (e) {
                print(e);
                await AppSettings.openBluetoothSettings();
              }
            } else {
              await AppSettings.openBluetoothSettings();
            }
          },
        ),
      ));
    }

    bleManager.observeBluetoothState().listen((btState) {
      print(btState);
    });

    return bleManager.destroyClient();
  }

The return bleManager.destroyClient(); was the issue :) Leaving the issue in case someone does the same than me ;)