Files
nearby/cpp/core/internal/mediums/bluetooth_radio.cc
T
Alexey Polyudov 2155b3ddeb Roll forward to cl/338482889
Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I9850950db8bd84f0904ea1a413151887f52098cf
2020-10-22 10:47:34 -07:00

107 lines
2.6 KiB
C++

#include "core/internal/mediums/bluetooth_radio.h"
#include "platform/base/exception.h"
#include "platform/public/logging.h"
#include "platform/public/system_clock.h"
namespace location {
namespace nearby {
namespace connections {
constexpr absl::Duration BluetoothRadio::kPauseBetweenToggle;
BluetoothRadio::BluetoothRadio() {
if (!IsAdapterValid()) {
NEARBY_LOG(ERROR, "Bluetooth adapter is not valid: BT is not supported");
}
}
BluetoothRadio::~BluetoothRadio() {
// We never enabled Bluetooth, nothing to do.
if (!ever_saved_state_.Get()) {
NEARBY_LOG(INFO, "BT adapter was not used. Not touching HW.");
return;
}
// Toggle Bluetooth regardless of our original state. Some devices/chips can
// start to freak out after some time (e.g. b/37775337), and this helps to
// ensure BT resets properly.
NEARBY_LOG(INFO, "Toggle BT adapter state before releasing adapter.");
Toggle();
NEARBY_LOG(INFO, "Bring BT adapter to original state");
if (!SetBluetoothState(originally_enabled_.Get())) {
NEARBY_LOG(INFO, "Failed to restore BT adapter original state.");
}
}
bool BluetoothRadio::Enable() {
if (!SaveOriginalState()) {
return false;
}
return SetBluetoothState(true);
}
bool BluetoothRadio::Disable() {
if (!SaveOriginalState()) {
return false;
}
return SetBluetoothState(false);
}
bool BluetoothRadio::IsEnabled() const {
return IsAdapterValid() && IsInDesiredState(true);
}
bool BluetoothRadio::Toggle() {
if (!SaveOriginalState()) {
return false;
}
if (!SetBluetoothState(false)) {
NEARBY_LOG(INFO, "BT Toggle: Failed to turn BT off.");
return false;
}
if (SystemClock::Sleep(kPauseBetweenToggle).Raised(Exception::kInterrupted)) {
NEARBY_LOG(INFO, "BT Toggle: interrupted before turing on.");
return false;
}
if (!SetBluetoothState(true)) {
NEARBY_LOG(INFO, "BT Toggle: Failed to turn BT on.");
return false;
}
return true;
}
bool BluetoothRadio::SetBluetoothState(bool enable) {
return bluetooth_adapter_.SetStatus(
enable ? BluetoothAdapter::Status::kEnabled
: BluetoothAdapter::Status::kDisabled);
}
bool BluetoothRadio::IsInDesiredState(bool should_be_enabled) const {
return bluetooth_adapter_.IsEnabled() == should_be_enabled;
}
bool BluetoothRadio::SaveOriginalState() {
if (!IsAdapterValid()) {
return false;
}
// If we haven't saved the original state of the radio, save it.
if (!ever_saved_state_.Set(true)) {
originally_enabled_.Set(bluetooth_adapter_.IsEnabled());
}
return true;
}
} // namespace connections
} // namespace nearby
} // namespace location