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

0

I'm trying to connect two Arduino device to the raspberry pi using the csr 4.0 dongle. I've tried python3 and bluepy, but always one of them failed to connect! I've also tried to connect to them using bluetoothctl at the same time, but I got this:

Failed to connect: org.bluez.Error.Failed

Due to the BLE protocol it should be possible to connect multiple devices! both csr 4.0 dongle and HC-08 are BLE devices! why i keep getting this error?!

adding logs for debugging as the @ukBaz said

for bluetoothctl version:

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

for service bluetooth status:

● 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

for 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  
python
bluetooth
raspberry-pi
bluez
pybluez
asked on Stack Overflow Sep 14, 2020 by kiarash_kiani • edited Nov 14, 2020 by kiarash_kiani

1 Answer

0

It should be possible to connect to BLE devices to your Raspberry Pi at the same time. You mention about the CSR 4.0 dongle so your setup might vary from a typical setup because you have two adapters?

I suspect if you listed your Bluez service you will have hci0 and hci1 representing the inbuilt adapter and the CSR dongle. In the list will also be the devices that you have paired with. You will need to use the correct hci path for the devices you want to connect to

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

You can see more details on the adapter with the following command:

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

You can list the adapters (but not the paths) in 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 

For controlling BlueZ from Python I use the pydbus library. Assuming your devices are on hci1 then the following code should work (assuming you have paired with the devices already). If your devices are on hci0 then you will need to modify the device path.

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()

Very occasionally I got the the following error reported when trying to conenct:

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)
answered on Stack Overflow Sep 14, 2020 by ukBaz • edited Sep 14, 2020 by ukBaz

User contributions licensed under CC BY-SA 3.0