I wanted to do something with my Android phone using ADB commands, while keeping it plugged in to run the said commands. However, the thing I wanted to test was related to the idle (dozing or asleep) mode of Android devices. The workaround was to spoof the battery state to bypass Android’s charging restrictions.
Spoofing Battery Charging Status and Forcing Android Device to Sleep (Idle Mode)
- Turn off the device screen using the physical power button.
- Trick Android into thinking the device is running on battery power:
adb shell dumpsys battery unplug - Force the device to step into deep Doze mode:
adb shell dumpsys deviceidle force-idle - Verify the device is successfully spoofed and asleep:
adb shell dumpsys deviceidle get deep(This should returnIDLE).
Can also do:adb shell dumpsys battery unplug && adb shell dumpsys deviceidle force-idle && adb shell dumpsys deviceidle get deep
Device Cleanup for Spoofed Battery and Forced Sleep (Crucial!)
- Once testing is done, you must tell the OS to start reading the actual battery state again, otherwise it won’t charge properly:
adb shell dumpsys battery reset - Bring the device out of idle:
adb shell dumpsys deviceidle unforce
Can also do:
adb shell dumpsys battery reset && adb shell dumpsys deviceidle unforce && adb shell dumpsys deviceidle get deep