Added a feature flag to control whether we are allowed to set the bluetooth adapter state or not on windows.

PiperOrigin-RevId: 479589889
This commit is contained in:
John Carroll
2022-10-07 08:49:24 -07:00
committed by Copybara-Service
parent d4392b4506
commit a3f5d108f5
2 changed files with 37 additions and 10 deletions
+2
View File
@@ -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() {
@@ -14,8 +14,6 @@
#include "internal/platform/implementation/windows/bluetooth_adapter.h"
#include <windows.h> // These two headers must be defined
#include <winioctl.h> // first and in this order
#include <bthdef.h>
#include <bthioctl.h>
#include <cfgmgr32.h>
@@ -25,10 +23,13 @@
#include <setupapi.h>
#include <stdio.h>
#include <usbiodef.h>
#include <windows.h> // These two headers must be defined
#include <winioctl.h> // first and in this order
#include <string>
#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;
}