Back

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.

Android Shell.Mocking battery status

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

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.

 Status Health
1Unknown1Unknown
2Charging2Good
3Discharging3Overheat
4Not charging4Dead
5Full5Over voltage
6Unspecified failure
7Cold

“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

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

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

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

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.

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.

Give it a try. It only takes a click to unsubscribe.

Read more:

 

Need help with development of your Android application?

Contact Stanfy button

 

November 25, 2015
  • Thanks for the Great info.

  • You made my day!!!
    I was looking for this info for a long time.

androidapp development