33

How to wake up Android with use adb - I want to wake up (if asleep) Android terminal before debugging every new version of application.

Typical flow is: 1. I do some changes in Eclipse. 2. In the meantime screen goes off or not. 3. I run "debug" and want force screen to wake up.

I found a method with "power key" emulation but it does not turn it on but rather toggles the power state. I do not want to add extra code to my application. What are the other methods to do such trivial task, please help.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
Chameleon
  • 9,722
  • 16
  • 65
  • 127

9 Answers9

54
adb shell input keyevent KEYCODE_WAKEUP

As described here, this wakes up the device. Behaves somewhat like KEYCODE_POWER but it has no effect if the device is already awake.

To toggle sleep/wake with one command you can issue

adb shell input keyevent KEYCODE_POWER
boltup_im_coding
  • 6,345
  • 6
  • 40
  • 52
18

Just use :

adb shell input keyevent 26
Rene Barbosa
  • 2,867
  • 1
  • 13
  • 8
  • 3
    26 == KEYCODE_POWER. There's also a KEYCODE_WAKE which will have no effect if the device is already awake. See my answer: http://stackoverflow.com/a/33683205/1172494 – boltup_im_coding Nov 12 '15 at 23:15
9

Here's what I came up with:

adb shell dumpsys power | grep "mScreenOn=true" | xargs -0 test -z && adb shell input keyevent 26

This will first check to see if the screen is on. If it isn't, it will emulate the power key, which will turn on the device's screen.

Brian
  • 334
  • 3
  • 4
  • what is the `xargs -0 test -z &&` portion of this command doing? Here is my understanding, please let me know if Im correct: `xargs -0` verifies that the mScreenOn text was successfully "grepped" and if so `&&` `adb shell input keyevent 26` is called?? Is the test -z command necessary for this and if so...what is it doing? – Tim Boland Nov 23 '14 at 01:31
  • Has that changed over the time? On a kitkat and on a lollipop device there is no mScreenOn. – rekire Apr 02 '15 at 09:04
  • 1
    For the latest API, use this `adb shell dumpsys power | grep "Display Power: state=ON" | xargs -0 test -z && adb shell input key event 26` – Vimalraj Selvam Feb 09 '16 at 11:48
8

if you execute adb shell input keyevent KEYCODE_POWER and got Killed, you should use root user by execute su before.

sleep:(adb shell) input keyevent KEYCODE_SLEEP

wakeup:(adb shell) input keyevent KEYCODE_WAKEUP

toggle:(adb shell) input keyevent KEYCODE_POWER

ife
  • 1,231
  • 13
  • 13
3

You can check device's current power state (including display) via adb with dumpsys power command and send the power key press event only if the display is off. The easier solution would be disabling the display timeout altogether "while connected to USB" in the Developer options.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • Thanks for solution I will write some small script. Yes I could disable display timeout "while connected to USB" but it stops recharging better is just switch before debug since it very fast. – Chameleon Feb 08 '13 at 22:58
2

you could also add the following flags to your onCreate() in your main activity:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

This way your device should wake up when it is loaded onto the device through Eclipse.

amark
  • 79
  • 5
1

I know that this is the opposite of what the OP requested, but I wanted to point out the definition of "truly asleep" for other readers: If adb is active, then the device is not truly asleep, since USB is on and the command prompt is running (how else is adb shell going to work?). When fully asleep, only the physical power button will wake it (unless other buttons are selected as a wake source in the BSP). My project draws about 120ma with screen off, but only 23ma in sleep mode (disconnect USB cable). "adb shell input keyevent 26" does not work in this mode. Neither does the serial console. My test fixture does have access to the battery and external power line separately. I can toggle the power line (with battery connected) to wake it up. I also have a switched USB hub that disconnects the USB specifically for the sleep portion of the test.

I hope this can be some help to someone.

gyent
  • 11
  • 1
  • I have the same problem. I can't WAKE(UP) when I use remote ssh access and no power cable is pluged in. – Bob Genom Nov 09 '22 at 14:57
0

For Windows and Eclipse you can use .bat file:

@ECHO OFF
setlocal
for /f "delims= tokens=1*" %%a in ('adb shell dumpsys power ^| findstr.exe "mScreenOn="') DO (
for /f "delims== tokens=2*" %%S in ("%%a") do (
if "%%S" == "false" (goto move1) else (goto move2)
)
)

:move1
adb shell input keyevent 26
goto end

:move2
goto end

:end
exit

It will check, if screen is off it will switch the power state. To use it with Eclipse this .bat file should be used as "External tool" (just fill the path to the .bat) and linked to the project in it's properties (Builders - Import - ).

patrik
  • 4,506
  • 6
  • 24
  • 48
Sergey Grabak
  • 183
  • 1
  • 12
0

I used the following to wake up remote devices for automated tests. If the screen isn't on, then it will press the power button followed by the menu button.

(
  cd ${platform-tools};
  ./adb shell dumpsys power | grep -i 'SCREEN_ON' >/dev/null;
  if [ $? -eq 0 ]; then
    echo "Screen is on...";
  else
    echo "Pressing buttons to wake-up...";
    # http://developer.android.com/reference/android/view/KeyEvent.html
    ./adb shell input keyevent 26; # Power
    ./adb shell input keyevent 82; # Menu
    echo "Pausing for state to settle...";
    sleep 1;
    ./adb shell dumpsys power | grep -i 'SCREEN_ON';
  fi;
  echo '---';
  ./adb shell dumpsys power | grep -i 'SCREEN'
)
TJR
  • 3,617
  • 8
  • 38
  • 41