Created
March 27, 2015 14:40
-
-
Save tomrenn/5f6c50245b3a0b40ff8b to your computer and use it in GitHub Desktop.
Python script to wakeup connected Android devices
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Simple script that uses ADB to wake and unlock any connected devices | |
""" | |
import subprocess | |
import sys | |
output = subprocess.check_output(['adb', 'devices']) | |
output = set(output.split('\n')[1:]) | |
output.remove('') | |
def send_key_event(device_id, event_code): | |
subprocess.call(['adb', '-s', device_id, 'shell', | |
'input', 'keyevent', event_code]) | |
# true if screen is already on | |
def screen_already_on(device_id): | |
try: | |
output = subprocess.check_output( | |
'adb -s {0} shell dumpsys input_method | grep mScreenOn'.format(device_id), | |
shell=True) | |
except subprocess.CalledProcessError: | |
return False; | |
return "mScreenOn=true" in output | |
# main execution | |
for device_output in output: | |
try: | |
device_id, status = device_output.split('\t') | |
if (screen_already_on(device_id)): | |
print('Screen already on for {0}'.format(device_id)) | |
else: | |
# unlock the device for use | |
print('Waking device: {0}'.format(device_id)) | |
# keyevent 26, display on | |
send_key_event(device_id, '26') | |
# call keyevent 82, unlock | |
send_key_event(device_id, '82') | |
except ValueError: | |
print(r'Device not in the right format <device_id>\t<status>') | |
print(device_output) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment