python中的蓝牙设备名称和对应的串口名称

Que*_*ark 5 python windows serial-port bluetooth spp

我有一个支持蓝牙的设备,其用户友好名称为“Sensor1”。该设备使用 SPP 配置文件。为了要求设备通过蓝牙开始数据流,我必须在与该设备对应的COM端口上写入“10111011”,如下所示:

ser = serial.Serial('COM5') 
ser.write('10111011')     
Run Code Online (Sandbox Code Playgroud)

问题是我不知道哪个 COM 端口对应于“Sensor1”。因此,我读取 Windows 注册表来获取设备名称:

import _winreg as reg
from itertools import count

key = reg.OpenKey(reg.HKEY_LOCAL_MACHINE, 'HARDWARE\\DEVICEMAP\\SERIALCOMM')
for i in count():
    device, port = reg.EnumValue(key, i)[:2]
    print "Device name \"%s\" found at %s" % (device, port)
Run Code Online (Sandbox Code Playgroud)

我得到的只是:

Device name \Device\Serial0 found at COM3
Device name \Device\BthModem16 found at COM4
Device name \Device\BthModem17 found at COM5
Run Code Online (Sandbox Code Playgroud)

如何获取设备名称,如下所示:

service = bluetooth.find_service()
print service["name"]
Run Code Online (Sandbox Code Playgroud)

Tor*_*xed 2

import bluetooth
decices = bluetooth.discover_devices()
Run Code Online (Sandbox Code Playgroud)

使用以下库: https: //pypi.python.org/pypi/PyBluez/
以下是一些很好的用法示例: https: //people.csail.mit.edu/albert/bluez-intro/c212.html

如果您对使用其他库不感兴趣,您可以随时尝试提取与 Windows 相关的发现功能,可在此处找到: https: //github.com/karulis/pybluez/blob/2a22e61fb21c27b47898c2674662de65162b485f/bluetooth/widcomm.py# L109