hbldh / bleak

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

bleak.exc.BleakError: Characteristic was not found! #359

Closed mrdc closed 1 year ago

mrdc commented 3 years ago

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!
dlech commented 1 year ago

Closing since the BlueZ backend in Bleak has significantly changed since the issue was opened. Hopefully the original issue could be solved by upgrading BlueZ and/or ensuring that the device is sending the correct information about the attribute database to BlueZ in the first place.

abel2319 commented 6 months ago

Hi guys, i faced the same problem with my esp32. My outputs:

$ python3 service_explorer.py --address 9C:9C:1F:C4:DD:D6                    2 ⨯
2023-12-03 10:03:51,766 __main__ INFO: starting scan...
2023-12-03 10:03:53,745 __main__ INFO: connecting to device...
2023-12-03 10:03:55,642 __main__ INFO: connected
2023-12-03 10:03:55,643 __main__ INFO: [Service] 6e400001-b5a3-f393-e0a9-e50e24dcca9e (Handle: 19): Nordic UART Service
2023-12-03 10:03:55,643 __main__ INFO:   [Characteristic] 6e400002-b5a3-f393-e0a9-e50e24dcca9e (Handle: 23): Nordic UART RX (write)
2023-12-03 10:03:55,644 __main__ INFO:   [Characteristic] 6e400003-b5a3-f393-e0a9-e50e24dcca9e (Handle: 20): Nordic UART TX (notify)
2023-12-03 10:03:55,854 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 22): Client Characteristic Configuration, Value: bytearray(b'\x00\x00')
2023-12-03 10:03:55,854 __main__ INFO: [Service] 00001801-0000-1000-8000-00805f9b34fb (Handle: 15): Generic Attribute Profile
2023-12-03 10:03:55,855 __main__ INFO:   [Characteristic] 00002a05-0000-1000-8000-00805f9b34fb (Handle: 16): Service Changed (indicate)
2023-12-03 10:03:55,947 __main__ INFO:     [Descriptor] 00002902-0000-1000-8000-00805f9b34fb (Handle: 18): Client Characteristic Configuration, Value: bytearray(b'\x02\x00')
2023-12-03 10:03:55,947 __main__ INFO: disconnecting...
2023-12-03 10:03:58,100 __main__ INFO: disconnected

My code:

$ python3 esp32_ble.py                                                   
Traceback (most recent call last):
  File "/home/merino/Documents/M2_Data_Science/PT/Car_project/esp32_ble.py", line 31, in <module>
    asyncio.run(run())
  File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/merino/Documents/M2_Data_Science/PT/Car_project/esp32_ble.py", line 18, in run
    await client.write_gatt_char(UUID3, int_parameter.to_bytes(4, byteorder='little'))
  File "/home/merino/.local/lib/python3.11/site-packages/bleak/__init__.py", line 769, in write_gatt_char
    raise BleakError("Characteristic {char_specifier} was not found!")
bleak.exc.BleakError: Characteristic {char_specifier} was not found!
dlech commented 6 months ago

What is UUID3?

SoonCan commented 3 months ago

May be your UUID(write property) is mistake? I met this mistake too. After I modified the UUID, the problem was solved.