question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Intermittent "bleak.exc.BleakError: Services discovery error" when reading from Arduino

See original GitHub issue
  • bleak version: 0.9.1
  • Python version: 3.7.3
  • Operating System: Linux (Raspberry PI OS Lite)
  • BlueZ version (bluetoothctl -v) in case of Linux: 5.5.0

Description

I have an Arduino Nano 33 BLE Sense reading environmental data (temperature, humidity, barometric pressure, irradiance) from its sensors and reporting via BLE. I am able to consistently read the measured characteristics using Linux’s ‘gatttool’ and Nordic Semiconductor’s nRF Connect Android app. When attempting to read with Python/Bleak, the connection throws a Service discovery error most of the time, but not always.

What I Did

I constructed a simple Python program, based on the example code in the Bleak README.rst file. On rare occasions, I am able to read characteristics with Python, but most times it will throw an error: “bleak.exc.BleakError: Services discovery error”

I have debugging output (see below) from the Arduino sketch that shows a connection being made from the Python program. The connection is closed a short time later (about 0.15 seconds.) Then, about five seconds later, Bleak throws the error. I assume the data has been read during the 0.15s connection time, because it is consistent with the length of time gatttool remains connected to carry out the same request.

My Arduino sketch is at: https://github.com/DavesCodeMusings/Nano33BLESense/blob/master/BluetoothWeather3.ino The Python code to read the temperature is at: https://github.com/DavesCodeMusings/Nano33BLESense/blob/master/read_temperature.py, but does not deviate much from the example in the README.

The fact that a connection is being made and then closed, with the error coming five seconds later makes me wonder if Bleak is timing out expecting some information my Arduino sketch is not providing. Though, if that is the case, I would expect gatttool and nRF to throw errors as well, but they do not. Reading the other characteristics (humidity, barometric pressure, and irradiance) gives the same results.

I do not have any other BLE devices to test against, and it might be tempting to say the error lies in my Arduino sketch. But, I am able to consistently read data from the same device using two other tools without error.

$ sudo ./read_temperature.py
Traceback (most recent call last):
  File "./read_temperature.py", line 21, in <module>
    loop.run_until_complete(run(address))
  File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
    return future.result()
  File "./read_temperature.py", line 15, in run
    async with BleakClient(address) as client:
  File "/usr/local/lib/python3.7/dist-packages/bleak/backends/client.py", line 60, in __aenter__
    await self.connect()
  File "/usr/local/lib/python3.7/dist-packages/bleak/backends/bluezdbus/client.py", line 157, in connect
    await self.get_services()
  File "/usr/local/lib/python3.7/dist-packages/bleak/backends/bluezdbus/client.py", line 389, in get_services
    raise BleakError("Services discovery error")
bleak.exc.BleakError: Services discovery error
$ sudo gatttool -b 71:A2:AE:D8:21:0C --char-read --uuid=0x2a6e
handle: 0x000d   value: e1 07

Debugging Output From BLE Device

Arduino serial debug output when connecting with Python: sudo ./read_temperature.py (The loop runs at 100mS intervals. Connection timeout is decremented with each iteration.)

Incoming connection from: b8:27:eb:10:b7:7f
New ambient count: 12
Pressure: 99165.9Pa
Temperature: 20.49°C
Humidity: 39.18%
Irradiance: 0.0W/sq. meter
Connect timeout: 600
Pressure: 99166.2Pa
Temperature: 20.38°C
Humidity: 39.20%
Irradiance: 0.0W/sq. meter
Connect timeout: 599
New ambient count: 12
Pressure: 99165.0Pa
Temperature: 20.40°C
Humidity: 39.26%
Irradiance: 0.0W/sq. meter
Connect timeout: 598
Pressure: 99168.8Pa
Temperature: 20.43°C
Humidity: 39.23%
Irradiance: 0.0W/sq. meter
Connect timeout: 597
New ambient count: 12
Pressure: 99166.1Pa
Temperature: 20.47°C
Humidity: 39.20%
Irradiance: 0.0W/sq. meter
Connect timeout: 596
Pressure: 99169.6Pa
Temperature: 20.42°C
Humidity: 39.10%
Irradiance: 0.0W/sq. meter
Connect timeout: 595
New ambient count: 12
Pressure: 99170.2Pa
Temperature: 20.43°C
Humidity: 39.16%
Irradiance: 0.0W/sq. meter
Connect timeout: 594
Pressure: 99163.8Pa
Temperature: 20.50°C
Humidity: 39.23%
Irradiance: 0.0W/sq. meter
Connect timeout: 593
New ambient count: 12
Pressure: 99166.1Pa
Temperature: 20.52°C
Humidity: 39.20%
Irradiance: 0.0W/sq. meter
Connect timeout: 592
Pressure: 99164.8Pa
Temperature: 20.52°C
Humidity: 39.22%
Irradiance: 0.0W/sq. meter
Connect timeout: 591
New ambient count: 12
Pressure: 99169.1Pa
Temperature: 20.52°C
Humidity: 39.29%
Irradiance: 0.0W/sq. meter
Connect timeout: 590
Pressure: 99169.2Pa
Temperature: 20.54°C
Humidity: 39.21%
Irradiance: 0.0W/sq. meter
Connect timeout: 589
New ambient count: 12
Pressure: 99167.6Pa
Temperature: 20.56°C
Humidity: 39.22%
Irradiance: 0.0W/sq. meter
Connect timeout: 588
Pressure: 99167.6Pa
Temperature: 20.49°C
Humidity: 39.16%
Irradiance: 0.0W/sq. meter
Connect timeout: 587
New ambient count: 12
Pressure: 99167.4Pa
Temperature: 20.49°C
Humidity: 39.19%
Irradiance: 0.0W/sq. meter
Connect timeout: 586
Connection terminated.

Arduino serial debug output when connecting with gatttool: sudo gatttool -b 71:A2:AE:D8:21:0C --char-read --uuid=0x2a6e (The loop runs at 100mS intervals. Connection timeout is decremented with each iteration.)

Incoming connection from: b8:27:eb:10:b7:7f
Pressure: 99158.1Pa
Temperature: 20.08°C
Humidity: 39.44%
Irradiance: 0.0W/sq. meter
Connect timeout: 600
New ambient count: 13
Pressure: 99166.5Pa
Temperature: 20.02°C
Humidity: 39.46%
Irradiance: 0.0W/sq. meter
Connect timeout: 599
Pressure: 99163.3Pa
Temperature: 20.02°C
Humidity: 39.52%
Irradiance: 0.0W/sq. meter
Connect timeout: 598
New ambient count: 13
Pressure: 99163.1Pa
Temperature: 20.02°C
Humidity: 39.51%
Irradiance: 0.0W/sq. meter
Connect timeout: 597
Pressure: 99161.2Pa
Temperature: 20.08°C
Humidity: 39.53%
Irradiance: 0.0W/sq. meter
Connect timeout: 596
New ambient count: 13
Pressure: 99164.1Pa
Temperature: 20.04°C
Humidity: 39.46%
Irradiance: 0.0W/sq. meter
Connect timeout: 595
Pressure: 99163.3Pa
Temperature: 20.08°C
Humidity: 39.56%
Irradiance: 0.0W/sq. meter
Connect timeout: 594
New ambient count: 13
Pressure: 99158.1Pa
Temperature: 20.08°C
Humidity: 39.54%
Irradiance: 0.0W/sq. meter
Connect timeout: 593
Pressure: 99162.8Pa
Temperature: 20.13°C
Humidity: 39.46%
Irradiance: 0.0W/sq. meter
Connect timeout: 592
New ambient count: 13
Pressure: 99165.2Pa
Temperature: 20.09°C
Humidity: 39.52%
Irradiance: 0.0W/sq. meter
Connect timeout: 591
Pressure: 99161.6Pa
Temperature: 20.11°C
Humidity: 39.51%
Irradiance: 0.0W/sq. meter
Connect timeout: 590
New ambient count: 13
Pressure: 99164.4Pa
Temperature: 20.09°C
Humidity: 39.48%
Irradiance: 0.0W/sq. meter
Connect timeout: 589
Pressure: 99162.2Pa
Temperature: 20.08°C
Humidity: 39.52%
Irradiance: 0.0W/sq. meter
Connect timeout: 588
New ambient count: 13
Pressure: 99157.1Pa
Temperature: 20.11°C
Humidity: 39.55%
Irradiance: 0.0W/sq. meter
Connect timeout: 587
Pressure: 99162.7Pa
Temperature: 20.06°C
Humidity: 39.44%
Irradiance: 0.0W/sq. meter
Connect timeout: 586
New ambient count: 13
Pressure: 99159.3Pa
Temperature: 20.09°C
Humidity: 39.46%
Irradiance: 0.0W/sq. meter
Connect timeout: 585
Pressure: 99166.7Pa
Temperature: 20.11°C
Humidity: 39.49%
Irradiance: 0.0W/sq. meter
Connect timeout: 584
New ambient count: 13
Pressure: 99163.9Pa
Temperature: 20.08°C
Humidity: 39.50%
Irradiance: 0.0W/sq. meter
Connect timeout: 583
Pressure: 99160.9Pa
Temperature: 20.13°C
Humidity: 39.53%
Irradiance: 0.0W/sq. meter
Connect timeout: 582
New ambient count: 13
Pressure: 99166.7Pa
Temperature: 20.11°C
Humidity: 39.48%
Irradiance: 0.0W/sq. meter
Connect timeout: 581
Pressure: 99162.5Pa
Temperature: 20.13°C
Humidity: 39.51%
Irradiance: 0.0W/sq. meter
Connect timeout: 580
New ambient count: 13
Pressure: 99159.1Pa
Temperature: 20.11°C
Humidity: 39.48%
Irradiance: 0.0W/sq. meter
Connect timeout: 579
Connection terminated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DavesCodeMusingscommented, Oct 29, 2020

I have found the culprit! I must apologize, as it was not Bleak as I initially suspected. I’m leaving this explanation of what happened in case any other Arduino users are hit by the same problem.

Uploading an Arduino sketch can result in a new BLE address for the device. That was the source of the problem.

I was assuming a BLE address was like an Ethernet MAC address in that it was burned in at the factory and read-only. Not so! Not only does the Arduino Nano’s BLE address change when you update the firmware, but it can also change when a new sketch is uploaded.

I had made the mistake of hard-coding the BLE address in my Python test scripts. They would then work until I made a change to the sketch and uploaded it.

The nRF Connect Android app was looking up the device by name and resolving to the proper BLE address each time, which is why it never failed as my Python scripts did.

Many thanks to the Bleak devs for answering my questions prior to this discovery.

1reaction
DavesCodeMusingscommented, Oct 27, 2020

Thanks for the clarification. And thank you for all the time you spent on this issue. I still do not know why it is working now when it almost consistently failed before. I’ll keep experimenting. I can see Bleak being very useful in my home automation projects and I appreciate your contributions to free software with this library.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem in receiving the data via bluetooth in python using ...
im trying to print the values in bluetooth from esp32 and i want to receive the data in python by using bleak library....
Read more >
Could not get GATT services: Unreachable" , how to solve it?
Python's Bluetooth library of the Bleak report "bleak.exc.BleakError: Could not get GATT services: Unreachable" , how to solve it? Ask Question.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found