25

I need to unlock my screen using adb, and wait-for-device exits way too early(when the device is booting up), and screen unlock fails. Is there a way to detect that the home screen, so I can fire screen unlock then?

This is the command I am using to unlock screen -

adb wait-for-device shell input keyevent 82

3 Answers 3

30

well wait-for-device, as you already realized only waits until the adb daemon properly started. This is already at init time. In order to check for a complete boot you cann ad to your script something like:

in pseudo:

  1. wait-for-device
  2. long as getprop sys.boot_completed != 1 sleep some seconds check getprop sys.boot_completed again
  3. shell input keyevent 82

This should work.

Something like this:

#!/bin/bash

adb wait-for-device

A=$(adb shell getprop sys.boot_completed | tr -d '\r')

while [ "$A" != "1" ]; do
        sleep 2
        A=$(adb shell getprop sys.boot_completed | tr -d '\r')
done

adb shell input keyevent 82

This is not tested so be aware of potential mistakes

7
  • Please edit your answer to be easily readable. You have a valid answer, but a little clarity would help everyone. Thanks
    – HasH_BrowN
    Commented Sep 30, 2014 at 16:09
  • answer is now edited. Hope this helps you :) Commented Oct 6, 2014 at 14:58
  • good answer +1, now others will be able to follow it. Dont forget to add "Always perform a backup", for those accidental moments;)
    – HasH_BrowN
    Commented Oct 6, 2014 at 16:10
  • 2
    I found that result of getprop had a carriage return after it, so changing the adb command to $(adb shell getprop sys.boot_completed | tr -d '\r') did the trick. This is just trimming out the carriage return character. Commented Jan 5, 2015 at 21:28
  • @divided-by-zero please modify your answer to include the trimming mentioned by CoatedMoose. Umang please accept it as a answer as well. Commented Jun 4, 2015 at 12:26
18

This is an old question and borderline off-topic but here is how to do it in a single line:

adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done; input keyevent 82'
1

Here's what I came up with:

adb wait-for-device shell <<ENDSCRIPT
echo -n "Waiting for device to boot "
echo "" > /data/local/tmp/zero
getprop dev.bootcomplete > /data/local/tmp/bootcomplete
while cmp /data/local/tmp/zero /data/local/tmp/bootcomplete; do 
{
    echo -n "."
    sleep 1
    getprop dev.bootcomplete > /data/local/tmp/bootcomplete
}; done
echo "Booted."
exit
ENDSCRIPT

echo "Waiting 30 secs for us to be really booted"
sleep 30

echo "Unlocking screen"
adb shell "input keyevent 82"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .