hbldh / bleak

A cross platform Bluetooth Low Energy Client for Python using asyncio
MIT License
1.58k stars 277 forks source link

connect does not succeed if called a second time #1432

Open aaronfultonnz opened 8 months ago

aaronfultonnz commented 8 months ago

Description

I am developing a Bluetooth device and often need to reset the MCU. After an MCU reset, I call disconnect() to ensure the PC side of things is shut down, then call connect() again after the MCU reset, however, connect() never completes. I have traced this down to the self._session.add_session_status_changed in the winrt/client.py file not being called on the second connect() call.

If I don't first call disconnect() then I get the following error: bleak.backends.winrt.client :: getting services (service_cache_mode=None, cache_mode=None)... [2023-10-04 16:11:31,563] :: DEBUG :: bleak.backends.winrt.client :: skipping service 6e400001-b5a3-f393-e0a9-e50e24dcca9e due to access denied

If I don't reset the MCU and just call disconnect() and then connect() there are no errors.

Logs

Here are the Bluetooth capture logs Logs.zip

dlech commented 7 months ago

I have traced this down to the self._session.add_session_status_changed in the winrt/client.py file not being called on the second connect() call.

So in other words, self._session = await GattSession.from_device_id_async() is hanging forever and not returning?

then I get the following error:

This is just a debug message, not an error.

Here are the Bluetooth capture logs

Thanks for the logs. It looks like the difference is in the good connect, the services are being enumerated whereas in the bad connect, it is using cached services and looking at the database hash characteristic. You might be able to play around with the windows-specific cache options to force it to never use the cache for getting services on Windows.

It would also be helpful to see the program used to reproduce the problem.