代码之家  ›  专栏  ›  技术社区  ›  Sandrocottus

pygatt:无法执行device.subscribe()

  •  0
  • Sandrocottus  · 技术社区  · 4 年前

    我试图认同关贸总协定的特点。

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

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

    但是,我无法执行此特定特性的device.subscribe()函数。

    当我使用

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

    我得到了错误

    pygatt.backends.bgabi异常。预期响应超时:超时 在1000万人等待之后 []

    在链接中 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()
    

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

    我真的被困在这里了。

    任何帮助都将不胜感激!

    0 回复  |  直到 4 年前
        1
  •  2
  •   Sandrocottus    4 年前

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

    以下是代码:-

    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()
    
    推荐文章