bleak icon indicating copy to clipboard operation
bleak copied to clipboard

bleak.exc.BleakError: Characteristic was not found!

Open mrdc opened this issue 4 years ago • 43 comments

  • 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!

mrdc avatar Nov 15 '20 17:11 mrdc

Try pip3 install --upgrade bleak on Linux to move past 0.8.0.

dhalbert avatar Nov 15 '20 17:11 dhalbert

Try pip3 install --upgrade bleak on Linux to move past 0.8.0.

Thanks!

Unfortunately, the same error with bleak 0.9.1.

mrdc avatar Nov 15 '20 17:11 mrdc

@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 avatar Nov 24 '20 16:11 hbldh

@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+

mrdc avatar Nov 25 '20 09:11 mrdc

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?

hbldh avatar Nov 25 '20 09:11 hbldh

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.

mrdc avatar Nov 25 '20 09:11 mrdc

Do you have any non-RPi Linux system to try connecting from?

hbldh avatar Nov 25 '20 10:11 hbldh

I can try to install Ubuntu, I hope it will work with CSR 4.0 dongle.

mrdc avatar Nov 25 '20 12:11 mrdc

@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.

mrdc avatar Nov 26 '20 10:11 mrdc

Have you tried the suggestions in #332? Otherwise, do that in this case!

hbldh avatar Dec 03 '20 14:12 hbldh

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.

mrdc avatar Dec 03 '20 14:12 mrdc

Which version of the firmware did you update to?

dhalbert avatar Dec 03 '20 14:12 dhalbert

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.

mrdc avatar Dec 03 '20 14:12 mrdc

Any updates with this one? Facing similar issue.

aayushsingla avatar Mar 16 '21 23:03 aayushsingla

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.

dlech avatar Mar 16 '21 23:03 dlech

@aayushsingla The new version 0.11.0 has new been released to PyPI. Install that as see if that improves your situation.

hbldh avatar Mar 18 '21 09:03 hbldh

@hbldh Hello, I've checked with the latest bleak (0.11.0) - the same issue :(

mrdc avatar Mar 19 '21 10:03 mrdc

Did you try removing the device using bluetoothctl ?

dlech avatar Mar 19 '21 16:03 dlech

Did you try removing the device using bluetoothctl ?

In case I've paired my device? Or you mean something else?

mrdc avatar Mar 19 '21 17:03 mrdc

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.

dlech avatar Mar 19 '21 17:03 dlech

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.

mrdc avatar Mar 20 '21 12:03 mrdc

Does it still raise a BleakError with v0.11.0? What is the new stack trace?

dlech avatar Mar 20 '21 15:03 dlech

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!

mrdc avatar Mar 20 '21 17:03 mrdc

Same problem 😢

Carlitox7 avatar Mar 24 '21 22:03 Carlitox7

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...

hbldh avatar Mar 29 '21 08:03 hbldh

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.

mrdc avatar Mar 31 '21 09:03 mrdc

bluez-firmware is already the newest version (1.2-4+rpt8)

The same issue :(

mrdc avatar Mar 31 '21 09:03 mrdc

@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?

hbldh avatar Apr 20 '21 08:04 hbldh

@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.

mrdc avatar Apr 21 '21 09:04 mrdc

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?

dlech avatar Oct 07 '21 16:10 dlech