From bbf9703303315087146bed487fbb1b970b00cfd0 Mon Sep 17 00:00:00 2001 From: Rohit Sangwan Date: Mon, 9 Dec 2024 20:38:12 +0530 Subject: [PATCH] Fix Windows Bluetooth availability state in Release build (#112) * Fix Windows Bluetooth availability state in Release build * Make initialized private --------- Co-authored-by: Foti Dim --- .vscode/launch.json | 7 +++++++ windows/src/universal_ble_plugin.cpp | 27 ++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index f944144..ccb1785 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,6 +10,13 @@ "request": "launch", "type": "dart" }, + { + "name": "example release", + "cwd": "example", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, { "name": "example_mock", "cwd": "example", diff --git a/windows/src/universal_ble_plugin.cpp b/windows/src/universal_ble_plugin.cpp index f406339..2067b83 100644 --- a/windows/src/universal_ble_plugin.cpp +++ b/windows/src/universal_ble_plugin.cpp @@ -34,6 +34,8 @@ namespace universal_ble std::unique_ptr callbackChannel; std::unordered_map characteristicsTokens{}; // TODO: Remove the map and store the token inside the characteristic object + bool initialized = false; + void UniversalBlePlugin::RegisterWithRegistrar(flutter::PluginRegistrarWindows *registrar) { auto plugin = std::make_unique(registrar); @@ -56,9 +58,20 @@ namespace universal_ble void UniversalBlePlugin::GetBluetoothAvailabilityState(std::function reply)> result) { if (!bluetoothRadio) - result(static_cast(AvailabilityState::unsupported)); + { + if (!initialized) + { + result(static_cast(AvailabilityState::unknown)); + } + else + { + result(static_cast(AvailabilityState::unsupported)); + } + } else + { result(static_cast(getAvailabilityStateFromRadio(bluetoothRadio.State()))); + } }; void UniversalBlePlugin::EnableBluetooth(std::function reply)> result) @@ -485,13 +498,17 @@ namespace universal_ble { bluetoothRadio = radio; radioStateChangedRevoker = bluetoothRadio.StateChanged(winrt::auto_revoke, {this, &UniversalBlePlugin::Radio_StateChanged}); + Radio_StateChanged(bluetoothRadio, nullptr); break; } } if (!bluetoothRadio) { std::cout << "Bluetooth is not available" << std::endl; + uiThreadHandler_.Post([] + { callbackChannel->OnAvailabilityChanged(static_cast(AvailabilityState::unsupported), SuccessCallback, ErrorCallback); }); } + initialized = true; } winrt::fire_and_forget UniversalBlePlugin::PairAsync( @@ -825,18 +842,14 @@ namespace universal_ble { auto state = [=]() -> AvailabilityState { - if (radioState == RadioState::Unknown) + if (radioState == RadioState::On) { - return AvailabilityState::unknown; + return AvailabilityState::poweredOn; } else if (radioState == RadioState::Off) { return AvailabilityState::poweredOff; } - else if (radioState == RadioState::On) - { - return AvailabilityState::poweredOn; - } else if (radioState == RadioState::Disabled) { return AvailabilityState::unsupported;