如何通过ADB和Python获取全机型Android设备CPU代号
适配多机型的手机CPU代号获取方案
没有能100%覆盖所有机型的完美方法,但通过结合多种系统路径与命令的 fallback 机制,可以适配绝大多数设备,以下是具体实现方案:
1. 读取SOC ID并映射到CPU代号
多数高通设备会在系统文件中存储SOC的数字ID,通过ID与代号的映射表即可得到CPU型号:
- 核心ADB命令:
adb shell cat /sys/devices/soc0/soc_id # 备用路径 adb shell cat /sys/devices/system/soc/soc0/id - Python调用示例:
import subprocess def get_soc_id(): try: # 优先尝试第一个路径 result = subprocess.check_output( ["adb", "shell", "cat", "/sys/devices/soc0/soc_id"], stderr=subprocess.STDOUT, text=True ).strip() if result.isdigit(): return result # 路径不存在则尝试备用路径 result = subprocess.check_output( ["adb", "shell", "cat", "/sys/devices/system/soc/soc0/id"], stderr=subprocess.STDOUT, text=True ).strip() return result if result.isdigit() else None except subprocess.CalledProcessError: return None # 需自行补充更多SOC ID与代号的映射关系 soc_id_map = { "336": "MSM8996", "294": "MSM8976", "463": "SM8150", "393": "SDM845" } soc_id = get_soc_id() if soc_id and soc_id in soc_id_map: print(f"CPU代号: {soc_id_map[soc_id]}") else: print("未识别到对应CPU代号,请补充映射表")
2. 解析设备树兼容信息
部分设备会在设备树文件中直接标注SOC型号,输出内容包含类似qcom,msm8996的字段:
- 核心ADB命令:
adb shell cat /proc/device-tree/compatible - Python处理示例:
def get_cpu_from_device_tree(): try: output = subprocess.check_output( ["adb", "shell", "cat", "/proc/device-tree/compatible"], stderr=subprocess.STDOUT, text=True ) for line in output.splitlines(): if "qcom," in line: return line.split("qcom,")[-1].strip() return None except subprocess.CalledProcessError: return None
3. 多方法 fallback 组合逻辑
将多种获取方式按优先级组合,依次尝试直到得到有效结果,最大化适配范围:
def get_cpu_code(): # 1. 先尝试设备树解析 code = get_cpu_from_device_tree() if code: return code # 2. 尝试SOC ID映射 soc_id = get_soc_id() if soc_id in soc_id_map: return soc_id_map[soc_id] # 3. 尝试系统属性字段 try: platform = subprocess.check_output( ["adb", "shell", "getprop", "ro.board.platform"], stderr=subprocess.STDOUT, text=True ).strip() if platform and not platform.startswith("["): return platform except: pass # 4. 最后尝试/proc/cpuinfo try: cpuinfo = subprocess.check_output( ["adb", "shell", "cat", "/proc/cpuinfo"], stderr=subprocess.STDOUT, text=True ) for line in cpuinfo.splitlines(): if line.startswith("Hardware"): return line.split(":")[-1].strip() except: pass return "无法获取CPU代号"
补充说明
部分定制ROM会隐藏或修改系统文件路径,无法做到完全适配,但上述组合方案已能覆盖绝大多数主流品牌与机型。SOC ID映射表可通过收集不同设备的测试数据逐步完善。
内容的提问来源于stack exchange,提问作者
相关产品推荐
相关产品推荐

