Python利用蓝牙设备类型数值识别设备类别的方法

沈知远
沈知远
发布时间:2026-10-13| 4人浏览过
原创

本文介绍如何使用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'

最终效果如下,若需显示中文名称,可将映射结果修改为中文。如果觉得有帮助,请点赞支持。

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。