bleak icon indicating copy to clipboard operation
bleak copied to clipboard

Cannot re-connect to BLE device after KeyboardInterrupt (CTRL-C)

Open nomadbyte opened this issue 5 months ago • 2 comments

  • bleak: v1.1.0
  • python: 3.13.7 (Windows 10)

I was testing the bleak functionality using its examples/disconnect_callback.py. The code works correctly -- connects to the BLE device, then waits for the device to turn off, then correctly disconnects it , and ends the execution. On the next run (after turning the BLE device on), the example code again proceeds and ends correctly.

However, when the wait is terminated with a KeyboardInterrupt (Ctrl+C) instead of turning the device off, the execution terminates with a Traceback. Then on the subsequent re-run, the example cannot connect to the BLE device (could not find device). The device has to be manually turned off and on for the example to be able to connect to it again.

While a Traceback on the KeyboardInterrupt is expected, I would also expect bleak to properly disconnect the BLE device from the context manager. For some reason this does not take place. Thus the device remains connected to the host, and won't be available for the re-connection.

Below is the log of the interrupted execution and the subsequent re-run of the example code:

>python disconnect_callback.py --name "TestBLE"
2025-08-14 21:35:40,048 __main__ INFO: scanning...
2025-08-14 21:35:44,580 __main__ INFO: Sleeping until device disconnects...    ### CTRL-C pressed
2025-08-14 21:35:49,025 __main__ INFO: Disconnected callback called!
Traceback (most recent call last):
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "C:\Users\tester\Documents\python\bleak\disconnect_callback.py", line 58, in main
    await disconnected_event.wait()
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\locks.py", line 213, in wait
    await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\tester\Documents\python\bleak\disconnect_callback.py", line 99, in <module>
    asyncio.run(main(args))
    ~~~~~~~~~~~^^^^^^^^^^^^
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 195, in run
    return runner.run(main)
           ~~~~~~~~~~^^^^^^
  File "C:\Users\tester\AppData\Local\Programs\Python\Python313\Lib\asyncio\runners.py", line 123, in run
    raise KeyboardInterrupt()
KeyboardInterrupt
^C


>python disconnect_callback.py --name "TestBLE"
2025-08-14 21:36:52,162 __main__ INFO: scanning...
2025-08-14 21:37:02,314 __main__ ERROR: could not find device with name 'TestBLE'

nomadbyte avatar Aug 17 '25 01:08 nomadbyte

I have the same issue running on the lastest Raspberry Pi Linux and Windows 10. I am connecting to a BLE in the Colmi fitness ring.

Here is the code that triggers with the Keyboard exit:

        try:
            await stop_event.wait()
        finally:
            print("[INFO] Stopping device communication...")
            await send_data_array(client, DISABLE_RAW_SENSOR_CMD, "RXTX")
            print("[INFO] Disconnected from device.")

Here is what shows on the console:

[INFO] Signal 2 received. Exiting...
[INFO] Stopping device communication...
Failed to send data to RXTX service: Not connected
[INFO] Disconnected from device.

rjapenga avatar Oct 31 '25 22:10 rjapenga

This is the kind of issue where we need Bluetooth packet logs to see what is actually happening. See the troubleshooting page in the docs for instructions.

dlech avatar Nov 02 '25 15:11 dlech