hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

My program hangs when connecting laptop with device via BLE (Python, Windows, BleakClient, STM32)

mantisbug opened this issue · comments

  • bleak version: 0.21.1
  • Python version: 3.11
  • Operating System: Windows 10

Description

The program runs continuously for several hours without a problem but, for no apparent reason, it suddenly hangs when connecting and I cannot catch the event to control it.

I suppose there is no way to prevent the comms problem; I need to catch the error and do something within the program to re-try the connection, instead of stop the program and rerun it.

Can you help, please?

Posted in stackoverflow as:

https://stackoverflow.com/q/77577781/8678704

What I Did

The program is long to put it here. The comms bit is quite simple:

logger.info('Connecting...')
async with BleakClient(client_address) as client:
    logger.debug(f'Subscribing to characteristic [{nh}]')
    subscribed = await w.subscribe_to_characteristic(nh)
    ...

Logs

30Nov2023 02:29:41 [: root :] INFO -> Connecting...
30Nov2023 02:29:41 [: bleak.backends.winrt.scanner :] DEBUG -> Received 80:E1:26:14:11:3D: .
30Nov2023 02:29:41 [: bleak.backends.winrt.scanner :] DEBUG -> Received 09:80:4C:6F:36:12: .
30Nov2023 02:29:41 [: bleak.backends.winrt.scanner :] DEBUG -> Received 80:E1:26:14:11:3D: *NAME_OF_MY_DEVICE_HERE*
30Nov2023 02:29:41 [: bleak.backends.winrt.scanner :] DEBUG -> 2 devices found. Watcher status: 3.
30Nov2023 02:29:41 [: bleak.backends.winrt.client :] DEBUG -> Connecting to BLE device @ 80:E1:26:14:11:3D
30Nov2023 02:29:41 [: bleak.backends.winrt.scanner :] DEBUG -> Received 80:E1:26:14:11:3D: .
30Nov2023 02:29:41 [: bleak.backends.winrt.client :] DEBUG -> getting services (service_cache_mode=None, cache_mode=None)...
30Nov2023 02:29:41 [: bleak.backends.winrt.client :] DEBUG -> calling get_gatt_services_async
30Nov2023 02:29:43 [: bleak.backends.winrt.client :] DEBUG -> session_status_changed_event_handler: id: BluetoothLE#BluetoothLE44:85:00:63:7e:cc-80:e1:26:14:11:3d, error: 0, status: 1
30Nov2023 02:29:43 [: bleak.backends.winrt.client :] DEBUG -> max_pdu_size_changed_handler: 156
30Nov2023 02:29:47 [: bleak.backends.winrt.client :] DEBUG -> returned from get_gatt_services_async
30Nov2023 02:29:47 [: bleak.backends.winrt.client :] DEBUG -> calling get_characteristics_async
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> 80:E1:26:14:11:3D: services changed
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> 80:E1:26:14:11:3D: services changed
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> returned from get_characteristics_async
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> calling get_descriptors_async
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> returned from get_descriptors_async
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> calling get_characteristics_async
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> 80:E1:26:14:11:3D: services changed
30Nov2023 02:29:48 [: bleak.backends.winrt.client :] DEBUG -> 80:E1:26:14:11:3D: services changed
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> returned from get_characteristics_async
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> calling get_descriptors_async
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> returned from get_descriptors_async
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> calling get_descriptors_async
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> returned from get_descriptors_async
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> calling get_descriptors_async
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> returned from get_descriptors_async
30Nov2023 02:29:49 [: bleak.backends.winrt.client :] DEBUG -> calling get_characteristics_async
30Nov2023 02:29:51 [: bleak.backends.winrt.client :] DEBUG -> returned from get_characteristics_async
30Nov2023 02:29:51 [: bleak.backends.winrt.client :] DEBUG -> calling get_descriptors_async
30Nov2023 02:29:51 [: bleak.backends.winrt.client :] DEBUG -> returned from get_descriptors_async
30Nov2023 02:29:51 [: bleak.backends.winrt.client :] DEBUG -> calling get_descriptors_async
30Nov2023 02:29:52 [: bleak.backends.winrt.client :] DEBUG -> returned from get_descriptors_async
30Nov2023 02:29:52 [: bleak.backends.winrt.client :] DEBUG -> calling get_characteristics_async
30Nov2023 02:29:53 [: bleak.backends.winrt.client :] DEBUG -> session_status_changed_event_handler: id: BluetoothLE#BluetoothLE44:85:00:63:7e:cc-80:e1:26:14:11:3d, error: 0, status: 0
30Nov2023 02:29:53 [: bleak.backends.winrt.client :] DEBUG -> max_pdu_size_changed_handler: 23
30Nov2023 02:29:53 [: bleak.backends.winrt.client :] DEBUG -> disposing service objects

I would suggest logging Bluetooth packets to try to see why it is disconnecting. https://bleak.readthedocs.io/en/latest/troubleshooting.html

Hard to say if it is behaving as expected or not without seeing a full program.

I would suggest logging Bluetooth packets to try to see why it is disconnecting. https://bleak.readthedocs.io/en/latest/troubleshooting.html

Hard to say if it is behaving as expected or not without seeing a full program.

Thank you for answering, @dlech

That log I put in my query comes from Bleak.
The program has to connect to the device and once connected it works just fine.
Consistently achieving the connection is the problem. I have to connect to the device once every minute. I have run the program for more than 30 hours without errors. But then, suddenly, it hangs and never comes back from the line:

async with BleakClient(client_address) as client:

I have seen the logged error "session_status_changed_event_handler: id: ..." in another program that tries to connect to the same kind of device, but that program is in C## and my colleague uses another driver.