hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio
MIT License
1.84k stars 302 forks source link

examples/service_explorer.py errors out with "bleak.exc.BleakDBusError: [org.bluez.Error.Failed] br-connection-unknown" #1333

Open jsmif opened 1 year ago

jsmif commented 1 year ago

Description

Trying to scan GATT and see available characteristics.

Installed with

git clone https://github.com/hbldh/bleak.git
cd bleak
pip3 install -e .
Defaulting to user installation because normal site-packages is not writeable
Obtaining file:///home/user/Downloads/bleak
  Installing build dependencies ... done
  Checking if build backend supports build_editable ... done
  Getting requirements to build editable ... done
  Preparing editable metadata (pyproject.toml) ... done
Requirement already satisfied: async-timeout<5,>=3.0.0 in /home/user/.local/lib/python3.10/site-packages (from bleak==0.21.0a1) (4.0.2)
Requirement already satisfied: dbus-fast<2.0.0,>=1.83.0 in /home/user/.local/lib/python3.10/site-packages (from bleak==0.21.0a1) (1.86.0)
Building wheels for collected packages: bleak
  Building editable for bleak (pyproject.toml) ... done
  Created wheel for bleak: filename=bleak-0.21.0a1-py3-none-any.whl size=3413 sha256=44674f94c6a828ede3bc84712d7642f6f1e99465784d2bcb6fac2c7e9073bc52
  Stored in directory: /tmp/pip-ephem-wheel-cache-0py9upgz/wheels/d4/3b/06/13cf45a652a54e73f849d6ff9a18603cdf3145a3e31f1ddfc4
Successfully built bleak
Installing collected packages: bleak
  Attempting uninstall: bleak
    Found existing installation: bleak 0.20.2
    Uninstalling bleak-0.20.2:
      Successfully uninstalled bleak-0.20.2
Successfully installed bleak-0.21.0a1

What I Did

 python3 ./service_explorer.py --address 6C:<CENSORED>
2023-06-14 12:16:02,343 __main__ INFO: starting scan...
2023-06-14 12:16:02,649 __main__ INFO: connecting to device...
Traceback (most recent call last):
  File "/home/user/Downloads/bleak/examples/./service_explorer.py", line 129, in <module>
    asyncio.run(main(args))
  File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/home/user/Downloads/bleak/examples/./service_explorer.py", line 41, in main
    async with BleakClient(
  File "/home/user/Downloads/bleak/bleak/__init__.py", line 491, in __aenter__
    await self.connect()
  File "/home/user/Downloads/bleak/bleak/__init__.py", line 531, in connect
    return await self._backend.connect(**kwargs)
  File "/home/user/Downloads/bleak/bleak/backends/bluezdbus/client.py", line 244, in connect
    assert_reply(reply)
  File "/home/user/Downloads/bleak/bleak/backends/bluezdbus/utils.py", line 22, in assert_reply
    raise BleakDBusError(reply.error_name, reply.body)
bleak.exc.BleakDBusError: [org.bluez.Error.Failed] br-connection-unknown

Logs

Unfortunately I can't provide logs as that would provide my exact physical location... Please just see if this replicates for you on Ubuntu 22.04

dlech commented 1 year ago

Same answer as your other issues - it is not really possible to determine what is going on without seeing Bluetooth packet logs.

jsmif commented 1 year ago

I think I know part of what is the problem. I think you're trying to establish a connection, when that's not actually necessary for GATT in many cases.

If I open bluetoothctl and try to connect to the same device (an AppleTV), I get this:

[bluetooth]# connect 6C:CENSORED
Attempting to connect to 6C:CENSORED
Request confirmation
[agent] Confirm passkey 474903 (yes/no): yes
Failed to connect: org.bluez.Error.Failed br-connection-unknown

Note the same br-connection-unknown error. (And there's two attempts to get me to confirm a pin, which I say yes to both times, but it still never connects.)

Basically the AppleTV is probably rejecting the connection attempt, because I'm not also going over to it and hitting OK to the pairing request on the screen. But connecting is entirely unnecessary to get basic GATT info! E.g. here's one example of a command with gatttool that is capable of inspecting the same device.

gatttool --char-desc -b 6C:CENSORED
handle = 0x0001, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x0002, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0003, uuid = 00002a00-0000-1000-8000-00805f9b34fb
handle = 0x0004, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0005, uuid = 00002a01-0000-1000-8000-00805f9b34fb
handle = 0x0006, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x0007, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0008, uuid = 00002a05-0000-1000-8000-00805f9b34fb
handle = 0x0009, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x000a, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x000b, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x000c, uuid = 00002a29-0000-1000-8000-00805f9b34fb
handle = 0x000d, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x000e, uuid = 00002a24-0000-1000-8000-00805f9b34fb
handle = 0x000f, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x0010, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0011, uuid = 8667556c-9a37-4c91-84ed-54ee27d90049
handle = 0x0012, uuid = 00002900-0000-1000-8000-00805f9b34fb
handle = 0x0013, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x0014, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x0015, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x0016, uuid = af0badb1-5b99-43cd-917a-a77bc549e3cc
handle = 0x0017, uuid = 00002900-0000-1000-8000-00805f9b34fb
handle = 0x0018, uuid = 00002902-0000-1000-8000-00805f9b34fb
handle = 0x0019, uuid = 00002800-0000-1000-8000-00805f9b34fb
handle = 0x001a, uuid = 00002803-0000-1000-8000-00805f9b34fb
handle = 0x001b, uuid = 6c251515-f290-4f91-808c-5123a0cd02e9
handle = 0x001c, uuid = 00002900-0000-1000-8000-00805f9b34fb
handle = 0x001d, uuid = 00002902-0000-1000-8000-00805f9b34fb

(And other commands such as reading specific handles work fine too.)

But of course gatttool is deprecated, which is why I'm looking for an alternative solution. Is it possible to query without connecting through bleak?

dlech commented 1 year ago

Is it possible to query without connecting through bleak?

No, it is not. This is a limitation of BlueZ (bluetoothd) and the D-Bus API as you have seen with bluetoothctl.