无法将套接字连接到蓝牙配对设备

时间:2019-06-03 13:27:45

标签: python python-3.x bluetooth raspberry-pi hc-05

我正在尝试将Raspberry Pi 3B连接到具有HC-05蓝牙芯片以发送命令的Arduino。 我已经使用

在HC-05和Pi之间成功配对
Device 98:7B:F3:57:76:34
    Name: BT05
    Alias: BT05
    Paired: yes
    Trusted: yes
    Blocked: no
    Connected: yes
    LegacyPairing: no
    UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
    UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
    UUID: Device Information        (0000180a-0000-1000-8000-00805f9b34fb)
    UUID: Unknown                   (0000ffe0-0000-1000-8000-00805f9b34fb)
    Modalias: bluetooth:v000Dp0000d0110

现在,我正在尝试使用Python发送命令。我的代码是:

import bluetooth

bd_addr = "98:7B:F3:57:76:34"

def connect ():
    port = 1
    sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    print("Trying to pair to", bd_addr)
    sock.connect((bd_addr, port))
    a = "a"
    while a != 'quit':
        a = input("<<< ")
        sock.send(a)
    sock.close()

connect()

我在运行代码时遇到异常,说主机已关闭,但我找不到问题:

python3 tests/bt.py 
Trying to pair to 98:7B:F3:57:76:34
Traceback (most recent call last):
  File "<string>", line 3, in connect
_bluetooth.error: (112, 'Host is down')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "tests/bt.py", line 16, in <module>
    connect()
  File "tests/bt.py", line 9, in connect
    sock.connect((bd_addr, port))
  File "<string>", line 5, in connect
bluetooth.btcommon.BluetoothError: (112, 'Host is down')

我曾尝试更换HC-05设备并重新启动蓝牙服务和Pi,但我似乎仍然无法连接到Arduino,我迷路了。

感谢所有助手

1 个答案:

答案 0 :(得分:1)

我通过做两件事解决了这个问题: 首先,我的5个HC-05模块中只有一个是可以的,这使得解决另一半问题非常困难。

另一个解决方案来自this帖子。 我在

中更改了课程部分
/etc/bluetooth/main.conf

收件人:

Class = 0x400100

就是这样。重新启动(Arduino或Pi)后,甚至不需要配对设备。这段代码将Pi和Arduino连接起来并发送所有命令。