PhilipsHue / flutter_reactive_ble

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

Error scanning devices in the background #880

Open franciscogodoy97 opened 4 months ago

franciscogodoy97 commented 4 months ago

When the app goes to the background, I am using the background_fetch dependency, and when it tries to scan I get the following error. This only happens on Android, it works correctly on iOS.

Error: Exception: GenericFailure(code: ScanFailure.unknown, message: "Scan failed because application registration failed (code 6)")

config androidManifest.xml

thx.

benoit-rolandeau-act commented 2 months ago

I think your problem may be linked to this: https://developer.android.com/develop/background-work/services/foreground-services

Is your problem happened only on Android 14?

benoit-rolandeau-act commented 2 months ago

There is also a linked subject on the external Android Library used to connect to BLE: https://github.com/dariuszseweryn/RxAndroidBle/wiki/Tutorial:-Background-operation

benoit-rolandeau-act commented 2 months ago

I had the same problem even if I succeeded to manage the foreground services. I have searched a long time on internet and I have found this: https://stackoverflow.com/questions/48077690/ble-scan-is-not-working-when-screen-is-off-on-android-8-1-0

I have added an advertised UUID in the scanForDevices method:

flutterBle
          .scanForDevices(
            withServices: [
              Uuid.parse("669a0c20-0008-a7ba-e311-0685c0f7978a")
            ], //BleServiceHelper.getAvailableServices(),
            scanMode: _currentScanMode!,
          )
          .timeout(
            scanTimeoutWithoutEvent,
            onTimeout: _onScanTimeout,
          )
          .listen(
            _addScanDeviceCallback,
            onError: _onScanError,
          );

And now it's working on Android 14 and 13 when the screen is locked.

GoodOldBatman commented 1 month ago

From my experience with background scanning (without background_fetch) I can confirm that the UUID filter is required to make it work across both iOS & Android (all versions) so I would assume the same is true when using background_fetch.

This is more a question out of curiosity: @franciscogodoy97 were you able to scan on iOS using background_fetch without filtering the UUID and still receive scan results?