November 25, 2015

Android Shell. Part 1: Mocking Battery Status

This article starts a series of short posts describing some useful commands available in Android shell.

We’ll start with interacting with a system service responsible for providing information about device battery.

Using “dumpsys battery” we can get information about the device battery status.

$ adb shell dumpsys battery
Current Battery Service state:
AC powered: false
USB powered: true
Wireless powered: false
status: 2
health: 2
present: true
level: 100
scale: 100
voltage: 4240
temperature: 273
technology: Li-ion

First three lines show what charges are connected. To interpret the status and health values you can use corresponding constants in BatteryManager documentation. Here’s an extract from that doc. Values we got are highlighted.

[table id=1 /]

“scale” is the maximum value of “level”. Divide “temperature” by 10 to get value in Celsius.

We can make the system think that charger is disconnected with a command

$ adb shell dumpsys battery set usb 0

In Android 6 a new “unplug” command is available. It’s an equivalent to setting all the chargers (usb, ac, wireless) to 0.

$ adb shell dumpsys battery unplug

If you want to change battery level value, use “set level” command:

$ adb shell dumpsys battery set level 5

Also let’s set battery status to “discharging”:

$ adb shell dumpsys battery set status 3

At this moment our battery is in discharging state with level 5.

The first time you invoke one of “set” commands, device stops getting information from real hardware. Hence, do not forget to finish your gambling with “reset” command in order to get our device back to earth.

$ adb shell dumpsys battery reset

Now you have a good instrument to test how your app behaves in low battery conditions. Good luck!

Check out the next part of the series Android Shell Part 2: Activity Manager Client.