Dumpsys: Battery mocking with ADB shell
In some cases a specific conditions in relation to device’s battery state, level or charging method are required for either development or testing. It seems relatively easy to charge or discharge a device. However, what if you are building an app that triggers an event when wireless charging starts? Seems like the only choice is the get device that supports wireless charging, right? You can actually easily mock any battery condition you wish, all with few simple shell commands.
With help of dumpsys (previously mentioned in: Query information from Android device). You can get quick help with battery mocking options by running:
$ adb shell dumpsys battery -h Dump current battery state, or: set [ac|usb|wireless|status|level|invalid] <value> unplug reset
This gives us following options:
- Setting battery states with => set
- ac [0/1] – sets power source as an ac charger to [false/true]
- usb [0/1] – sets power source as a usb charger to [false/true]
- wireless [0/1] – sets power source as a wireless charger to [false/true]
- status [1/2/3/4/5] – sets battery status as [Unknown/Charging/Discharging/Not charging/Full]
- level [0-100] – sets battery charge value between 0-100%
- unplug => sets all ac/usb/wireless to 0 – mocks no charging at all (available only in Android 6 and up)
- reset => once you start mocking battery status, information from hardware will no longer be used, run this at the end of your tests
Also based on following example of battery output:
$ adb shell dumpsys battery Current Battery Service state: AC powered: false USB powered: true Wireless powered: false Max charging current: 500000 status: 2 health: 2 present: true level: 75 scale: 100 voltage: 4048 temperature: 298 technology: Li-ion
- health [1/2/3/4/5/6/7] => represents battery health
- 1 => health unknown
- 2 => health good
- 3 => health overheat
- 4 => health dead
- 5 => health over voltage
- 6 => health unspecified failure
- 7 => health cold
- scale => represents maximum value of level
- voltage => divided by 1000 gives battery voltage value (4.048V)
- temperature => divided by 10 gives temperature value in Celsius (29.8C)
- technology => type of battery (Lithium-ion)
A good information source is also official Battery Manager documentation.