bleak icon indicating copy to clipboard operation
bleak copied to clipboard

Advertising interval dependent issue: Could not get GATT services: Unreachable

Open DaGigabyte opened this issue 1 month ago • 4 comments

Background

I have a custom BLE device with a known address that I would like to connect to using bleak. Using uart_service.py, I am able to connect to the device when its advertising interval is set to 500ms. However, when its advertising interval is 10240ms, uart_service.py always failed to connect to the BLE device.

Description of issue

uart_service.py

Since I know the address and the characteristics, I have slightly modified the uart_service.py to the following:

target_address = "f5:f5:22:22:33:44"
UART_TX_CHAR_UUID = "e09abcdef..."
UART_RX_CHAR_UUID = "e09abcdef..."
...
async with BleakClient(target_address, disconnected_callback=handle_disconnect, timeout=100) as client:
    await client.start_notify(UART_TX_CHAR_UUID, handle_rx)
    print("Connected, start typing and press ENTER...")
...

Case 1: Adv_int = 500ms

  1. Launch script with python .\uart_service.py
  2. After 1-5 seconds passed...
  3. Connected, start typing and press ENTER...
  4. At this point, I can type into the terminal and the communication with the BLE device characteristics works perfectly.

Case 2: Adv_int = 10240ms

  1. Launch script with python .\uart_service.py
  2. After 10-102 seconds passed...
  3. Exception raised:
raise BleakError(f"{fail_msg}: Unreachable")
bleak.exc.BleakError: Could not get GATT services: Unreachable

My Intuition

This seem to be an advertising interval dependent issue. Increasing timeout parameter from the default 10s to 100s did not help to solve it. In both cases, I have verified that the BLE device is advertising at the correct interval using a nRF Connect app on my phone.

DaGigabyte avatar Dec 03 '25 08:12 DaGigabyte

Which OS are you using?

You didn't give the full stack trace, but I'm guessing this is propagating an error from the OSes Bluetooth stack. So there isn't really anything we can do about it in Bleak.

dlech avatar Dec 03 '25 15:12 dlech

The OS is Windows 11, running on a laptop that is BLE-capable.

May I know what kind of stack trace should I provide? I have installed Wireshark as recommended in the official debug guide, but I don't know what to filter or look for among the full stack trace.

DaGigabyte avatar Dec 03 '25 15:12 DaGigabyte

May I know what kind of stack trace should I provide?

The stack trace from this exception:

raise BleakError(f"{fail_msg}: Unreachable") bleak.exc.BleakError: Could not get GATT services: Unreachable

I assume there was more printed that included the stack trace?

I have installed Wireshark as recommended in the official debug guide, but I don't know what to filter

For issues like this, I usually look for what is different between a "good" capture and and "bad" capture.

dlech avatar Dec 03 '25 17:12 dlech

I have captured the connection packets with WireShark. It seems that LE Create Connection is followed by LE Create Connection Cancel after 7.7s. I have already passed timeout=100 into the context manager async with BleakClient(target_address, disconnected_callback=handle_disconnect, timeout=100) as client: The only difference in the below cases is that the BLE device is programmed with a different advertising interval. The bleak code remains unchanged.

Advertising interval = 10240ms (Bad capture)

Image

Advertising interval = 500ms (Good capture)

Image

DaGigabyte avatar Dec 04 '25 09:12 DaGigabyte