From a3f5d108f597fa2c249a83e08ad25efe5db3a5f4 Mon Sep 17 00:00:00 2001 From: John Carroll Date: Fri, 7 Oct 2022 08:47:14 -0700 Subject: [PATCH] Added a feature flag to control whether we are allowed to set the bluetooth adapter state or not on windows. PiperOrigin-RevId: 479589889 --- internal/platform/feature_flags.h | 2 + .../windows/bluetooth_adapter.cc | 45 ++++++++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/internal/platform/feature_flags.h b/internal/platform/feature_flags.h index 384c8d79..9a287746 100644 --- a/internal/platform/feature_flags.h +++ b/internal/platform/feature_flags.h @@ -48,6 +48,8 @@ class FeatureFlags { bool support_multiple_bwu_mediums = true; // Ble v2/v1 switch flag: the flag will be removed once v2 refactor is done. bool support_ble_v2 = false; + // Allows the code to change the bluetooth radio state + bool enable_set_radio_state = false; }; static const FeatureFlags& GetInstance() { diff --git a/internal/platform/implementation/windows/bluetooth_adapter.cc b/internal/platform/implementation/windows/bluetooth_adapter.cc index a52dec15..ac119327 100644 --- a/internal/platform/implementation/windows/bluetooth_adapter.cc +++ b/internal/platform/implementation/windows/bluetooth_adapter.cc @@ -14,8 +14,6 @@ #include "internal/platform/implementation/windows/bluetooth_adapter.h" -#include // These two headers must be defined -#include // first and in this order #include #include #include @@ -25,10 +23,13 @@ #include #include #include +#include // These two headers must be defined +#include // first and in this order #include #include "absl/strings/str_format.h" +#include "internal/platform/feature_flags.h" #include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h" #include "internal/platform/implementation/windows/json/json.hpp" #include "internal/platform/implementation/windows/utils.h" @@ -95,15 +96,33 @@ bool BluetoothAdapter::SetStatus(Status status) { return false; } - bool is_radio_state_on = windows_bluetooth_radio_.State() == RadioState::On; - bool is_new_radio_state_on = status == Status::kEnabled; - if (is_radio_state_on == is_new_radio_state_on) { - NEARBY_LOGS(INFO) << __func__ - << ": Skip to set radio status due to requested state is " - "same as current."; + auto radio_state = windows_bluetooth_radio_.State(); + + if (status == Status::kDisabled && + (radio_state == RadioState::Unknown || radio_state == RadioState::Off || + radio_state == RadioState::Disabled)) { + NEARBY_LOGS(INFO) + << __func__ + << ": Skip set radio status kDisabled due to requested state is " + "already kDisabled."; return true; } + if (status == Status::kEnabled && radio_state == RadioState::On) { + NEARBY_LOGS(INFO) + << __func__ + << ": Skip set radio status kEnabled due to requested state is " + "already kEnabled."; + return true; + } + + if (!FeatureFlags::GetInstance().GetFlags().enable_set_radio_state) { + NEARBY_LOGS(INFO) << __func__ + << ": Attempt to set the radio state while " + "FeatureFlags::enable_set_radio_state is false."; + return false; + } + try { // An asynchronous operation that attempts to set the state of the radio // represented by this object. @@ -115,12 +134,18 @@ bool BluetoothAdapter::SetStatus(Status status) { } } catch (const winrt::hresult_error &ex) { NEARBY_LOGS(ERROR) << __func__ - << ": Failed to set Bluetooth radio state: " << ex.code() - << ": " << winrt::to_string(ex.message()); + << ": Failed to set Bluetooth radio state to " + << (status == Status::kDisabled ? "kDisabled." + : "kEnabled.") + << "Exception: " << ex.code() << ": " + << winrt::to_string(ex.message()); return false; } + NEARBY_LOGS(INFO) << __func__ << ": Successfully set the radio state to " + << (status == Status::kDisabled ? "kDisabled." + : "kEnabled."); return true; }