Reconnect and keep reading from device with asyncio and BleakClient #1216
Replies: 2 comments 15 replies
-
It looks like you are missing the part where it should wait for disconnect. There is an example at https://github.com/hbldh/bleak/blob/develop/examples/disconnect_callback.py |
Beta Was this translation helpful? Give feedback.
-
I've added some prints, same effect I guess: async def read_data():
def handle_disconnect(_: BleakClient):
print("Device was disconnected, goodbye.")
disconnected_event.set()
def handle_response(_: int, data: bytearray):
print("received:", data)
disconnected_event = asyncio.Event()
while True:
device = await BleakScanner.find_device_by_filter(
lambda d, ad: d.name and d.name.lower() == GLOVEName.lower()
)
print("I found the device: " + str(device))
if device is None:
await asyncio.sleep(1)
continue
try:
async with BleakClient(device, disconnected_callback=handle_disconnect) as client:
print("Client is connected: " + str(client.is_connected))
await client.start_notify(UART_TX_CHAR_UUID, handle_rx)
print("I got past start_notify")
await disconnected_event.wait()
print("I got past disconnection")
disconnected_event.clear()
print("I cleared the disconnect event")
except BleakError as e:
print(e)
await client.disconnect() And I ran as usual. Turn on the device -> start script -> it connects the device successfully -> I click the button to send data, it prints successfully, after a couple seconds I click again to stop -> I turn off the device, it disconnects. After a couple seconds I turn it on again, it connects successfully it seams, at least the print of the device variable shows the device again and the client.is_connected is True -> Despite this, when I click the button to send data nothing happens.
|
Beta Was this translation helpful? Give feedback.
-
Hi, I'm taking the first steps with bleak and I'm a bit stumped. After trying several things I decided to ask here if anyone can help.
I have a Bluetooth device that sends data and the goal is to read from it and in case it turns off, wait for it to turn back on again and keep reading. But I'm having problems with that last part.
Here's what I have so far (the function that does it all, I just call it with asyncio.run(read_data())):
Here's the prints of an execution with a device disconnection in between:
So, first it connects well, and then comes my first question, why does the client.is_connected return false if it is connected?
I then press the button on the device which sends data and gets printed in the terminal, I then press the button again and it stops. So far so good.
I can keep pressing the button as many times as I like and it will keep starting and stopping sending and printing data which is the intended behaviour.
After that I turn off the device and turn it back on. The device is found again going by the prints, again with a False in the is_connected. But this second time it fails with a time out error in await client.connect(). Why? What am I doing wrong here?
I've tried adding some code to run the await client.disconnect when I turn off the device, before connecting again, and that doesn't throw the timeout error on await client.connect(), but it just stays in the loop printing None and doesn't read anything when the device sends data.
I saw this issue and got most of the code from there #933. But I can't make it work for my scenario.
Any help either explaining this behavior or with a solution to the problem would be appreciated.
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions