OpenBluetoothToolbox / SimpleBLE

SimpleBLE - the all-in-one Bluetooth library for MacOS, iOS, Windows, Linux and Android.
https://www.simpleble.org
Other
701 stars 118 forks source link

Fix WinRT initialization (again) #156

Closed kdewald closed 1 year ago

Andrey1994 commented 1 year ago

Hi @kdewald, I've ported this commit to brainflow and now it cannot connect to device anymore...

messages from simpleble:

INFO [SimpleBLE] D:\a\brainflow\brainflow\third_party\SimpleBLE\simpleble\src\backends\windows\Utils.cpp:56 initialize_winrt - CoGetApartmentType: cotype=-1, qualifier=0, result=-2147221008
INFO [SimpleBLE] D:\a\brainflow\brainflow\third_party\SimpleBLE\simpleble\src\backends\windows\Utils.cpp:101 initialize_winrt - CoInitializeEx: result=0

So far, I tested it only from python binding for brainflow

The only change I made is https://github.com/brainflow-dev/brainflow/pull/589

Andrey1994 commented 1 year ago

the same if I run it from C++ console app:

[2023-01-03 23:25:52.198] [board_logger] [debug] use dyn lib: D:\workspace\brainflow\installed\lib\simpleble-c.dll
INFO [SimpleBLE] D:\workspace\brainflow\third_party\SimpleBLE\simpleble\src\backends\windows\Utils.cpp:56 initialize_winrt - CoGetApartmentType: cotype=-1, qualifier=0, result=-2147221008
INFO [SimpleBLE] D:\workspace\brainflow\third_party\SimpleBLE\simpleble\src\backends\windows\Utils.cpp:101 initialize_winrt - CoInitializeEx: result=0
[2023-01-03 23:25:52.236] [board_logger] [info] found 1 BLE adapter(s)
[2023-01-03 23:25:52.330] [board_logger] [trace] address be:89:40:02:18:00
[2023-01-03 23:25:52.330] [board_logger] [trace] identifier ELK-BLEDOM
[2023-01-03 23:25:52.430] [board_logger] [trace] address 00:55:da:b9:2d:11
[2023-01-03 23:25:52.430] [board_logger] [trace] identifier MuseS-2D11
[2023-01-03 23:25:52.430] [board_logger] [info] Found Muse device
[2023-01-03 23:26:02.444] [board_logger] [error] Failed to connect to Muse Device
Andrey1994 commented 1 year ago

for me it starts to connect again if I change this:

         } else {
             // qualifier is an unknown value.
             // Initialize the apartment with the default concurrency model.
-            result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
+            result = WINRT_IMPL_CoInitializeEx(nullptr, COINIT_MULTITHREADED);
         }
Andrey1994 commented 1 year ago

Maybe stupid idea but if we cannot find a better option and if it helps somebody else, can we add an option to select default concurency model for unknown value of qualifier?

Ideally directly via API, if there is no decent way to pass it via function arguments and keep it more or less OS agnostic even env variable will be ok. For me it seems like its ok to just change a default value for unknown qualifier, but maybe for some cases its correct to keep as is.

And it seems like COINIT_MULTITHREADED is a default. https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex "The default is COINIT_MULTITHREADED."

kdewald commented 1 year ago

I swear that this stupid problem is driving me nuts. Unfortunately this cannot be fixed with flags as the initialization path heavily depends on how other libraries are interacting with WinRT and how those threads interact with SimpleBLE.

I just applied your change to make sure that you're unblocked while I continue researching this damn issue. The documentation you shared has an interesting suggestion that I'm going to research now.