Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

python 3.x - pygatt: Unable to execute device.subscribe()

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...