hbldh / bleak

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

backends.winrt: add allow_sta() utility function #1567

Closed dlech closed 1 month ago

dlech commented 1 month ago

We had lots of users reporting issues with the WinRT backend hanging forever when trying to connect to a device. This was happening because some other imported library was initializing the the main thread to STA which caused some async callback to never be called.

To work around this, we added a check to make sure the main thread is set to MTA rather than STA. Unfortunately, in cases where there is a graphical user interface library being used AND that library is properly interated with asynco so that Bleak runs in the main thread rather than in a background thread, the thread type does need to be STA for the GUI to work. So in those very specific conditions, we need to not raise an exception.

We don't know of a way to detect this automatically, so we added a new utility function allow_sta() that the user can call to allow Bleak to run in an STA thread.