Android BLE Scan Stops after a couple of minutes in background - android

I faced the Android scan stopping issue in background when I am developing my COVID contact tracing app for my company. Here is what I have tried:
Add foreground service
Disable all the battery related optimization options in the phones
Enable the application running in background
Tesging devices:Galaxy S20 and Xperia with Android 10, Huawei with Android 8.
The scan stops immediately when going to background if you don't disable those battery optimization settings and application background update. After you disabled those settings, the scan can run about a couple of minutes(~5 minutes), then still stops. From the blog of David:http://www.davidgyoungtech.com/2017/08/07/beacon-detection-with-android-8, it seems that it is impossible to scan continuesly in background, because the JobScheduler will restart every 15 minutes while each scan lasts ~10 minutes at most. Is this the reality, or this is the best solution that I can scan 10 minutes at every 15 mins cycle?

Background beacon detections are tricky to implement because many small issues can trip you up, and the specific issues vary by Android version, Android manufacturer and sometimes model. While Android 8+ restricts background ranging to every 15 mins using the Job Scheduler, if you add a Foreground Service, you can unlock unlimited background ranging.
A few tips:
Focus first on the Galaxy S20 as Samsung behaviors are better documented and closer to vanilla Android. (Ideally you would test on a Pixel device first.) Only once it works on Samsung, move on to the others.
Using the Android Beacon Library reference app configured with the built-in foreground service, I have seen detections of a standard iBeacon or AltBeacon continue on a Galaxy S10 indefinitely in the background, even with a 1.1 second scan period. See if you can reproduce the same.
Be careful of Doze mode. If the CPU is put to sleep due to the phone being motionless with the screen off and not charging, your detections will stop. You can defeat Doze mode with wake locks, but it has a punishing impact on battery usage. You are better off accepting its limitations and keeping your phone in motion periodically during testing. If you want to get logs to see what is happening, use ADB commands to disable charging when connected via USB or learn to use ADB over WiFi.

Related

is there any way to continue Bluetooth Low Energy scan,never stop for a android app

I 'm developping a continue scan android app, because I have to collect some important info from another device , I have to scan ble device,and never stop, otherwise, I may miss some important info. Is there any way for a android app to continue Bluetooth Low Energy scan,never stop ,scan for a year if the important device don't appear. During the year, my android phone always keep sufficient electricity,but sometimes I will press home button , sometimes I will make Screen Off (not power off ,just make screen off).
Modern Android has implemented various battery saving measures which will prevent you from continuous data collection.
Doze mode is implemented by the Android framework. See: Scanning for Bluetooth LE devices when the phone is in doze mode, don't work. CallBack method doesn't called when doze mode
Additionally various device manufacturers have implemented their own battery savings systems which will kill your app even when you are following Android framework rules. See: https://dontkillmyapp.com/ for details.

Bluetooth Low Energy scan result delivery is reduced when the screen is OFF

I am developing an android application that uses Bluetooth Low Energy to receive data from a peripheral device.
The device itself sends the data as part of it's advertising message and it could send the message at any time.
So the applications works by continuously scanning for BLE devices.
When the screen is ON the application receives scan results as intended.
When the screen is OFF it appears that it only receives scan results every 5 seconds.
I guess that the phone is trying to optimize for battery life but this app is mission-critical so we can not afford to lose messages.
The scanning is done by a Service which is set as a 'foreground service' so that the OS knows that it is important.
The service also acquires a PowerManager.PARTIAL_WAKE_LOCK but that does not seem to help.
The application is excluded from battery optimizations so that it does not enter Doze or deep sleep mode.
The application is using the following ScanSettings:
ScanSettings.SCAN_MODE_LOW_LATENCY
ScanSettings.CALLBACK_TYPE_ALL_MATCHES
ScanSettings.MATCH_MODE_AGGRESSIVE
ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT
I am working with a Galaxy S7 but I have been told the same behaviour happens with a Galaxy S7 Edge.
I have tested with other non-samsung devices and they work as intended (continuously receiving scan results when the screen is OFF).
The question: is there something that I, as a developer, can do to make the BT adapter deliver scan results continuously even when the screen is OFF?
Samsung automatically change scan mode to low_power if screen is turned off. It's their customisation on their Android OS, so unfortunately no way to change it.

How to check battery consumption caused by Bluetooth Scans

I have an Android application to detect Eddystone beacons using the Android Beacon Library. I would like to test the battery consumption caused by the application to find the optimal background and foreground scan rates. In Settings > Apps > MyBeaconDetectorApp, it shows 0% battery usage after having the bluetooth On for more than an hour. I think the application is in background and hence it is 0%. However, how do I check the battery consumption caused by Bluetooth scans. I have set my BackgroundBetweenScanPeriod as 1 minute.
Thanks
While it is difficult to measure the battery consumption directly, here is the technique I use:
Use a test device with a limited number of apps installed that will be running in the background. A stock Nexus device after factory reset is ideal.
Charge the battery to 100%
Put the device into airplane mode and put bluetooth on.
Install your app start it running in the background. Turn off the screen, and disconnect the device's USB connector.
Note the start time.
Let the app run in the background for at least 8 hours, ideally in the presence of at least one beacon.
Measure the battery level. The delta from 100% is the percentage drain. Note this number and the end time.
Look up how many mAH your battery has for your phone model. Multiply the percentage drain by the mAH and divide by the time of the test in hours. This tells you how many mA the phone uses when running your app.
Next, uninstall your app, and repeat the procedure above. This will give you a baseline battery drain for your device without your app running.
Finally, subtract the mAH baseline from the mAH with your app running to measure the additional mAH used by your app.
One final note: if you are really scanning every minute in the background (the default is every five minutes to save battery), I would expect this may cause a significant additional battery drain. Testing using the procedure above will tell you for sure. I'd love to hear your results!

Android notification when BLE device discovered

We are working on an Android app that makes GATT connection to BLE devices and writes some characteristics. Our current approach is to scan for devices (startLEScan on Android 4.3 and 4.4. devices and startScan on Android 5.0+) every few seconds and connect to device when discovered.
This solution works but it drains a lot of battery because we need to keep wake locks (or trigger alarm manager every few seconds) for the scanning to work when the phone is in sleep.
Is there an way that Android can notify the app when the BLE device is discovered instead of continuously scanning for devices?
iOS app works great because iOS wakes the app in background when there is any BLE device nearby, which saves a lot of battery.
Wondering it there is something similar on Android to keep the battery under acceptable limit.
Just for reference, we have also tried the Alt-Beacon library but found
that it internally usages alarm manager and if we set alarm manager to trigger every 5 seconds, it consumes 7-8% battery every hour.

Android BLE disconnected when screen off (without charger)

It's working well on most Android devices.
Other device's screen gets locked after 1 minutes BLE device gets disconnected,if device without charger.
Thanks for any suggestions!
this might come from the Keep Wi-Fi on during sleep option (which might affect all radios) - as it seems this only exists in 8.0 but not in 8.1 (at least on the Pixel 2 XL, according to this article).
You might be having this problem because your app is not properly configured to handle Doze mode.
While there do not seem to be any restrictions on using BLE itself, doze mode might be messing with the control mechanism for your bluetooth connection (ex: a background service).
To see if this is the issue, you can whitelist your app manually via Settings > Battery > Battery Optimization and disabling optimization for your app.
Furthermore, as of Android 8.0, there are restrictions on background services when your app is not in the foreground. So, you may need to create a foreground service to handle the connection.

Categories

Resources