无法使用 bluepy 和 Bluez 将多个 HC-08 连接到 raspberry pi

can't connect multiple HC-08 to raspberry pi using bluepy and Bluez

我正在尝试使用 csr 4.0 加密狗将两个 Arduino 设备连接到 raspberry pi。我试过 python3 和 bluepy,但总是其中之一无法连接!我也尝试同时使用 bluetoothctl 连接到它们,但我得到了这个:

Failed to connect: org.bluez.Error.Failed

由于 BLE 协议,应该可以连接多个设备! csr 4.0 dongle 和 HC-08 都是 BLE 设备!为什么我总是收到这个错误?!

如@ukBaz 所说调试 添加日志

bluetoothctl 版本:

pi@raspberrypi:~ $ bluetoothctl
[bluetooth]# version
Version 5.43

服务蓝牙状态:

● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2020-09-14 18:49:08 +0430; 14h ago
     Docs: man:bluetoothd(8)
  Process: 269 ExecStartPost=/usr/bin/sdptool add SP (code=exited, status=0/SUCCESS)
 Main PID: 264 (bluetoothd)
   Status: "Running"
   Memory: 3.2M
      CPU: 832ms
   CGroup: /system.slice/bluetooth.service
           └─264 /usr/lib/bluetooth/bluetoothd -C

Sep 14 18:49:08 raspberrypi bluetoothd[264]: Bluetooth management interface 1.14 initialized
Sep 14 18:49:08 raspberrypi bluetoothd[264]: Failed to obtain handles for "Service Changed" characteristic
Sep 14 18:49:08 raspberrypi bluetoothd[264]: Sap driver initialization failed.
Sep 14 18:49:08 raspberrypi bluetoothd[264]: sap-server: Operation not permitted (1)
Sep 14 18:49:08 raspberrypi sdptool[269]: Serial Port service registered
Sep 14 18:49:08 raspberrypi systemd[1]: Started Bluetooth service.
Sep 14 19:08:24 raspberrypi bluetoothd[264]: Unable pair since another pairing is in progress
Sep 14 19:09:43 raspberrypi bluetoothd[264]: No cache for 00:15:87:10:9A:4E
Sep 14 19:16:59 raspberrypi bluetoothd[264]: No cache for 00:15:87:10:9A:4E
Sep 14 19:17:31 raspberrypi bluetoothd[264]: No cache for 00:15:87:10:9A:4E

对于 sudo btmon -t:

Bluetooth monitor ver 5.43
= Note: Linux version 4.19.66+ (armv6l)                                                                                                                                                                                                                                    09:25:32.554465
= Note: Bluetooth subsystem version 2.22                                                                                                                                                                                                                                   09:25:32.554485
= New Index: 00:1A:7D:DA:71:10 (Primary,USB,hci0)                                                                                                                                                                                                                   [hci0] 09:25:32.554491
= Open Index: 00:1A:7D:DA:71:10                                                                                                                                                                                                                                     [hci0] 09:25:32.554495
= Index Info: 00:1A:7D:DA:71:10 (Cambridge Silicon Radio)                                                                                                                                                                                                           [hci0] 09:25:32.554502
@ MGMT Open: bluetoothd (privileged) version 1.14                                                                                                                                                                                                                 {0x0001} 09:25:32.554510
@ MGMT Open: btmon (privileged) version 1.14                                                                                                                                                                                                                      {0x0002} 09:25:32.554755
< HCI Command: LE Add Device To White List (0x08|0x0011) plen 7                                                                                                                                                                                                     [hci0] 09:26:08.930803
        Address type: Public (0x00)
        Address: A8:E2:C1:76:6A:B8 (OUI A8-E2-C1)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:08.934471
      LE Add Device To White List (0x08|0x0011) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                                                                                                                                                                                          [hci0] 09:26:08.934746
        Type: Passive (0x00)
        Interval: 60.000 msec (0x0060)
        Window: 30.000 msec (0x0030)
        Own address type: Public (0x00)
        Filter policy: Ignore not in white list (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:08.936466
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                                                              [hci0] 09:26:08.937163
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:08.944467
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 43                                                                                                                                                                                                                           [hci0] 09:26:09.007422
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Connectable undirected - ADV_IND (0x00)
        Address type: Public (0x00)
        Address: A8:E2:C1:76:6A:B8 (OUI A8-E2-C1)
        Data length: 31
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        16-bit Service UUIDs (partial): 1 entry
          Unknown (0xfff0)
        Company: not assigned (17224)
          Data: 2d30380000000000000000000000000000000000
        RSSI: 0 dBm (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                                                              [hci0] 09:26:09.007649
        Scanning: Disabled (0x00)
        Filter duplicates: Disabled (0x00)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:09.010459
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Create Connection (0x08|0x000d) plen 25                                                                                                                                                                                                           [hci0] 09:26:09.010832
        Scan interval: 60.000 msec (0x0060)
        Scan window: 60.000 msec (0x0060)
        Filter policy: White list is not used (0x00)
        Peer address type: Public (0x00)
        Peer address: A8:E2:C1:76:6A:B8 (OUI A8-E2-C1)
        Own address type: Public (0x00)
        Min connection interval: 30.00 msec (0x0018)
        Max connection interval: 50.00 msec (0x0028)
        Connection latency: 0x0000
        Supervision timeout: 420 msec (0x002a)
        Min connection length: 0.000 msec (0x0000)
        Max connection length: 0.000 msec (0x0000)
> HCI Event: Command Status (0x0f) plen 4                                                                                                                                                                                                                           [hci0] 09:26:09.019534
      LE Create Connection (0x08|0x000d) ncmd 1
        Status: Success (0x00)
> HCI Event: LE Meta Event (0x3e) plen 19                                                                                                                                                                                                                           [hci0] 09:26:09.221422
      LE Connection Complete (0x01)
        Status: Success (0x00)
        Handle: 1
        Role: Master (0x00)
        Peer address type: Public (0x00)
        Peer address: A8:E2:C1:76:6A:B8 (OUI A8-E2-C1)
        Connection interval: 50.00 msec (0x0028)
        Connection latency: 0.00 msec (0x0000)
        Supervision timeout: 420 msec (0x002a)
        Master clock accuracy: 0x00
@ MGMT Event: Device Connected (0x000b) plen 44                                                                                                                                                                                                            {0x0002} [hci0] 09:26:09.221713
        LE Address: A8:E2:C1:76:6A:B8 (OUI A8-E2-C1)
        Flags: 0x00000000
        Data length: 31
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        16-bit Service UUIDs (partial): 1 entry
          Unknown (0xfff0)
        Company: not assigned (17224)
          Data: 2d30380000000000000000000000000000000000
@ MGMT Event: Device Connected (0x000b) plen 44                                                                                                                                                                                                            {0x0001} [hci0] 09:26:09.221713
        LE Address: A8:E2:C1:76:6A:B8 (OUI A8-E2-C1)
        Flags: 0x00000000
        Data length: 31
        Flags: 0x06
          LE General Discoverable Mode
          BR/EDR Not Supported
        16-bit Service UUIDs (partial): 1 entry
          Unknown (0xfff0)
        Company: not assigned (17224)
          Data: 2d30380000000000000000000000000000000000
< HCI Command: LE Read Remote Used Features (0x08|0x0016) plen 2                                                                                                                                                                                                    [hci0] 09:26:09.229846
        Handle: 1
> HCI Event: Command Status (0x0f) plen 4                                                                                                                                                                                                                           [hci0] 09:26:09.232463
      LE Read Remote Used Features (0x08|0x0016) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Remove Device From White List (0x08|0x0012) plen 7                                                                                                                                                                                                [hci0] 09:26:09.232668
        Address type: Public (0x00)
        Address: A8:E2:C1:76:6A:B8 (OUI A8-E2-C1)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:09.235453
      LE Remove Device From White List (0x08|0x0012) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                                                                                                                                                                                          [hci0] 09:26:09.235831
        Type: Passive (0x00)
        Interval: 60.000 msec (0x0060)
        Window: 30.000 msec (0x0030)
        Own address type: Public (0x00)
        Filter policy: Ignore not in white list (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:09.237589
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                                                              [hci0] 09:26:09.237989
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:09.240489
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Command Disallowed (0x0c)
> HCI Event: LE Meta Event (0x3e) plen 12                                                                                                                                                                                                                           [hci0] 09:26:09.311465
      LE Read Remote Used Features (0x04)
        Status: Success (0x00)
        Handle: 1
        Features: 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00
          LE Encryption
< ACL Data TX: Handle 1 flags 0x00 dlen 7                                                                                                                                                                                                                           [hci0] 09:26:09.357158
      ATT: Exchange MTU Request (0x02) len 2
        Client RX MTU: 517
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7                                                                                                                                                                                                          [hci0] 09:26:09.374549
        Type: Passive (0x00)
        Interval: 60.000 msec (0x0060)
        Window: 30.000 msec (0x0030)
        Own address type: Public (0x00)
        Filter policy: Ignore not in white list (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:09.376446
      LE Set Scan Parameters (0x08|0x000b) ncmd 1
        Status: Success (0x00)
< HCI Command: LE Set Scan Enable (0x08|0x000c) plen 2                                                                                                                                                                                                              [hci0] 09:26:09.376660
        Scanning: Enabled (0x01)
        Filter duplicates: Enabled (0x01)
> HCI Event: Command Complete (0x0e) plen 4                                                                                                                                                                                                                         [hci0] 09:26:09.378464
      LE Set Scan Enable (0x08|0x000c) ncmd 1
        Status: Command Disallowed (0x0c)
@ MGMT Event: Connect Failed (0x000d) plen 8                                                                                                                                                                                                               {0x0002} [hci0] 09:26:09.379025
        LE Address: 00:15:87:10:9A:4E (Takenaka Seisakusho Co.,Ltd)
        Status: Busy (0x0a)
@ MGMT Event: Connect Failed (0x000d) plen 8                                                                                                                                                                                                               {0x0001} [hci0] 09:26:09.379025
        LE Address: 00:15:87:10:9A:4E (Takenaka Seisakusho Co.,Ltd)
        Status: Busy (0x0a)
< HCI Command: LE Set Scan Parameters (0x08|0x000b) plen 7  

应该可以同时将 BLE 设备连接到您的 Raspberry Pi。您提到了 CSR 4.0 加密狗,所以您的设置可能与典型设置有所不同,因为您有两个适配器?

我怀疑如果您列出了您的 Bluez 服务,您将有 hci0hci1 代表内置适配器和 CSR 加密狗。列表中还将包含您已配对的设备。您需要为要连接的设备使用正确的 hci 路径

pi@raspberrypi:~/stack_overflow $ busctl tree org.bluez
└─/org
  └─/org/bluez
    ├─/org/bluez/hci0
    ├─/org/bluez/hci1
    │ ├─/org/bluez/hci1/dev_DC_DB_16_6B_8C_5F
    │ └─/org/bluez/hci1/dev_DE_82_35_E7_43_BE
    └─/org/bluez/test

您可以使用以下命令查看有关适配器的更多详细信息:

pi@raspberrypi:~/stack_overflow $ busctl introspect org.bluez /org/bluez/hci1 org.bluez.Adapter1
NAME                 TYPE      SIGNATURE RESULT/VALUE                             FLAGS
.ConnectDevice       method    a{sv}     -                                        -
.GetDiscoveryFilters method    -         as                                       -
.RemoveDevice        method    o         -                                        -
.SetDiscoveryFilter  method    a{sv}     -                                        -
.StartDiscovery      method    -         -                                        -
.StopDiscovery       method    -         -                                        -
.Address             property  s         "00:02:5B:03:44:07"                      emits-change
.AddressType         property  s         "public"                                 emits-change
.Alias               property  s         "raspberrypi #2"                         emits-change writable
.Class               property  u         4718592                                  emits-change
.Discoverable        property  b         false                                    emits-change writable
.DiscoverableTimeout property  u         180                                      emits-change writable
.Discovering         property  b         false                                    emits-change
.Modalias            property  s         "usb:v1D6Bp0246d0532"                    emits-change
.Name                property  s         "raspberrypi #2"                         emits-change
.Pairable            property  b         true                                     emits-change writable
.PairableTimeout     property  u         0                                        emits-change writable
.Powered             property  b         true                                     emits-change writable
.UUIDs               property  as        9 "00001112-0000-1000-8000-00805f9b34fb… emits-change

您可以在 bluetootctl 中列出适配器(但不是路径):

pi@raspberrypi:~/stack_overflow $ bluetoothctl list
Controller 00:02:5B:03:44:07 raspberrypi #2 [default]
Controller B8:27:EB:22:57:E0 SeeMe 

为了从 Python 控制 BlueZ,我使用 pydbus 库。假设您的设备在 hci1 上,那么下面的代码应该可以工作(假设您已经与设备配对)。 如果您的设备在 hci0 上,那么您将需要修改设备路径。

from signal import pause
import pydbus

DEVICE_ADDR_1 = 'DE:82:35:E7:43:BE'
DEVICE_ADDR_2 = 'DC:DB:16:6B:8C:5F'


# DBus object paths
BLUEZ_SERVICE = 'org.bluez'
ADAPTER_PATH = '/org/bluez/hci1' # Is your CSR dongle this path?
dev1_path = f"{ADAPTER_PATH}/dev_{DEVICE_ADDR_1.replace(':', '_')}"
dev2_path = f"{ADAPTER_PATH}/dev_{DEVICE_ADDR_2.replace(':', '_')}"

# setup dbus
bus = pydbus.SystemBus()
adapter = bus.get(BLUEZ_SERVICE, ADAPTER_PATH) 
device_1 = bus.get(BLUEZ_SERVICE, dev1_path)
device_2 = bus.get(BLUEZ_SERVICE, dev2_path)

try:
    device_1.Connect()
    device_2.Connect()
    print('Connected...')
    pause()
except KeyboardInterrupt:
    print('Disconnecting...')
    device_1.Disconnect()
    device_2.Disconnect()

我在尝试连接时偶尔会收到以下错误报告:

pi@raspberrypi:~/stack_overflow $ python3 multi_adapters.py 
Traceback (most recent call last):
  File "multi_adapters.py", line 20, in <module>
    device_1.Connect()
  File "/usr/local/lib/python3.7/dist-packages/pydbus/proxy_method.py", line 75, in __call__
    0, timeout_to_glib(timeout), None).unpack()
gi.repository.GLib.Error: g-io-error-quark: GDBus.Error:org.bluez.Error.Failed: Input/output error (36)