You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows 11下用Python获取蓝牙COM端口的方向与名称

问题描述

打开蓝牙设置(蓝牙和设备>设备>更多蓝牙设置)的COM端口标签页,能看到以下信息:

Port    Direction   Name
COM3    Outgoing    BCST-72-384F-SPP 'JL_SPP'
COM4    Incoming    BCST-72-384F-SPP
COM5    Outgoing    TM Wireless IF 'RNI-SPP'
COM6    Incoming    TM Wireless IF

我需要用Python获取其中的**Direction(方向)和Name(名称)**字段,但尝试了两种方法都没拿到对应信息:

  1. 使用pyserial.tools.list_ports.comports返回的结果里没有这两项,返回数据示例:
[
    {"device": "COM3", "name": "COM3", "description": "Bluetooth (COM3)", "hwid": "BTHENUM\\{00001101-0000-1000-8000-00805F9B34FB}_VID&000105D6_PID&000A\\7&3733A217&0&AE2B0004384F_C00000000", "vid": None, "pid": None, "serial_number": None, "location": None, "manufacturer": "Microsoft", "product": None, "interface": None},
    {"device": "COM4", "name": "COM4", "description": "Bluetooth (COM4)", "hwid": "BTHENUM\\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\\7&3733A217&0&000000000000_00000004", "vid": None, "pid": None, "serial_number": None, "location": None, "manufacturer": "Microsoft", "product": None, "interface": None},
    {"device": "COM5", "name": "COM5", "description": "Bluetooth (COM5)", "hwid": "BTHENUM\\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002\\7&3733A217&0&000666B332F4_C00000000", "vid": None, "pid": None, "serial_number": None, "location": None, "manufacturer": "Microsoft", "product": None, "interface": None},
    {"device": "COM6", "name": "COM6", "description": "Bluetooth (COM6)", "hwid": "BTHENUM\\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000\\7&3733A217&0&000000000000_00000005", "vid": None, "pid": None, "serial_number": None, "location": None, "manufacturer": "Microsoft", "product": None, "interface": None}
]
  1. 使用bleak扫描蓝牙设备,同样没找到相关信息,代码示例:
from bleak import BleakScanner
import asyncio

async def scan_devices():
    async with BleakScanner() as scanner:
        bt_devices = await scanner.discover()
        for device in bt_devices:
            print(device)

asyncio.run(scan_devices())

请问有没有办法用Python获取蓝牙COM端口的Direction和Name?

解决方案

在Windows系统中,蓝牙COM端口的Direction和Name信息存储在Windows注册表或可通过**WMI(Windows Management Instrumentation)**查询,以下是两种可行的实现方式:

方法1:读取Windows注册表(推荐)

蓝牙串口的核心信息直接存放在注册表指定路径下,用Python的winreg库可以直接读取,结果和蓝牙设置界面完全一致:

import winreg

def get_bluetooth_com_details():
    reg_path = r"SYSTEM\CurrentControlSet\Services\BTHPORT\Parameters\SerialPorts"
    try:
        with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path) as root_key:
            index = 0
            while True:
                try:
                    # 枚举子键,每个子键对应一个COM端口
                    com_port = winreg.EnumKey(root_key, index)
                    index += 1
                    with winreg.OpenKey(root_key, com_port) as port_key:
                        # 读取Direction和Name值
                        direction, _ = winreg.QueryValueEx(port_key, "Direction")
                        name, _ = winreg.QueryValueEx(port_key, "Name")
                        # 转换Direction数值为对应文本
                        direction_text = "Outgoing" if direction == 0 else "Incoming"
                        print(f"Port: {com_port}")
                        print(f"Direction: {direction_text}")
                        print(f"Name: {name}")
                        print("---")
                except OSError:
                    # 枚举完所有子键后退出循环
                    break
    except FileNotFoundError:
        print("未找到蓝牙串口相关注册表项")

if __name__ == "__main__":
    get_bluetooth_com_details()

方法2:通过WMI查询

使用wmi库可以从系统管理对象中获取蓝牙串口信息,需要先安装依赖:

pip install wmi

实现代码:

import wmi

def get_bluetooth_com_via_wmi():
    c = wmi.WMI()
    # 查询所有串口设备,筛选蓝牙相关条目
    for serial_port in c.Win32_SerialPort():
        if "Bluetooth" in serial_port.Description:
            # 关联获取设备的详细属性
            pnp_entity = c.Win32_PnPEntity(DeviceID=serial_port.PNPDeviceID)[0]
            print(f"Port: {serial_port.DeviceID}")
            print(f"Name: {pnp_entity.Name}")
            # 从设备配置中提取方向信息(部分系统可能需要调整字段)
            print(f"Direction: {'Outgoing' if 'Outgoing' in pnp_entity.Description else 'Incoming'}")
            print("---")

if __name__ == "__main__":
    get_bluetooth_com_via_wmi()

内容的提问来源于stack exchange,提问作者Lou K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 10:13:18