python-3.xbluetooth-gattgatttool

Cannot read/write to characteristics in BLE


I have a BLED112 dongle and a BLE device which contains a GATT profile with Services-Characteristics. I have installed pygatt to communicate with my BLE device.

Following is my python code to connect to my BLE device and read/write to a characteristic:-

import pygatt
adapter = pygatt.BGAPIBackend()
#adapter = pygatt.BGAPIBackend()
adapter.start()
adapter.scan(timeout=1)
device = adapter.connect('30:F5:6A:01:2D:05',address_type=pygatt.BLEAddressType.public)
characteristic = "f1126ec4-6e99-5552-5286-55bc21d65776"
device.char_write(characteristic, bytearray([0x00]), wait_for_response=True)
value = device.char_read(characteristic)
print(value)
adapter.stop()

I am able to connect to my device. However, i get the following error while reading or writing:

C:\Python\Python37-32\lib\site-packages\pygatt\backends\bgapi\bgapi.py", line 570, in expect_any raise exc pygatt.backends.bgapi.exceptions.ExpectedResponseTimeout: Timed out after 10.000000s waiting for []

I have included wait_for_response=True in my write instruction, but i still get this issue.

How do i read/write to the characteristic without any error?

PS: While scanning, i observed that the UUID above belongs to a "descriptor".

Is this the reason why i am getting the error?

Is there a way to write to "descriptor" (and not characteristic) in pygatt?


Solution

  • I finally got it by changing the characteristic UUID to the characteristic handle. Following is the working code:-

    import pygatt
    import time
    adapter = pygatt.BGAPIBackend()
    #adapter = pygatt.BGAPIBackend()
    adapter.start()
    adapter.scan(timeout=1)
    device = adapter.connect('30:F5:6A:01:2D:05',address_type=pygatt.BLEAddressType.public)
    characteristic = "26"
    device.char_write_handle(characteristic, bytearray([0x00, 0x09]), wait_for_response=True)
    time.sleep(2)
    value = device.char_read_handle(characteristic)
    print(value)
    adapter.stop()