pygatt: Unable to execute device.subscribe()

后端 未结 1 1095
我寻月下人不归
我寻月下人不归 2021-01-15 10:38

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.

1条回答
  •  野的像风
    2021-01-15 10:49

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

    0 讨论(0)
提交回复
热议问题