adb command in self timer tutorial 43 read hardware configuration with one click

During Android system testing,
For example, the iccid or uuid of the device is needed for interface testing,
VIN number (vehicle unique identification number) is required for on-board test,
The configuration word parameters of IMEI number and other equipment shall be designed for 4G test,
We can also read the resolution of the device, emmc disk capacity,
cpu model, memory size, etc., these hardware configuration information,
It can help us to do tests. How can we read these hardware information?


Preparation stage
  1. The adb shell getprop command can obtain the iccid, imei and emmc disk capacity of Android devices
  2. adb shell wm size can read the resolution
  3. adb shell cat /proc/cpuinfo can read cpu related information
  4. adb shell cat /proc/meminfo or adb shell free can read memory related information
  5. The os.popen() function can quickly call the above command and get the output string.
  6. python's re regular expression can easily perform powerful string processing such as data matching, searching, etc

Introduction to getprop command

The getprop command is an android built-in dynamic access to system property parameters,
It includes software, hardware attribute parameters, etc. it has several execution modes, including:
Usage 1: getprop gets all system property parameters,
For example, note 5 of Meizu getprop we save as a txt file (click download)

Usage 2: getprop property name, which can obtain the specific parameter information of a specified property, such as:

Correspondingly, the setprop command is also used to set specific parameters of a property,
In general, it is not recommended to set property parameters at will.

Python batch script form
# coding=utf-8

import os
import re

# Get iccid
try:
    iccid_1 = os.popen("adb shell getprop persist.radio.cfu.iccid.0").read().strip()
    iccid_2 = os.popen("adb shell getprop persist.radio.cfu.iccid.1").read().strip()
except:
    iccid_1 = "Unknown"
    iccid_2 = "Unknown"
print("iccid_1: %s" % iccid_1)
print("iccid_2: %s" % iccid_2)

# Get imei
try:
    imei = os.popen("adb shell getprop ril.gsm.imei").read().strip().split(",")
except:
    imei = ["Unknown", "Unknown"]
print("IMEI_1: %s" % imei[0])
print("IMEI_2: %s" % imei[1])

# Get EMMC disk capacity
try:
    emmc_size = os.popen("adb shell getprop vold.emmc_size").read().strip()
    emmc_size = str(int(int(emmc_size) / 1024 / 1024 / 1024))
except Exception as e:
    print(e)
    emmc_size = "Unknown"
print("EMMC_Size: %s GB" % emmc)

# Get the memory size of Android device
ram_size = os.popen("adb shell cat /proc/meminfo").read()
try:
    ram_size = re.findall(r"MemTotal:(.*)", ram_size)[0].strip()
    ram_size = re.sub("kB", "", ram_size)
    ram_size = str(round(int(ram_size) / 1024 / 1024))
except Exception as e:
    print(e)
    ram_size = "Unknown"
print("RAM Size: %s GB" % ram_size)

# Get the CPU model of Android device
cpu_profile = os.popen("adb shell cat /proc/cpuinfo").read()
try:
    cpu_profile = re.findall(r"Hardware\s+:(.*)", cpu_profile)[0].strip()
except:
    cpu_profile = "Unknown"
print("CPU Profile: %s" % cpu_profile)

# Get the resolution of Android device
resolution = os.popen("adb shell wm size").read()
resolution = resolution.split(":")[1].strip()
print("Resolution: %s" % resolution)

os.system("pause")

try ...
except Exception as e is used for debugging,
That is to say, when a normal exception occurs, print(e) displays the error content,
In case of exception, the corresponding hardware information will be assigned as "Unknown",
It helps to improve the robustness of the program!

Python procedure oriented function form

This improvement:

  1. os.popen("command"). read() is used too much. We need to consider encapsulation as a function.
  2. Obtain the statement blocks of all kinds of hardware equipment configuration, and make them into functions independently.
# coding=utf-8

import os
import re


# Execute the command independently and get its output string
def get_popen(command):
    p_str = os.popen("%s" % command).read()
    return p_str.strip()


def get_iccid():  # Get iccid
    try:
        iccid_1 = get_popen("adb shell getprop persist.radio.cfu.iccid.0")
        iccid_2 = get_popen("adb shell getprop persist.radio.cfu.iccid.1")
    except:
        iccid_1 = "Unknown"
        iccid_2 = "Unknown"
    return iccid_1, iccid_2


def get_imei():  # Get imei
    try:
        imei = get_popen("adb shell getprop ril.gsm.imei").split(",")
    except:
        imei = ["Unknown", "Unknown"]
    return imei


def get_emmc_size():  # Get EMMC disk capacity
    try:
        emmc_size =get_popen("adb shell getprop vold.emmc_size")
        emmc_size = str(int(int(emmc_size) / 1024 / 1024 / 1024))
    except:
        emmc_size = "Unknown"
    return emmc_size


def get_ram_size():  # Get the memory size of Android device
    ram_size = get_popen("adb shell cat /proc/meminfo")
    try:
        ram_size = re.findall(r"MemTotal:(.*)", ram_size)[0].strip()
        ram_size = re.sub("kB", "", ram_size)
        ram_size = str(round(int(ram_size) / 1024 / 1024))
    except:
        ram_size = "Unknown"
    return ram_size


def get_cpu_profile():  # Get the CPU model of Android device
    cpu_profile = get_popen("adb shell cat /proc/cpuinfo")
    try:
        cpu_profile = re.findall(r"Hardware\s+:(.*)", cpu_profile)[0].strip()
    except:
        cpu_profile = "Unknown"
    return cpu_profile


def get_resolution():  # Get the resolution of Android device
    resolution = get_popen("adb shell wm size")
    resolution = resolution.split(":")[1].strip()
    return resolution


iccid_1, iccid_2 = get_iccid()
imei = get_imei()
emmc_size = get_emmc_size()
ram_size = get_ram_size()
cpu_profile = get_cpu_profile()
resolution = get_resolution()
print("iccid_1: %s" % iccid_1)
print("iccid_2: %s" % iccid_2)
print("IMEI_1: %s" % imei[0])
print("IMEI_2: %s" % imei[1])
print("EMMC_Size: %s GB" % emmc_size)
print("RAM Size: %s GB" % ram_size)
print("CPU Profile: %s" % cpu_profile)
print("Resolution: %s" % resolution)

os.system("pause")


The form of object-oriented class
  1. With the idea of "all things can be classified", we first abstract a class,
    Class name is generally recommended to use "noun", so we call it "HWInfoGetter",
    Represents the hardware information collector, and generally hump type (initial capital) is used to standardize the class naming.
  2. Form a good habit of class init ialization,
    During initialization, 7 properties (variables) can be initialized, and can be called at will within the scope of the class
  3. Refine the function of obtaining different hardware information
  4. Class is an abstract thing, which must be instantiated into concrete objects,
    Can be called, so we instantiate and name it as H obj, indicating that it is an object.
  5. After instantiation, the object can call the functions in this class
# coding=utf-8

import os
import re


# Execute the command independently and get its output string
def get_popen(command):
    p_str = os.popen("%s" % command).read()
    return p_str.strip()


class HWInfoGetter():
    def __init__(self):
        self.iccid_1 = None
        self.iccid_2 = None
        self.imei = None
        self.emmc_size = None
        self.ram_size = None
        self.cpu_profile = None
        self.resolution = None

    def get_iccid(self):  # Get iccid
        try:
            self.iccid_1 = get_popen("adb shell getprop persist.radio.cfu.iccid.0")
            self.iccid_2 = get_popen("adb shell getprop persist.radio.cfu.iccid.1")
        except:
            self.iccid_1 = "Unknown"
            self.iccid_2 = "Unknown"
        return self.iccid_1, self.iccid_2

    def get_imei(self):  # Get imei
        try:
            self.imei = get_popen("adb shell getprop ril.gsm.imei").split(",")
        except:
            self.imei = ["Unknown", "Unknown"]
        return self.imei

    def get_emmc_size(self):  # Get EMMC disk capacity
        try:
            self.emmc_size = get_popen("adb shell getprop vold.emmc_size")
            self.emmc_size = str(int(int(emmc_size) / 1024 / 1024 / 1024))
        except:
            self.emmc_size = "Unknown"
        return self.emmc_size

    def get_ram_size(self):  # Get the memory size of Android device
        self.ram_size = get_popen("adb shell cat /proc/meminfo")
        try:
            self.ram_size = re.findall(r"MemTotal:(.*)", self.ram_size)[0].strip()
            self.ram_size = re.sub("kB", "", self.ram_size)
            self.ram_size = str(round(int(self.ram_size) / 1024 / 1024))
        except:
            self.ram_size = "Unknown"
        return self.ram_size

    def get_cpu_profile(self):  # Get the CPU model of Android device
        cpu_profile = get_popen("adb shell cat /proc/cpuinfo")
        try:
            self.cpu_profile = re.findall(r"Hardware\s+:(.*)", cpu_profile)[0].strip()
        except:
            self.cpu_profile = "Unknown"
        return self.cpu_profile

    def get_resolution(self):  # Get the resolution of Android device
        self.resolution = get_popen("adb shell wm size")
        self.resolution = self.resolution.split(":")[1].strip()
        return self.resolution


if __name__ == '__main__':
    h_obj = HWInfoGetter()
    iccid_1, iccid_2 = h_obj.get_iccid()
    imei = h_obj.get_imei()
    emmc_size = h_obj.get_emmc_size()
    ram_size = h_obj.get_ram_size()
    cpu_profile = h_obj.get_cpu_profile()
    resolution = h_obj.get_resolution()
    print("iccid_1: %s" % iccid_1)
    print("iccid_2: %s" % iccid_2)
    print("IMEI_1: %s" % imei[0])
    print("IMEI_2: %s" % imei[1])
    print("EMMC_Size: %s GB" % emmc_size)
    print("RAM Size: %s GB" % ram_size)
    print("CPU Profile: %s" % cpu_profile)
    print("Resolution: %s" % resolution)
    os.system("pause")


Code operation mode

Make sure the Android device is connected to the computer through the USB cable, and the adb device is effectively connected,
The three implementation forms of the above code can be run directly, for example, save as get? HW? Info.py and put it on the desktop,
Every time you need to get the system version information, double-click to run get HW info.py,
The operation effect is as follows (take Meizu Note5 for example),

The comparison is as follows:


For more and better original articles, please visit the official website: www.zipython.com
Selfie course (Python course of automatic test, compiled by Wu Sanren)
Original link: https://www.zipython.com/#/detail?id=fdbe5cbccbb4400b959716226643367d
You can also follow the wechat subscription number of "wusanren" and accept the article push at any time.

Tags: Python shell Android Attribute

Posted on Thu, 12 Mar 2020 22:45:10 -0700 by tempi