Windows: allow to create Bluetooth classes in secondary threads


The commit 340b84a5578f78d7a399e369e900ac991d9e0da2 fixed COM
initialization for the Bluetooth classes that required it, but at the
same time forced it to happen in the main thread only.

However, there seems to be no strong reasoning for this limitation. It
might be the legacy of WinRT backend, which was indeed running
everything on the UI thread.

This patch updates implementation to only require that an object calls
CoInitializeEx() and CoUninitialize() on the same thread. We still use
COINIT_APARTMENTTHREADED model by default, because that's required by
the UI threads. However, QtBluetooth itself does not have such
limitation. It can also work if the thread is initialized with the
COINIT_MULTITHREADED model. That's why the updated code does not
generate a warning if CoInitializeEx() returns RPC_E_CHANGED_MODE.

Note that since the initialization/uninitialization can now happen in
different threads, we need to protect the access to a global cache
with mutex.

This patch adds a new Windows-specific test-case that runs device
discovery in a secondary thread in three modes:
* QtBluetooth is responsible for initializing the thread;
* The thread is manually initialized with multithreaded model;
* The thread is manually initialized with apartment model.

[ChangeLog][QtBluetooth][Windows] Bluetooth classes on Windows can
now be created and used in secondary threads.

Fixes: QTBUG-142821
Pick-to: 6.11
Change-Id: I4a4c4f5a9f19dcc016d3789f7e8212b493772b54
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
commit11e5c8710fe7bcc97dfc682c0f247dd922e1a0ed
parentdf63080a3f31b2fc0e2b08c8ebc90dc0dc67b51f
authorIvan Solovev <ivan.solovev@qt.io>
committerIvan Solovev <ivan.solovev@qt.io>
commit timeTue, 12 May 2026 11:02:15 +0000