Skip to content

Instantly share code, notes, and snippets.

@vrendina
Created June 24, 2017 22:29
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vrendina/c8dd3f4983d09b1e4877b271ada29058 to your computer and use it in GitHub Desktop.
Save vrendina/c8dd3f4983d09b1e4877b271ada29058 to your computer and use it in GitHub Desktop.
Turn the screen on or off on all connected Android devices
#!/bin/sh
# Returns the power state of the screen 1 = on, 0 = off
getDisplayState() {
state=$(adb -s $1 shell dumpsys power | grep mScreenOn= | grep -oE '(true|false)')
# If we didn't get anything it might be a pre-lollipop device
if [ "$state" = "" ]; then
state=$(adb -s $1 shell dumpsys power | grep 'Display Power' | grep -oE '(ON|OFF)')
fi
if [ "$state" = "ON" ] || [ "$state" = "true" ]; then
return 1;
else
return 0;
fi
}
if [ $# -eq 0 ]; then
echo "Usage: $0 [on|off]"
exit 1;
fi
if [ "$1" = "on" ]; then
echo "Turning on screen on all connected devices..."
for device in `adb devices | grep device$ | cut -f1`
do
echo -n "Found device: $device ... "
getDisplayState $device
state=$?
# If the display is off, turn it on and unlock
if [ $state -eq 0 ]; then
echo "display was off, turning on"
# press power on
adb -s $device shell input keyevent 26
# press menu
adb -s $device shell input keyevent 82
# enter pin
adb -s $device shell input keyevent xxx
adb -s $device shell input keyevent xxx
adb -s $device shell input keyevent xxx
adb -s $device shell input keyevent xxx
# press enter
adb -s $device shell input keyevent 66
else
echo "display was on, pressing home button to keep alive"
adb -s $device shell input keyevent 3
fi
done
exit 0;
fi
if [ "$1" = "off" ]; then
echo "Turning off screen on all connected devices..."
for device in `adb devices | grep device$ | cut -f1`
do
echo -n "Found device: $device ... "
getDisplayState $device
state=$?
# If the display is on, turn it off
if [ $state -eq 1 ]; then
echo "display was on, turning off"
adb -s $device shell input keyevent 26
else
echo "display was off"
fi
done
exit 0;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment