通过python通过蓝牙发送消息或数据
如何通过python通过蓝牙发送消息,而无需密钥身份验证(如类型号) 我用的是pybluez 但我有一个错误:通过python通过蓝牙发送消息或数据,python,bluetooth,Python,Bluetooth,如何通过python通过蓝牙发送消息,而无需密钥身份验证(如类型号) 我用的是pybluez 但我有一个错误: File "./send", line 12, in <module> connect() File "./send", line 8, in connect sock.connect((bd_addr, port)) File "<string>", line 5, in connect bluetooth.btcommon.Bluet
File "./send", line 12, in <module>
connect()
File "./send", line 8, in connect
sock.connect((bd_addr, port))
File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: (111, 'Connection refused')
您是否尝试过从pybluez的基本rfcomm客户端和rfcomm服务器示例代码开始
这基本上就是您的代码所做的,但它使用服务发现来确保连接到正确的端口。我也有同样的错误。绑定地址后,错误消失了
rfcomm bind 0 <address> 1
rfcomm绑定0 1
0表示您的蓝牙设备。1表示端口号。
如果您运行的是linux,则可以运行hciconfig以获取设备号。正如@TJD所说,您需要确保为所需的服务绑定了正确的端口
>>> from bluetooth import *
>>> from pprint import pprint
>>>
>>> devices = discover_devices()
>>> devices
['xx:yy:tt:zz:44:BD', '00:yy:72:zz:bb:aa']
然后,作为第二步,尝试在要连接的设备上查找服务
>>> service = find_service(address='00:yy:72:zz:bb:aa')
>>> pprint(service)
[{'description': None,
'host': '00:yy:72:zz:bb:aa',
'name': 'Headset Audio Gateway',
'port': 12,
'profiles': [('1108', 258)],
...},
{'description': None,
'host': '00:yy:72:zz:bb:aa',
'name': 'Dial-Up Networking',
'port': 1,
'profiles': [('1103', 256)],
'protocol': 'RFCOMM',
...}]
基于此信息,您可以连接到设备上运行的服务。根据服务/配置文件,您可以发送特定于服务的命令,并从设备获取信息。例如,在上面的列表中,您可以看到“耳机音频网关”和编号为“1108”的配置文件列表,这是服务的短uuid。现在,您可以查找此配置文件的命令,它应该可以工作。请使用包含堆栈跟踪的完整错误消息进行编辑。0将其绑定到/dev/rfcomm0,1将其绑定到/dev/rfcomm1。
>>> service = find_service(address='00:yy:72:zz:bb:aa')
>>> pprint(service)
[{'description': None,
'host': '00:yy:72:zz:bb:aa',
'name': 'Headset Audio Gateway',
'port': 12,
'profiles': [('1108', 258)],
...},
{'description': None,
'host': '00:yy:72:zz:bb:aa',
'name': 'Dial-Up Networking',
'port': 1,
'profiles': [('1103', 256)],
'protocol': 'RFCOMM',
...}]