Python利用蓝牙设备类型数值识别设备类别的方法
本文介绍如何使用Python的bluetooth模块扫描蓝牙设备,并重点解析扫描结果中的设备类型数值(如5898764)。通过对比特定数值与字符串标识,可将未知设备准确分类为Android、iOS、电脑或耳机等类型,提供完整的映射逻辑参考。
Python蓝牙设备类型识别概述
Python 获取蓝牙设备类型
在扫描蓝牙设备获取的信息中,通常无法直接判断扫描到的设备具体属于哪一类类型。
这里使用的蓝牙信息扫描功能依赖于 Python 内置的 bluetooth 模块。
扫描数据与类型数值解析
首先,扫描出来的原始数据结构如下所示。
('74:60:FA:FD:FC:49','HUAWEI P30',5898764)
可以通过分析数值 5898764 来判断该蓝牙设备的具体类型。
经过对多种设备的收集与测试,可以成功将其转化为对应的设备类型标识。
设备类型映射代码实现
def bt_device_type(device_type):
if device_type == 5898764 or device_type == 'Android':
return 'Android'
if device_type == 7078144 or device_type == 'computer_ubuntu14':
return 'computer_ubuntu14'
if device_type == 786700 or device_type == 'computer_ubuntu16':
return 'computer_ubuntu16'
if device_type == 655620 or device_type == 'computer_windows':
return 'computer_windows'
if device_type == 2360324 or device_type == 'headset':
return 'headset' #耳机
if device_type == 2360328 or device_type =='speaker':
return 'speaker' # 扩音器
if device_type == 263208 or device_type == 'SV':
return 'SV' #蓝牙音响
if device_type == 7995916 or device_type == 'phone':
return 'phone' #苹果设备
if device_type == 3670284 or device_type == 'MACBook':
return 'MACBook'
if device_type == 7936 or device_type == 2752780 or device_type == 'PC':
return 'PC'
if device_type == 6947088 or device_type == 'iPad':
return 'iPad'
return 'unknown'
最终效果如下,若需显示中文名称,可将映射结果修改为中文。如果觉得有帮助,请点赞支持。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













