Search code examples
androidbootbattery

Android : Turn on a device programmatically


I have a smartphone connected to a solar charger. By day, it is powered correctly. But during the night, sometimes it turns itself off due to the lack of energy.

My question is : It is possible to turn it back on (programmatically), when the battery charge exceeds a certain percentage? I'm looking for a clean and legal way. I'm not interested in flaws or exploits. I found nothing in the official documentation. Thank you.


Solution

  • The mechanism for doing this relies on replacing the battery animation script, which is run while the device is turned off but plugged in, typically displaying an icon of the charging battery. The name of the script varies from device to device, but it is generally located in the /system/bin directory. Samsung devices generally call the script playlpm, and other names for the script that I've seen include ipod, lpm, and battery_charging. This will not necessarily work on every device, because this is well outside of the standard Android framework -- some devices might not have an equivalent script, or they might implement it in a different way.

    This could be characterized as an "exploit" in that it requires root and works at the Linux level rather than the Android framework level, but there is currently no alternative for implementing this behavior.

    The general mechanism for making this change is described here: https://android.stackexchange.com/questions/20021/automatically-power-on-android-when-the-charger-is-connected. Of course it's a good idea to back up the previous battery animation script before you do any of this.

    The following script has worked for me on multiple devices (several Samsung devices and the Verizon Ellipsis 7). Basically, it checks to see if the phone is plugged into AC power and has enough charge. If so, it boots up. If not, it waits for N seconds and tries again. As a side effect, the original battery animation script won't run, and you won't ever see the pretty charging animation.

    #!/system/bin/sh                                                                               
    
    # battery threshold before boot-up (in percent)                                                
    bthresh=10
    
    # time to sleep between checks (in seconds)                                                    
    sleeptime=600
    
    # file that contains current battery level as integer between 0 and 100                        
    cfi=/sys/class/power_supply/battery/capacity
    # file that contains 1 if we're plugged in to AC, 0 if not                                     
    acfi=/sys/class/power_supply/battery/subsystem/ac/online
    
    # if either file doesn't exist, just do normal sleep+boot                                      
    [ ! -f $cfi ] && sleep $sleeptime && /system/bin/reboot
    [ ! -f $acfi ] && sleep $sleeptime && /system/bin/reboot
    
    # populate capacity and AC variables                                                           
    c=`cat $cfi`
    ac=`cat $acfi`
    
    # stop loop if we're not plugged into AC                                                       
    until [ "$ac" -eq 0 ]
    do
        # if capacity above threshold, boot up                                                     
        if [ "$c" -gt "$bthresh" ]; then
        /system/bin/reboot
        fi
    
        # wait some time before next check                                                         
        sleep $sleeptime
    
        # update capacity and AC variables                                                         
        c=`cat $cfi`
        ac=`cat $acfi`
    done