gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:20:28 27 4
gpt4 key购买 nike

我正在尝试订阅 GATT 特性。

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

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

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

当我使用

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

我得到了错误

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



在链接中
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() got an unexpected keyword argument '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()

然而,只是我无法订阅它。

我真的被困在这里了。

任何帮助深表感谢!

最佳答案

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

以下是代码:-

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

关于python-3.x - pygatt : Unable to execute device. 订阅(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60888220/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com