pygatt: Unable to execute device.subscribe()
Asked Answered
C

1

2

I am trying to subscribe to a GATT characteristic.

I have set the "Indicate", "Notify" and "Read" attributes for the GATT characteristic in my BLE device.

I am able to connect to my BLE device and read/write to other characteristics.

However, i am unable to execute the device.subscribe() function for this particular characteristic.

When i use

device.subscribe("845ce63c-d003-423c-8922-818676d34255", callback=handle_data)

i get the error

pygatt.backends.bgapi.exceptions.ExpectedResponseTimeout: Timed out after 10.000000s waiting for []

In the link https://github.com/peplin/pygatt/blob/master/pygatt/device.py, the subscribe function has the parameter "wait_for_response"

In my code, if i use

device.subscribe("845ce63c-d003-423c-8922-818676d34255", callback=handle_data, wait_for_response=True)

it shows the error

TypeError: subscribe() got an unexpected keyword argument 'wait_for_response'

How do i eliminate these errors and subscribe to the characteristic?

EDIT:

I added the properties Read and Write to the characteristic along-with Notify and Indicate

I can read and Write to the characteristic using the following code:-

import pygatt

adapter = pygatt.BGAPIBackend()

try:

    adapter.start()

    device = adapter.connect('xx:xx:xx:xx:xx:xx')

    print("Connected")

    #value = device.char_write_handle(55, bytearray([0x00,0x01]), wait_for_response=True)

    value = device.char_read_handle(55)

    print(value)

finally:

    adapter.stop()

However, it is just that i am unable to subscribe to it.

I am really stuck here.

Any help is much appreciated!

Cowpoke answered 27/3, 2020 at 14:39 Comment(0)
C
2

After revisiting this issue, i found that after adding some delays, i was able to subscribe to the characteristic:

Following is the code:-

import pygatt
import time
from binascii import hexlify

adapter = pygatt.BGAPIBackend()

def handle_data(handle, value):
    """
    handle -- integer, characteristic read handle the data was received on
    value -- bytearray, the data returned in the notification
    """
    print("Received data: %s" % hexlify(value))

try:
    time.sleep(5)
    adapter.start()
    time.sleep(5)
    device = adapter.connect('xx:xx:xx:xx:xx:xx')
    time.sleep(5)

    device.subscribe("845ce63c-d003-423c-8922-818676d34255",
                     callback=handle_data)
    time.sleep(5)
    while 1:
        pass
finally:
    print("Adapter Stopped")
    adapter.stop()
Cowpoke answered 18/6, 2020 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.