命令
adb shell dumpsys battery

设置手机非充电状态
adb shell dumpsys battery set status 1
重置
adb shell dumpsys battery reset
python代码
import csvimport subprocessimport 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)#获取电量的levelfor 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()
