bleak icon indicating copy to clipboard operation
bleak copied to clipboard

Timeouts for read_gatt_char/write_gatt_char

Open cflavio opened this issue 4 years ago • 1 comments

  • bleak version: 0.12
  • Python version: 3.8
  • Operating System: Ubuntu 20.04
  • BlueZ version: 5.52

Description

Hi, I have observed that sometimes I have some invocations of read_gatt_char/write_gatt_char that never return. So, my application freezes. Is it the expected behavior? Is there a best practice for adding a timeout to them (so, if they don't return then something like an exception is raised and the control returns to the caller)? Thank you very much!

cflavio avatar Nov 25 '21 16:11 cflavio

There is no timeout capability in the dbus-next library that Bleak uses, but one could possibly use a await asyncio.wait_for(self._bus.call(...), timeout=5.0) (See Python docs) to stop waiting for a reply. I have no idea if this messes up the MessageBus that represents Bleak's connection to the DBus system which in turn communicates with BlueZ which in turn communicates with your device...

You can try it yourself by modifying in read_gatt_char and You can try it yourself by modifying in write_gatt_char. You should raise a BleakError if timeout happens and catch that in you application, instead of just returning None, though.

Try it and see if it works. Feel free to make a PR if you get it into a state that you are satified with, and we will review it!

hbldh avatar Nov 26 '21 13:11 hbldh

There is also now asyncio.timeout() which works better than asyncio.wait_for().

dlech avatar Jul 19 '23 15:07 dlech