8

I'm trying to get battery stats in my application for some benchmarking. The wonderful BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER lets me query the device's micro-amps which is great. However, this was only introduced in API 21 so the number of devices this can reach is quite limited. Is there a compatible way to do something similar in lower versions of the APIs (down to 4.0)? I know BatteryManager.EXTRA_LEVEL will give me the percentage but that's really not fine-grained.

David M
  • 841
  • 6
  • 23
  • I don't think there is one. From what I know early Android API are sloppy on this area. – StoneBird Jul 27 '15 at 15:02
  • 1
    @StoneBird are you familiar with the API? On some API 22 devices BATTERY_PROPERTY_CHARGE_COUNTER gives me -9223372036854775808, which is Long.MIN_VALUE. That's definitely strange. Other devices give me an actual result – David M Jul 27 '15 at 15:20
  • What kind of information do you need about the battery? – TDG Jul 27 '15 at 17:13
  • @TDG I'm looking for a way to get fine-grained power consumption for things like comparing power consumption of different algorithms. – David M Jul 27 '15 at 19:31
  • You can get battery capacity in mAh using this- http://stackoverflow.com/questions/23193388/android-get-battery-current-capacity-in-ma-and-total-capacity-of-battery-in-mah – Mohit Rajput Aug 03 '15 at 14:54

3 Answers3

11

As far as I know, you can't do that with older public APIs. I worked on a system-level app that shows battery usage stats to a user. I based my code from Android Settings app and my users might need root access or the app has to be signed with platform key. You can look into PowerUsageDetail, BatteryInfo, and PowerUsageSummary classes. If you are lucky, you might get access to hidden APIs with JAVA reflection. Otherwise, you need platform key or root. The internal APIs change very often so it's quite a pain to use.

There are also a couple tools that you might want to check out:

  • adb shell dumpsys batterystats
  • adb shell dumpsys batteryinfo
  • battery historian
  • adb shell bugreport

Dumpsys source code

Good luck!

Edit:

My old project might be useful for you.

pt2121
  • 11,720
  • 8
  • 52
  • 69
  • I can get root access on my test devices so if that's a possibility that would be great. I'm only concerned with getting the power consumption of my test devices, not for general users. – David M Jul 28 '15 at 01:48
  • https://github.com/prt2121/BatUsage might help. I don't quite remember what I did back then though... – pt2121 Jul 28 '15 at 01:56
  • I'll mark this as the answer since it's got a lot of useful information but not all devices even record all the necessary information for monitoring actual power usage, unfortunately so it doesn't appear possible to cover every device. At least, not without a huge amount of effort. – David M Aug 03 '15 at 22:40
  • from where i can get your aidl file. ?? – IshRoid Jan 07 '16 at 12:35
4

Earlier this year I ran into an interesting article about a method to locate smartphones based only on the power consumption of the device, called PowerSpy.

While I have never implemented/tested the method myself, the authors claim that:

On Android reading the phone’s aggregate power meter is done by repeatedly reading the following two files:
/sys/class/power_supply/battery/voltage_now
/sys/class/power_supply/battery/current_now.

They also claim to have tested the method on Nexus 4, Nexus 5 and HTC. While this is not that specific about which exact versions of Android the devices were equipped with, Nexus 4 is supposed to come with API level 17 and Nexus 5 with API level 19, but both phones seem to be subject to OS upgrades.

However, this seems to be a radically different method when compared with the ones mentioned by the other posters, so probably worth mentioning. Unfortunately, it seems to be a low-level approach, so a lot of work might be required to get what you need, since power, as in Physics, does not really mean battery consumption. But maybe you need it this way.

Last, the article mentioned that access to the two files requires no special permissions (which kind of motivated the study they did in the first place).

EDIT

Second thought, if you plot P=U*I (getting U and I as mentioned above) and integrate over time (that would be a sum in this discrete case) you might just get a very good approximation of battery consumption. Maybe you also factor in a Kalman filter (to attenuate the noise on U and I, keeping in mind that your algorithms are not the only consumer) and you might just get what you want.

cobarzan
  • 664
  • 3
  • 11
  • 23
  • I've actually read that paper myself when it came out. Interestingly, calling `Runtime.getRuntime().exec("cat /sys/class/power_supply/battery/voltage_now")` doesn't work on some of the devices and trying to open them as files doesn't work either even though if I run `adb shell cat /sys/class/power_supply/battery/voltage_now` it does work. – David M Aug 01 '15 at 04:09
  • It looks like these devices don't monitor current so that approach doesn't actually work on them. – David M Aug 02 '15 at 14:59
0

As Per the battery monitoring training http://developer.android.com/training/monitoring-device-state/battery-monitoring.html , you can get the current battery information by using:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;

Note that you need to continue to call this repeatedly to get the updated battery level. As waking up your app and checking this does take battery itself, they suggest:

kuljeet singh
  • 2,792
  • 2
  • 12
  • 15
  • 4
    If you read my post you'll see that the BatteryManager methods compatible with APIs below level 21 don't provide the granularity I need. – David M Jul 30 '15 at 13:00
  • I edit the answer above code will work with API level 21 and give you the battery stats . Please look into the topic battery monitoring in android developer before implementing the answer as I mentioned the link to the developer site in my edited answer – kuljeet singh Jul 30 '15 at 13:16
  • 4
    Again, if you read my post, you'll see I'm well aware of those APIs. I'm looking for alternatives because these don't do what I need. – David M Jul 30 '15 at 15:47