PhilipsHue / flutter_reactive_ble

Flutter library that handles BLE operations for multiple devices.
https://developers.meethue.com/
Other
662 stars 326 forks source link

ScanFailure on android #195

Closed carlos-labrador closed 3 years ago

carlos-labrador commented 3 years ago

Describe the bug After integrating FlutterFire package into our project , we are getting the following exception

REACTIVE_BLE: Received ScanResult(result: Result<DiscoveredDevice, GenericFailure<ScanFailure>>.failure(GenericFailure<ScanFailure>(code: ScanFailure.unknown, message: "Scanning parameters are not set"))) I/flutter (14507): Exception: GenericFailure<ScanFailure>(code: ScanFailure.unknown, message: "Scanning parameters are not set")

Pub Specs: ``` dependencies: flutter: sdk: flutter firebase_core: "^0.5.3" flutter_localizations: sdk: flutter flutter_reactive_ble: ^2.7.1 firebase_messaging: "^8.0.0-dev.10" ```

To Reproduce Steps to reproduce the behavior:

  1. Initialize FlutterReactiveBle
  2. if it is ready shows Bluetooth Scan page.
  3. Users tap on scan button.

Expected behavior it should show all available Bluetooth devices.

*Smartphone

Peripheral device

Additional context I could reproduce it on the base example app adding flutter fire package.

Taym95 commented 3 years ago

Hi, I think the error Scanning parameters are not set tells a lot, you should provide params to scanForDevices function, withServices param is required, check startScan in the example app, can you check?

carlos-labrador commented 3 years ago

Hi @Taym95 ,

In my ticket says "I could reproduce it in the example app" that's mean I took the time of review it a lot.

Should you need assistance to reproduce it I kindly can tell you.

Thanks.

Taym95 commented 3 years ago

Hi @carlos-labrador, I tried to reproduce it, I added await Firebase.initializeApp(); here before building UI and linked it with a firebse project, and I managed to scan for BLE devices, can you share the way you used firebase?

device: Huawei FIG LX1.

carlos-labrador commented 3 years ago

Hi @Taym95 , I already updated the example. Let me know if you need anything.

I found something interesting, it only fails if a callback is passed to FirebaseMessaging.onBackgroundMessage.

Thanks for your support.

jjchiw commented 3 years ago

Hi!

I can confirm the exception Scanning parameters are not set is thrown when declaring the onBackgroundMessage with a top-level function

This is the documentation of onBackgroundMessage

// void onBackgroundMessage(Future<void> Function(RemoteMessage) handler)
// package:firebase_messaging/firebase_messaging.dart
// Set a message handler function which is called when the app is in the background or terminated.
// This provided handler must be a top-level function and cannot be anonymous otherwise an [ArgumentError] will be thrown.

This snippet when using with flutter_reactive_blue makes the plugin to throw the exceptionScanning parameters are not set https://github.com/PhilipsHue/flutter_reactive_ble/issues/196 (It seems it resets the values in ScanDevicesHandler.kt)

// This is should be a top-level method or in a class as static
Future<void> firebaseBackgroundMessageHandler(
      RemoteMessage message) async {
    // handler
    return;
  }

class FirebaseHelper {
  static void init() {
    FirebaseMessaging.onBackgroundMessage(firebaseBackgroundMessageHandler);
  }
}

With this snippet flutter_reactive_blue works correctly, but Firebase fails to get the message when the app is onBackground

class FirebaseHelper {
  static void init() {
    FirebaseMessaging.onBackgroundMessage((RemoteMessage message) async {
        await Future.delayed(Duration(seconds: 1));
        return;
      });
  }
}
werediver commented 3 years ago

Can anybody comment on whether the same is happening on iOS?

jjchiw commented 3 years ago

Hi

Yes it works fine on iOS

remonh87 commented 3 years ago

Hmm looks like some race condition in combination with that method. I will take a look at it the coming days, already fixed something similar in the past but apparently the fix was not enough.

carlos-labrador commented 3 years ago

@remonh87 Thanks for your feedback and hard work.

remonh87 commented 3 years ago

This issue should be fixed in version 2.7.2 . Thanks for the clear reproduction steps, was easy to reproduce it.

carlos-labrador commented 3 years ago

@remonh87 , Thanks for your support.

riccardocescon commented 1 year ago

@remonh87 The same thing is happening with the latest version 5.0.2. The BLE was working perfectly, but last week i implemented the FirebaseMessage.onBackgroundMessage callback and started having this issue. Is it possible that this issue has been regressed with the latest versions? Tested with: Pixel 6 - Android 13 Xiaomi MI 9 - Android 11

insertjokehere commented 9 months ago

@remonh87 we have also been seeing this error in production - any suggestions for how we could debug this?

AndreaBalde commented 6 months ago

@remonh87 even with version 5.3.1 I have the same problem. Tested with: Pixel 7 - Android 14

chplalex commented 3 months ago

@remonh87 the same issue with 5.2.0 Tested with Android 12. iOS works fine.

UPD:

In my case, the issue was another: I created a new scan for the device's subscription without canceling the previous one. That's why the new subscription fails. Unfortunately, the error message does not explain exactly, what happens.