adb獲取android裝置資訊
阿新 • • 發佈:2018-12-17
import subprocess #ADB執行命令列,包括一些常用命令 class ADBShell: def __init__(self, adb_path=""): self.adb_path = adb_path def invoke(self,cmd): output, errors = subprocess.Popen(self.adb_path+" "+cmd, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate() o = output.decode("utf-8") return o def getDevices(self): android_devices_list = [] for device in self.invoke('devices').splitlines(): if 'device' in device and 'devices' not in device: device = device.split('\t')[0] android_devices_list.append(device) returnandroid_devices_list class Device: def __init__(self,adb_shell,device_id): self.device_id = device_id self.adb_shell = adb_shell def get_device_info(self): device_adb = DeviceADB(self.adb_shell,self.device_id) return DeviceInfo(self.device_id, "Android", device_adb.get_android_version(), device_adb.get_sdk_version(),device_adb.get_product_brand(), device_adb.get_product_model(),device_adb.get_product_rom()) classDeviceInfo: def __init__(self, uid, os_type, os_version, sdk_version, brand, model, rom_version): self.uid = uid self.os_type = os_type self.os_version = os_version self.sdk_version = sdk_version self.brand = brand self.model = model self.rom_version = rom_version def __repr__(self): str = "裝置ID:"+self.uid+"\n" str = str+"作業系統:"+self.os_type+"\n" str = str+"作業系統版本:"+self.os_version+"\n" str = str + "SDK版本:" + self.sdk_version + "\n" str = str + "裝置品牌:" + self.brand + "\n" str = str + "裝置型號:" + self.model + "\n" str = str + "ROM版本:" + self.rom_version return str ''' ADB options on a devices init_param:the device id ''' class DeviceADB(object): def __init__(self,adbShell, device_id): #device_id為需要操作的裝置ID self.adbShell = adbShell self.device_id = "-s %s" % device_id def adb(self, args): #在裝置上執行adb命令 cmd = "%s %s" % (self.device_id, str(args)) return self.adbShell.invoke(cmd) def shell(self, args): cmd = "%s shell %s" % (self.device_id, str(args),) return self.adbShell.invoke(cmd) def get_device_state(self): #獲取裝置狀態: offline | bootloader | device return self.adb("get-state").stdout.read().strip() def get_device_id(self): #獲取裝置id號,return serialNo return self.adb("get-serialno").stdout.read().strip() def get_android_version(self): #獲取裝置中的Android版本號,如4.2.2 return self.shell("getprop ro.build.version.release").strip() def get_sdk_version(self): #獲取裝置SDK版本號,如:24 return self.shell("getprop ro.build.version.sdk").strip() def get_product_brand(self): #獲取裝置品牌,如:HUAWEI return self.shell("getprop ro.product.brand").strip() def get_product_model(self): #獲取裝置型號,如:MHA-AL00 return self.shell("getprop ro.product.model").strip() def get_product_rom(self): #獲取裝置ROM名,如:MHA-AL00C00B213 return self.shell("getprop ro.build.display.id").strip()