Set g3 bluetooth adapter to be enabled by default and remove Toggle()

PiperOrigin-RevId: 509962396
This commit is contained in:
Aaron Yu
2023-02-15 16:25:43 -08:00
committed by Copybara-Service
parent afd1a0b10d
commit fc1811f966
9 changed files with 14 additions and 97 deletions
-27
View File
@@ -18,15 +18,11 @@
#include <string>
#include <utility>
#include "internal/platform/exception.h"
#include "internal/platform/logging.h"
#include "internal/platform/system_clock.h"
namespace nearby {
namespace fastpair {
constexpr absl::Duration Ble::kPauseBetweenToggle;
Ble::~Ble() {
// We never enabled Bluetooth, nothing to do.
if (!ever_saved_state_.Get()) {
@@ -60,29 +56,6 @@ bool Ble::IsEnabled() const {
return IsAdapterValid() && IsInDesiredState(true);
}
bool Ble::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 Ble::SetBluetoothState(bool enable) {
return adapter_.SetStatus(enable ? BluetoothAdapter::Status::kEnabled
: BluetoothAdapter::Status::kDisabled);
+1 -7
View File
@@ -20,7 +20,6 @@
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/time/clock.h"
#include "internal/platform/atomic_boolean.h"
#include "internal/platform/ble.h"
#include "internal/platform/bluetooth_adapter.h"
@@ -52,10 +51,6 @@ class Ble {
// Returns true if the Bluetooth radio is currently enabled.
bool IsEnabled() const;
// Turn BT radio Off, delay for kPauseBetweenToggle and then turn it On.
// This will block calling thread for at least kPauseBetweenToggle duration.
bool Toggle();
// Returns true if Ble communications are supported by a platform.
bool IsAvailable() const ABSL_LOCKS_EXCLUDED(mutex_);
@@ -95,7 +90,6 @@ class Ble {
DiscoveredPeripheralCallback discovered_peripheral_callback_;
BleMedium medium_ ABSL_GUARDED_BY(mutex_){adapter_};
bool is_scanning_ = false;
static constexpr absl::Duration kPauseBetweenToggle = absl::Seconds(3);
// Same as IsAvailable(), but must be called with mutex_ held.
bool IsAvailableLocked() const ABSL_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
@@ -107,7 +101,7 @@ class Ble {
bool IsInDesiredState(bool should_be_enabled) const;
// To be called in enable(), disable(), and toggle(). This will remember the
// To be called in enable() and disable(). This will remember the
// original state of the ble before any ble state has been modified.
// Returns false if Bluetooth doesn't exist on the device and the state cannot
// be obtained.
+4 -12
View File
@@ -47,6 +47,8 @@ TEST_F(BleTest, ConstructorDestructorWorks) {
TEST_F(BleTest, CanEnable) {
Ble ble;
EXPECT_TRUE(ble.IsAdapterValid());
EXPECT_TRUE(ble.IsEnabled());
EXPECT_TRUE(ble.Disable());
EXPECT_FALSE(ble.IsEnabled());
EXPECT_TRUE(ble.Enable());
EXPECT_TRUE(ble.IsEnabled());
@@ -55,21 +57,11 @@ TEST_F(BleTest, CanEnable) {
TEST_F(BleTest, CanDisable) {
Ble ble;
EXPECT_TRUE(ble.IsAdapterValid());
EXPECT_FALSE(ble.IsEnabled());
EXPECT_TRUE(ble.Enable());
EXPECT_TRUE(ble.IsEnabled());
EXPECT_TRUE(ble.Disable());
EXPECT_FALSE(ble.IsEnabled());
}
TEST_F(BleTest, CanToggle) {
Ble ble;
EXPECT_TRUE(ble.IsAdapterValid());
EXPECT_FALSE(ble.IsEnabled());
EXPECT_TRUE(ble.Toggle());
EXPECT_TRUE(ble.IsEnabled());
}
TEST_F(BleTest, CanConstructValidObject) {
env_.Start();
Ble ble_a;
@@ -77,10 +69,10 @@ TEST_F(BleTest, CanConstructValidObject) {
EXPECT_TRUE(ble_a.IsMediumValid());
EXPECT_TRUE(ble_a.IsAdapterValid());
EXPECT_FALSE(ble_a.IsAvailable());
EXPECT_TRUE(ble_a.IsAvailable());
EXPECT_TRUE(ble_b.IsMediumValid());
EXPECT_TRUE(ble_b.IsAdapterValid());
EXPECT_FALSE(ble_b.IsAvailable());
EXPECT_TRUE(ble_b.IsAvailable());
EXPECT_NE(&ble_a.GetBluetoothAdapter(), &ble_b.GetBluetoothAdapter());
env_.Stop();
}