命令
adb shell dumpsys battery
设置手机非充电状态
adb shell dumpsys battery set status 1
重置
adb shell dumpsys battery reset
python代码
import csv
import subprocess
import time
#控制类
class Controller(object):
def __init__(self, count):
#定义测试的次数
self.counter = count
#定义收集数据的数组
self.alldata = [("timestamp", "power")]
#单次测试过程
def testprocess(self):
#执行获取电量的命令
result = subprocess.Popen("adb shell dumpsys battery",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
#获取电量的level
for line in result.stdout.readlines():
line = line.decode('utf-8')
if "level" in line:
power = line.split(":")[1]
power = power.strip()
print("power===",power)
break
#获取当前时间
currenttime = self.getCurrentTime()
#将获取到的数据存到数组中
self.alldata.append((currenttime, power))
#多次测试过程控制
def run(self):
#设置手机进入非充电状态
subprocess.Popen("adb shell dumpsys battery set status 1",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
while self.counter >0:
self.testprocess()
self.counter = self.counter - 1
#每5秒钟采集一次数据
time.sleep(1)
#获取当前的时间戳
def getCurrentTime(self):
currentTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return currentTime
#数据的存储
def SaveDataToCSV(self):
csvfile = open('battary.csv', 'w',newline='')
writer = csv.writer(csvfile)
writer.writerows(self.alldata)
csvfile.close()
if __name__ == "__main__":
controller = Controller(5)
controller.run()
controller.SaveDataToCSV()