bleak
bleak copied to clipboard
bleak.exc.BleakError: Characteristic was not found!
- bleak version: 0.9.1 / 0.8.0
- Python version: 3.8.6 / 3.8.4
- Operating System: Windows 10 / Debian 10 (RPi3b+)
- BlueZ: bluetoothctl: 5.50
Description
I'm trying to connect to a BLE device via bleak
- it works fine when I use my script on Windows 10, but the same script fails on Linux:
bleak.exc.BleakError: Characteristic 11111111-1111-1111-1111-111111111111 was not found!
What I Did
My code:
from bleak import BleakClient
import asyncio
address = "xx:xx:xx:xx:xx:xx"
ble_uuid = "11111111-1111-1111-1111-111111111111"
msg = b'ABC'
async def run(address, loop):
async with BleakClient(address, loop=loop) as client:
await client.write_gatt_char(ble_uuid, msg, True)
loop = asyncio.get_event_loop()
loop.run_until_complete(run(address, loop))
The difference I see is a different bleak version on Win and on Linux. On Linux pip3 install bleak
returns only bleak 0,8.0:
pip3 install bleak
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: bleak in /home/pi/.local/lib/python3.8/site-packages (0.8.0)
Traceback (most recent call last):
File "script.py", line 13, in <module>
loop.run_until_complete(run(address, loop))
File "/usr/local/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "script.py", line 11, in run
await client.write_gatt_char(ble_uuid, msg, True)
File "/home/pi/.local/lib/python3.8/site-packages/bleak/backends/bluezdbus/client.py", line 580, in write_gatt_char
raise BleakError("Characteristic {0} was not found!".format(char_specifier))
bleak.exc.BleakError: Characteristic 11111111-1111-1111-1111-111111111111 was not found!
Try pip3 install --upgrade bleak
on Linux to move past 0.8.0.
Try
pip3 install --upgrade bleak
on Linux to move past 0.8.0.
Thanks!
Unfortunately, the same error with bleak 0.9.1
.
@mrdc Could you please run the service_explorer.py
example script (https://github.com/hbldh/bleak/blob/develop/examples/service_explorer.py) for your peripheral and post the results here?
@hbldh Here you go:
$ python3 service_explorer.py
Executing <Task pending name='Task-1' coro=<run() running at service_explorer.py:25> wait_for=<Future pending cb=[Deferred.asFuture.<locals>.checkCancel() at /home/pi/.local/lib/python3.8/site-packages/twisted/internet/defer.py:786, <TaskWakeupMethWrapper object at 0x7584d838>()] created at /usr/local/lib/python3.8/asyncio/base_events.py:422> cb=[_run_until_complete_cb() at /usr/local/lib/python3.8/asyncio/base_events.py:184] created at /usr/local/lib/python3.8/asyncio/base_events.py:595> took 0.136 seconds
Connected: True
pi@raspberrypi:~
It outputs nothing, but Connected: True Maybe it's related to https://github.com/hbldh/bleak/issues/332? I'm also using RPi3b+
It might. Regardless, the script should print something more at least? Or throw an error? Otherwise BlueZ just didn't detect any services on your peripheral.
BlueZ does its own handling of services, e.g. the Battery Service is not available in BlueZ as it is in Windows. Maybe it disregards services with non-standard uuids? Have you written the peripheral code yourself?
the script should print something more at least? Or throw an error?
Unfortunately, no output: it just says Connected: True
and then script exits. The same script works fine on Windows 10.
BTW I've updated BT firmware and still the same issue.
Have you written the peripheral code yourself?
Yes, it's homemade :) Had no issues with BLE part - it connects from macOS, Win10, iOS, but not from RPi3b+ Still it's strange - ble should be able to connect to any characteristic UUID I provide.
Do you have any non-RPi Linux system to try connecting from?
I can try to install Ubuntu, I hope it will work with CSR 4.0 dongle.
@hbldh
Do you have any non-RPi Linux system to try connecting from?
ubuntu-20.04.1-desktop-amd64@LiveMode and CSR 4.0 dongle.
Here you go:
root@ubuntu:~# python3 service_explorer.py
Connected: True
[Service] cc1ad9ab-9be8-400b-bde1-4af132b3a1b0: Unknown
[Characteristic] 22222222-2222-2222-2222-222222222222: (Handle: 43) (read,notify) | Name: Unknown, Value: b'#2'
[Characteristic] 11111111-1111-1111-1111-111111111111: (Handle: 41) (read,write) | Name: Unknown, Value: b'#1'
[Service] 00001801-0000-1000-8000-00805f9b34fb: Generic Attribute Profile
[Characteristic] 00002a05-0000-1000-8000-00805f9b34fb: (Handle: 2) (indicate) | Name: Service Changed, Value: None
[Descriptor] 00002902-0000-1000-8000-00805f9b34fb: (Handle: 4) | Value: b'\x02\x00'
and my initial script:
root@ubuntu:~# python3 ble2.py
Traceback (most recent call last):
File "ble2.py", line 14, in <module>
loop.run_until_complete(run(address, loop))
File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "ble2.py", line 11, in run
await client.write_gatt_char(ble_uuid, msg, True)
File "/usr/local/lib/python3.8/dist-packages/bleak/backends/bluezdbus/client.py", line 614, in write_gatt_char
await self._bus.callRemote(
txdbus.error.MarshallingError: List, Tuple, Bytearray, or Dictionary required for DBus array. Received: b'ABC'
So, looks like it's RPi related issue.
Have you tried the suggestions in #332? Otherwise, do that in this case!
Have you tried the suggestions in #332? Otherwise, do that in this case!
Yes, I’ve updated the firmware.
BTW I've updated BT firmware and still the same issue.
Which version of the firmware did you update to?
Which version of the firmware did you update to?
BCM4345C0_003.001.025.0171.0339.hcd (https://drive.google.com/file/d/1DVOtBjrsoR2NhwEBVn3ei0sv-xTIBCxR/view?usp=sharing)
According to the firmware change log (https://github.com/RPi-Distro/bluez-firmware/blob/master/debian/changelog), it fixes the connection issue, in my case RPi3 connects to BLE device.
Any updates with this one? Facing similar issue.
We're getting ready to do a release (probably tomorrow) that includes some troubleshooting tips on making sure the problem isn't cached device info. Also, there are major changes to the BlueZ backend, so if anything else, it should at least provide better debugging info.
@aayushsingla The new version 0.11.0 has new been released to PyPI. Install that as see if that improves your situation.
@hbldh Hello, I've checked with the latest bleak (0.11.0) - the same issue :(
Did you try removing the device using bluetoothctl
?
Did you try removing the device using
bluetoothctl
?
In case I've paired my device? Or you mean something else?
Even if it is not paired, BlueZ still caches information about the device. Removing the device will clear all of the cached info so that you can start fresh.
Removing the device will clear all of the cached info so that you can start fresh.
I've tried it: bluetoothctl -> remove DEVICE_MAC
- the same result.
Does it still raise a BleakError
with v0.11.0? What is the new stack trace?
Does it still raise a
BleakError
with v0.11.0? What is the new stack trace?
Hi, yes, it's the same error: bleak.exc.BleakError: Characteristic XXXXXXXXXXXXXXXXX was not found!
Same problem 😢
If you are using a Raspberry Pi, then try to follow the advice here: https://github.com/hbldh/bleak/issues/332#issuecomment-803623516
If the characteristic you want to use isn't printed when running service_explorer.py
example script, then BlueZ registered it, or at least did not give Bleak an opportunity to find it...
If the characteristic you want to use isn't printed when running
service_explorer.py
example script, then BlueZ registered it, or at least did not give Bleak an opportunity to find it...
Yes, in my case there are no characteristics found when I use service_explorer.py
If you are using a Raspberry Pi, then try to follow the advice here: #332 (comment)
Thanks, I'll test it and provide feedback.
bluez-firmware is already the newest version (1.2-4+rpt8)
The same issue :(
@mrdc I honestly have no idea what might be causing this, Does it happen on other RPis, if you happen to have more to try the code on?
@mrdc I honestly have no idea what might be causing this, Does it happen on other RPis, if you happen to have more to try the code on?
Actually, it's not a big deal - I don't depend on it. I'll buy RPi4 late for tests - will update the issue.
I recently found a bug in BlueZ where the cache was not entirely cleared when the device is removed. Here is the workaround:
bluetoothctl -- remove XX:XX:XX:XX:XX:XX
# prior to BlueZ 5.62 you also need to manually delete the GATT cache
sudo rm "/var/lib/bluetooth/YY:YY:YY:YY:YY:YY/cache/XX:XX:XX:XX:XX:XX"
...where XX:XX:XX:XX:XX:XX
is the Bluetooth address of your device and
YY:YY:YY:YY:YY:YY
is the Bluetooth address of the Bluetooth adapter on
your computer.
Does this resolve the issue?