I am working on a part of an indoor localization project. I need my Flutter application scan and get Mac addresses and RSSI values of Wifi and Bluetooth devices. I am using flutter_blue for bluetooth scans and wifi_scan for Wifi scans. The application works well while in the foreground but scans are failing when the application is in background.
What I have tried so far:
- Tried to change Bluetooth scan mode from
ScanMode.lowLatency
toScanMode.lowPower
andScanMode.opportunistic
for thestartScan()
method for flutter_blue. - flutter_background. I managed to do some tasks in background but wifi and bluetooth did not work.
- flutter_background_service the same situation
- flutter_foreground_plugin the same situation
- flutter_foreground_task the same situation
- workmanager did not try it because it says it can trigger the bg operation per every 15 minutes but I am scanning per every 6 seconds.
Is there any way to achieve this? Aren’t there any package with the option backgroundMode: true
? I don’t know the natives. Don’t know Kotlin too.
2
Answers
After a lot of things have been tried, I just tried to get GPS location too.
Used this package: location
And execute
location.enableBackgroundMode(enable: true)
as it is stated in readme, then my wifi and bluetooth scans starts working in background too.It's Ridicoulus
According to the Android Developer documentation,
ScanMode.lowPower
is the only possible mode for background scanning. You also need to request location access for the background (documentation here and here).Consider switching to Flutter Blue Plus instead of Flutter Blue; Flutter Blue Plus is the currently-maintained version. This pull request on GitHub shows that Flutter Blue cannot run in the background. The pull request has never been merged.