0

我正在尝试将我的 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,我迷路了。

感谢所有帮助者

4

1 回答 1

1

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

另一个解决方案来自这篇文章。我改变了课堂部分

/etc/bluetooth/main.conf

到:

Class = 0x400100

就是这样。我什至不需要在重新启动(Arduino 或 Pi)后配对我的设备。此代码连接 Pi 和 Arduino 并发送所有命令。

于 2019-06-03T15:03:14.787 回答