[方法提炼] 获取Android设备序列号方法

知识为进步之母,而进步又为富强之源泉。这篇文章主要讲述[方法提炼] 获取Android设备序列号方法相关的知识,希望能为你提供帮助。
【[方法提炼] 获取Android设备序列号方法】通过这个方法可以检测设备是否连接成功,如果有一台或者多台设备,可以将所有设备序列号全部输出

1 # -*- coding:utf-8 -*- 2 import os 3 4 def attachDeviceList(): 5device_list = [] 6cmd_output = os.popen("adb devices","r").read().split()[4:]# 从输出的列表中第5个开始为设备序列号,将后面所有内容全部输出 7for i in range(len(cmd_output)): 8if i%2 == 0:# 列表位置为偶数的全部为设备序列号,将device字符除去 9#print (cmd_output[i]) 10device_list.append(cmd_output[i])# 将提取出来的序列号加入到device_list列表中 11 #print (device_list) 12if device_list:# 通过判断列表是否为真(有数据),说明获取设备序列号成功 13return device_list 14else: 15return "No device found. Please check whether the device is connect or not!" 16 17 if __name__ == "__main__": 18print (attachDeviceList())

无设备连接输出如下:
No device found. Please check whether the device is connect or not!


有一台设备连接如下:
[‘75AQP7JVAQBAHMV4‘]


有两台设备连接如下:
[‘75AQP7JVAQBAHMV4‘, ‘RWGESGIFYL8H55M7‘]


有三台设备连接如下:
暂时没有这么多设备,请自行拿代码去验证 ^_^

    推荐阅读