Python Python3 获取 Android 手机设备的相关硬件配置信息

xiaoxiao · 2019年09月10日 · 最后由 xiaoxiao 回复于 2019年09月11日 · 2897 次阅读

新建脚本文件 get_device_info.py,添加代码如下:

##!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author: xulinzhou
# @date  : 2019/09/10
# How to run:
# 1.Connect your unique device
# 2.Execute terminal commands:python3 get_device_info.py

import subprocess
from decimal import Decimal
import re


# 执行adb shell
def adb_shell(cmd):
    # 执行cmd命令,如果成功,返回(0, 'xxx');如果失败,返回(1, 'xxx')
    result = subprocess.getstatusoutput(cmd)
    return result


# 获取设备系统model
def get_sys_model():
    cmd = 'adb shell getprop | grep "\[ro.product.model\]:"' # do not use: adb shell getprop | grep "[ro.product.model]:"
    result = adb_shell(cmd)
    if result[0] == 0:
        sys_model = result[1][21:-1]
        return sys_model
    else:
        return result[1]


# 获取设备系统版本
def get_sys_version():
    cmd = 'adb shell getprop ro.build.version.release'
    result = adb_shell(cmd)
    if result[0] == 0:
        return result[1]
    else:
        return result[1]


# 获取设备系统api版本
def get_sys_api():
    cmd = 'adb shell getprop ro.build.version.sdk'
    result = adb_shell(cmd)
    if result[0] == 0:
        return result[1]
    else:
        return result[1]


#获取设备屏幕分辨率
def get_device_image_resolution():
    cmd = 'adb shell wm size | grep "Physical size:"'
    result = adb_shell(cmd)
    if result[0] == 0:
        device_dpi = result[1][15:]
        return device_dpi
    else:
        return result[1]


# 获取设备屏幕dpi密度
def get_device_dpi():
    # # 方法一:大概0.7~0.8s,所以我们用后面的方法二
    # cmd = 'adb shell wm density'
    # dpi = shell(cmd)[:-1].split(':')[1][1:] + 'dpi'
    # 方法二:大概0.06~0.07s
    cmd = 'adb shell dumpsys window displays | grep "init"'
    result = adb_shell(cmd)
    if result[0] == 0:
        dpi_info = result[1][:-1]
        dpi = dpi_info.strip().split(' ')[1]
        return dpi
    else:
        return result[1]


# 获取设备屏幕宽高比
def get_device_screen_ratio():
    device_image_resolution = get_device_image_resolution()
    m = re.match(r'\d{3,4}x\d{3,4}', device_image_resolution)
    if m:
        width = int(device_image_resolution.split('x')[0])
        height = int(device_image_resolution.split('x')[1])
        if height/width == 3/2:
            return '3:2'
        elif height/width == 16/9:
            return '16:9'
        elif height/width == 15/9:
            return '15:9'
        elif height/width == 16/10:
            return '16:10'
        elif height/width == 18/9:
            return '18:9'
        elif height/width == 18.5/9:
            return '18.5:9'
        elif height/width == 18.7/9:
            return '18.7:9'
        elif height/width == 19/9:
            return '19:9'
        elif height/width == 19.5/9:
            return '19.5:9'
        return str(height) + ':' + str(width)
    else:
        return device_image_resolution


# 获取设备的cpu型号
def get_cpu_model():
    cmd = 'adb shell cat /proc/cpuinfo | grep "Hardware"'
    result = adb_shell(cmd)
    if result[0] == 0:
        if result[1] != '':
            cpu_model = result[1][11:]
        else:
            cmd = 'adb shell getprop | grep "ro.boot.hardware"'
            cpu_model = adb_shell(cmd)[1][21:-1]
        return cpu_model
    else:
        return result[1]


# 设备支持的cpu架构
def get_cpu_abi():
    cmd = 'adb shell getprop | grep "ro.product.cpu.abilist"'
    result = adb_shell(cmd)
    if result[1] == 0:
        cpu_abi = result[1][:-1]
        return cpu_abi
    else:
        return result[1]


# 获取gpu信息
def get_gpu_info():
    cmd = 'adb shell dumpsys | grep GLES'
    result = adb_shell(cmd)
    if result[0] == 0:
        gpu_info = result[1].split('GLES: ')
        if len(gpu_info) > 1:
            gpu_info = gpu_info[1]
        return gpu_info
    else:
        return result[1]


# 获取设备内存使用情况
def get_device_meminfo():
    cmd = 'adb shell dumpsys meminfo | grep -E "Total RAM|Free RAM"'
    result = adb_shell(cmd)
    if result[0] == 0:
        device_meminfo = result[1][:-1].replace(' ', '').replace(',', '').split('\n')
        mem_free = int(int(device_meminfo[1].split(':')[1].split('(')[0][:-2])/1024)
        mem_total = int(int(device_meminfo[0].split(':')[1].split('(')[0][:-2])/1024)
        meminfo = str(mem_free) + 'MB / ' + str(mem_total) + 'MB'
        return meminfo
    else:
        return result[1]


# 获取设备存储占用情况
def get_device_diff():
    cmd = 'adb shell df /data | sed -n \'2p\' | awk \'{print $2,$4}\''
    result = adb_shell(cmd)
    if result[0] == 0:
        if 'error:' in result[1] or 'no devices/emulators found' in result[1]:
            return result[1]
        else:
            device_diff = result[1][:-1]
            diff_free = device_diff.split(' ')[1]
            diff_total = device_diff.split(' ')[0]
            if 'GB' in diff_total or 'MB' in diff_total or 'G' in diff_total:
                diff = str(diff_free) + ' / ' + str(diff_total)
            else:
                diff = str(Decimal("%.2f" % (float(diff_free)/(1024*1024)))) + 'GB / ' + str(Decimal("%.2f" % (float(diff_total)/(1024*1024)))) + 'GB'
            return diff
    else:
        return result[1]


print('please wait...\n')
print('设备系统model:' + str(get_sys_model()))
print('设备系统版本:' + str(get_sys_version()))
print('设备系统api:' + str(get_sys_api()))
print('设备分辨率:' + str(get_device_image_resolution()))
print('设备屏幕dpi密度:' + str(get_device_dpi()))
print('设备屏幕宽高比:' + str(get_device_screen_ratio()))
print('设备cpu类型:' + str(get_cpu_model()))
print('设备支持的cpu架构:\n' + str(get_cpu_abi()))
print('设备GPU信息(获取耗时约15~30秒,请等候...):')
print(str(get_gpu_info()))
print('设备内存占用情况:' + str(get_device_meminfo()))
print('设备存储占用情况:' + str(get_device_diff()))
print('\nfinish!!!')

连接一台 Android 设备到电脑,然后终端执行python3 get_device_info.py,结果如下(我的是魅族 16):

~ $ python3 get_device_info.py 
please wait...

设备系统model:16th
设备系统版本:8.1.0
设备系统api:27
设备分辨率:1080x2160
设备屏幕dpi密度:480dpi
设备屏幕宽高比:18:9
设备cpu类型:Qualcomm Technologies, Inc SDM845
设备支持的cpu架构:
[ro.product.cpu.abilist]: [arm64-v8a,armeabi-v7a,armeabi]
[ro.product.cpu.abilist32]: [armeabi-v7a,armeabi]
[ro.product.cpu.abilist64]: [arm64-v8a]
设备GPU信息(获取耗时约15~30秒,请等候...):
Qualcomm, Adreno (TM) 630, OpenGL ES 3.2 V@300.0 (GIT@7eec766, I725ebfe152) (Date:11/28/18)
设备内存占用情况:351MB / 566MB
设备存储占用情况:3.15GB / 51.89GB

finish!!!
共收到 2 条回复 时间 点赞

赞 周爷

rhyme 回复

大佬好!程哥

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册