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 <fdimanidis@gmail.com>
This commit is contained in:
Rohit Sangwan
2024-12-09 20:38:12 +05:30
committed by GitHub
parent 5be22ac0e6
commit bbf9703303
2 changed files with 27 additions and 7 deletions
+7
View File
@@ -10,6 +10,13 @@
"request": "launch",
"type": "dart"
},
{
"name": "example release",
"cwd": "example",
"request": "launch",
"type": "dart",
"flutterMode": "release"
},
{
"name": "example_mock",
"cwd": "example",
+20 -7
View File
@@ -34,6 +34,8 @@ namespace universal_ble
std::unique_ptr<UniversalBleCallbackChannel> callbackChannel;
std::unordered_map<std::string, winrt::event_token> 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<UniversalBlePlugin>(registrar);
@@ -56,9 +58,20 @@ namespace universal_ble
void UniversalBlePlugin::GetBluetoothAvailabilityState(std::function<void(ErrorOr<int64_t> reply)> result)
{
if (!bluetoothRadio)
result(static_cast<int>(AvailabilityState::unsupported));
{
if (!initialized)
{
result(static_cast<int>(AvailabilityState::unknown));
}
else
{
result(static_cast<int>(AvailabilityState::unsupported));
}
}
else
{
result(static_cast<int>(getAvailabilityStateFromRadio(bluetoothRadio.State())));
}
};
void UniversalBlePlugin::EnableBluetooth(std::function<void(ErrorOr<bool> 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<int>(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;