How to check battery consumption caused by Bluetooth Scans - android

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!

Related

Android BLE Scan Stops after a couple of minutes in background

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.

How does the battery drain in Android when the phone search others?

I will make the app about searching periodically with a bluetooth in Android.
The app is operating in background and the period may be too short, about 1 min.
but i dont know how I decide that.
I heard the energy with bluetooth gets low.
So. I want the quantitative data about searching with bluetooth.
Or approximate data, fine.
Plz give a hand to set the period.

Does android-wear watch keep the accelerometer always on to turn on the screen automatically? [duplicate]

I'm creating an Android Wear app that tries to detect some of the hand movements, to do it, I need to continuously monitor the accelerometer output. I'm wondering how it will affect the battery life.
For phones, I know that there are methods like "disable accelerometer while screen is turned off" to save battery, but what's the battery cost in case of watches? Since Android watch can count your steps and it turns on the screen when you face it towards your face, I believe the accelerometer is turned-on all the time anyway.
in this case, will my app drain the battery?
(Calculations after receiving the accelerometer values will be pretty simple.)
I think most android wear devices keeps the accelerometer on all the time to monitor steps, tilt-to-wake etc. However, I think most or all of this devices handles this in a low powered CPU (Sensor hub) to allow the main CPU to sleep most of the time when the screen isn't on.
If you want to monitor the accelerometer from an app, it would require the main CPU to be up and running at all times and cause a huge battery drain (20-40 mAh). I've tried this in an attempt to create a sleep tracker and drained about 70% battery over night.
You could consider batching which allows you to continuously monitor the accelerometer while allowing the main CPU to sleep most of the time. Basically you tell the sensor subsystem to gather data while the rest of the system sleeps and you just set an alarm to wakeup at a fixed interval (around 10 s for most android wear devices) to gather the latest batch of data.

When does android device go to sleep mode?

I was searching SO and web but i couldn't find sufficient answer for my particular question. Ok here we go:
Every user can modify screen timeout on android. (when screen turns off when its idle for __ seconds/minutes - users choice ). But user can modify ONLY screen timeout and not when device goes actually to sleep. I found here what happens when device goes to sleep: (Really nice answer by CommonsWare) Android Sleep/Standby Mode
Ok my question is simple. How do i know when my phone will go to sleep? Will it go to sleep immediately after turning screen off? Will it stay "awake" a while after screen was turned off? And most important: Does sleep mode in particular device varies from brand device ( how manufacturer implement this ) or it depends on ROM version (For instance, all ICS powered devices will go to sleep X seconds after screen was turned off )?
You can never really be sure if the device has gone to sleep. At best, I can give you a list of things that would make sure that the device hasn't gone to sleep:
Screen being on - Requires CPU and GPU, and hence RAM, to stay active
Any app holding a wakelock - Even the lowest wakelocks keep the CPU on, though the screen may be switched off
Apart from this, there is no guarantee that the device will go to sleep n seconds after the screen is turned off, even if no wakelocks are being held. This is upto the OEM, and they can alter this.
Keep in mind that Android devices have an option to keep the device unlocked for a certain amount of time after the screen has gone to sleep.
There is small difference in power consumption when you compare a phone with laptops.
Laptops most of the times runs on direct power through charger. (Mostly we carry charge with laptop).
Mobiles mostly run on battery power. (Rarely we carry charger with us).
By keeping this in mind, android has designed in a such a way that, phone will not consume battery power if user is not interacting with the phone. This is to increase battery back up time for user.
That's why when LCD screen power is off (that means user is not interacting with phone), with in fraction of seconds CPU will also go to a mode (sleep mode) where it does minimal work and saves battery power. When CPU is in sleep mode, it accepts commands only from RIL (radio interface layer) which is basically SMS and call functions, and alarms. Other than this CPU will stop executing other applications functions.
Note : If any application wants CPU time for its emergency work when CPU is in sleep mode, then it can request CPU time by using WAKE LOCKS.
For eg : MP3 application has to keep playing songs in its service, even though user has turned off LCD screen. That means MP3 application's service has requested CPU time by using WAKE LOCKS.
Your question : How do i know when my phone will go to sleep?
- Ans : Simple it goes sleep mode when LCD power is off, with in fraction of seconds.
Your question : Does sleep mode in particular device varies from brand device
- Ans : Since android is open source, any OEM can take driver layer , which is GPU license and can modify the power manager drive to behave differently. All these sleep modes and wake locks are finally under control of Power manager driver of Linux kernel layer.
for testing sleep mode/doze in Android 6.0 you should use next commands:
$ adb shell dumpsys battery unplug
$ adb shell dumpsys deviceidle step
more info see here: Optimizing for Doze and App Standby

how to minimize battery drain in a heavy service

I would like create a service to do something when some "hot situation" occurs, and by "hot situation" I mean the following:
the GPS/cell coordinates are in a known zone
a known Bluetooth device is detected
a known Wi-Fi network is detected
weather info has changed
considerable change in acceleration of device detected: eg. from walking to travel by car.
As one can see, these tasks are "heavy" and will be run frequently – say, 15 minutes, but sometimes even more – when I have to detect the acceleration of the device.
The question I have is this: will this service drain the battery heavily?
If so, what recommendations do you have to minimize battery drain from the tasks described above?
This might be of interest to you:
Coding for battery life
GPS is the top battery drainer - by having it on all the time, the battery duration will probably be cut down to half. Wireless connection is also quite a big drainer. If your app is working via the cell network, then if the phone is connecting to 3G then that probably drains as quickly as wireless connection anyway (maybe even more)...
I would suggest rethinking the usage of your service. Maybe make it into an app and have the user explicitly turning it on when he thinks to be in a "hot situation". Leave it as a service and the user might start thinking of your app as some sort of malfunctioning program and that will hurt severely your downloads/sales.
Also, to keep track of your apps battery usage, you can visit the "Battery usage panel" that lives inside Android preferences.

Categories

Resources