From e22a3ddf99b60d366626d3f23fc1dcc1e0db9b40 Mon Sep 17 00:00:00 2001 From: Rohit Sangwan Date: Wed, 13 Mar 2024 17:33:42 +0530 Subject: [PATCH] Fix windows crash when no bluetooth adapter available (#20) --- windows/src/universal_ble_plugin.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/windows/src/universal_ble_plugin.cpp b/windows/src/universal_ble_plugin.cpp index 24a0bf3..7c8016b 100644 --- a/windows/src/universal_ble_plugin.cpp +++ b/windows/src/universal_ble_plugin.cpp @@ -500,11 +500,19 @@ namespace universal_ble winrt::fire_and_forget UniversalBlePlugin::InitializeAsync() { - auto bluetoothAdapter = co_await BluetoothAdapter::GetDefaultAsync(); - bluetoothRadio = co_await bluetoothAdapter.GetRadioAsync(); - if (bluetoothRadio) + auto radios = co_await Radio::GetRadiosAsync(); + for (auto &&radio : radios) { - radioStateChangedRevoker = bluetoothRadio.StateChanged(winrt::auto_revoke, {this, &UniversalBlePlugin::Radio_StateChanged}); + if (radio.Kind() == RadioKind::Bluetooth) + { + bluetoothRadio = radio; + radioStateChangedRevoker = bluetoothRadio.StateChanged(winrt::auto_revoke, {this, &UniversalBlePlugin::Radio_StateChanged}); + break; + } + } + if (!bluetoothRadio) + { + std::cout << "Bluetooth is not available" << std::endl; } }