2

我正在尝试订阅 GATT 特性。

我已经为我的 BLE 设备中的 GATT 特性设置了“指示”、“通知”和“读取”属性。

我能够连接到我的 BLE 设备并读取/写入其他特性。

但是,我无法针对此特定特征执行 device.subscribe() 函数。

当我使用

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

我得到了错误

pygatt.backends.bgapi.exceptions.ExpectedResponseTimeout:等待 10.000000 秒后超时 []

在链接 https://github.com/peplin/pygatt/blob/master/pygatt/device.py中,订阅函数有参数“wait_for_response”

在我的代码中,如果我使用

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

它显示错误

TypeError: subscribe() 得到了一个意外的关键字参数“wait_for_response”

我如何消除这些错误并订阅特性?

编辑:

我将属性读取和写入以及通知和指示添加到特征

我可以使用以下代码读取和写入特征:-

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

但是,只是我无法订阅它。

我真的被困在这里了。

任何帮助深表感谢!

4

1 回答 1

2

重新审视这个问题后,我发现在添加一些延迟后,我能够订阅特性:

以下是代码:-

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()
于 2020-06-18T17:30:38.653 回答