I have a phone with dropbear installed. I usually access my phone over WiFi & run commands in putty.
I want to get battery status & percentage in terminal. Is there any command or program to obtain battery info ?
I have a phone with dropbear installed. I usually access my phone over WiFi & run commands in putty.
I want to get battery status & percentage in terminal. Is there any command or program to obtain battery info ?
Query the system service battery
(possibly requires root access)
dumpsys battery
Output would be like
Current Battery Service state: AC powered: false USB powered: true Wireless powered: false status: 2 health: 2 present: true level: 70 scale: 100 voltage:3950 temperature: 260 technology: Li-ion
level: 70
is the battery percentage here.
Not sure about Android 2.3 but this is relevant for at least Android 4.2.1 and above:
(Prefix adb shell next to every command to run the command using adb.)
This would give you a list of all the historical broadcasts as well as the sticky ones:
dumpsys activity broadcasts
In the output search:
Sticky action android.intent.action.BATTERY_CHANGED:
The lines next to it, until you encounter a new sticky broadcast, are useful to us. In my devices, they are listed as:
Intent: act=android.intent.action.BATTERY_CHANGED flg=0x60000010 Bundle[{icon-small=17302819, present=true, scale=100, level=100, technology=Li-ion, status=4, voltage=4140, invalid_charger=0, plugged=0, health=2, temperature=200}]
Assuming that your Android has grep
utility, you can shorten the search by using:
dumpsys activity broadcasts | grep -A2 "Sticky action android.intent.action.BATTERY_CHANGED:"
Alternative: it is possible to query the battery info from /sys
or I should say the Linux kernel.
/sys/class/power_supply/battery/
has some files with useful information. The file named capacity
is equivalent to current battery percentage. You can get an accumulated info from uevent
file as well.
For more and related info, read my answer here.
dumpsys
), which is exactly what I successfully use with Adebar, tested on multiple devices. So your command will probably be something like dumpsys battery | grep "level:" | awk '{ print $2 }'
. However, I've never tested that with Android versions <4.
– Izzy
Nov 05 '15 at 11:51
cat /sys/class/power_supply/battery/capacity
at least on Android 4.1. As sysfs is a Linux feature already available for quite a while, this should work on other/previous Android versions as well. Other "files" in the context of the question: health
(e.g. "Good"), status
("Full"), temp
(centi-degrees Celsius? here: "300"). Just figuring the current power source might prove a bit tricky :)
– Izzy
Nov 05 '15 at 12:16
Using only the alternative method @Firelord mentioned.
#!/system/bin/sh # might need to change to your sh bin
print_battery_status()
{
ctype=cat /sys/class/power_supply/battery/charge_type
capacity=cat /sys/class/power_supply/battery/capacity
charging=cat /sys/class/power_supply/battery/charging_enabled
health=cat /sys/class/power_supply/battery/health
status=cat /sys/class/power_supply/battery/status
printf "# %s # %s # %s # %s # %s #\r" "Capacity: ${capacity}" "Charging: ${charging}" "Type: ${ctype}" "Health: ${health}" "Status: ${status}"
}
while [ true ]
do
print_battery_status
sleep 1 # 1 second of wait
done
You might need to adapt the script a bit. Some file names used above are device specific. For example, charging_enable
should be replaced by charge_now
and some devices, and so on.
It runs and shows status like bellow. To stop it use CRTL+C.
./batery_status.sh
# Capacity: 63 # Charging: 1 # Type: Fast # Health: Good # Status: Charging #
I use this since I do remote ssh on my device. It works even with zygote stopped.
dumpsys battery
(needs root) does the job in Android 4.2.1. Or, see if/sys/class/power_supply/battery/BatteryAverageCurrent/uevent
give you anything. See my answer here for more: How can profile (log the charging and discharging current for some time period) my battery? – Firelord Nov 05 '15 at 08:24