Skip to content

Instantly share code, notes, and snippets.

@macolyte
Created July 31, 2012 12:15
Show Gist options
  • Save macolyte/3216600 to your computer and use it in GitHub Desktop.
Save macolyte/3216600 to your computer and use it in GitHub Desktop.
Log Android Battery Status
import android
import time
from datetime import date, datetime
def log_battery_status(droid):
"""Log battery percentage"""
droid.batteryStartMonitoring()
battery = 0
while not battery:
battery = droid.batteryGetLevel().result
time.sleep(0.5)
droid.batteryStopMonitoring()
bright = droid.getScreenBrightness().result
wifi = droid.checkWifiState().result
bt = droid.checkBluetoothState().result
_date = date.today().isoformat()
_time = datetime.time(datetime.now())
_hour, _minute, _second = (add_leading_zero(x) for x in [_time.hour,_time.minute, _time.second])
date_string = "%s %s:%s:%s" % (_date, _hour, _minute, _second)
with open("/storage/sdcard0/Nexus7/battery_log.txt", "a") as f:
f.write("%s\t%d\t%d\t%d\t%d\n" % (date_string, battery, bright, wifi, bt))
def add_leading_zero(num):
num = str(num)
return "0" + num if len(num) == 1 else num
if __name__ == '__main__':
droid = android.Android()
log_battery_status(droid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment