mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Set g3 bluetooth adapter to be enabled by default and remove Toggle()
PiperOrigin-RevId: 509962396
This commit is contained in:
committed by
Copybara-Service
parent
afd1a0b10d
commit
fc1811f966
@@ -178,10 +178,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(&radio_a.GetBluetoothAdapter(), &radio_b.GetBluetoothAdapter());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -14,15 +14,11 @@
|
||||
|
||||
#include "connections/implementation/mediums/bluetooth_radio.h"
|
||||
|
||||
#include "internal/platform/exception.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/system_clock.h"
|
||||
|
||||
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");
|
||||
@@ -62,29 +58,6 @@ 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
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "absl/time/clock.h"
|
||||
#include "internal/platform/atomic_boolean.h"
|
||||
#include "internal/platform/bluetooth_adapter.h"
|
||||
|
||||
@@ -50,21 +49,15 @@ class BluetoothRadio {
|
||||
// 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 result of BluetoothAdapter::IsValid() for private adapter instance.
|
||||
bool IsAdapterValid() const { return bluetooth_adapter_.IsValid(); }
|
||||
|
||||
BluetoothAdapter& GetBluetoothAdapter() { return bluetooth_adapter_; }
|
||||
|
||||
private:
|
||||
static constexpr absl::Duration kPauseBetweenToggle = absl::Seconds(3);
|
||||
|
||||
bool SetBluetoothState(bool enable);
|
||||
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 radio before any radio state has been modified.
|
||||
// Returns false if Bluetooth doesn't exist on the device and the state cannot
|
||||
// be obtained.
|
||||
|
||||
@@ -30,6 +30,8 @@ TEST(BluetoothRadioTest, ConstructorDestructorWorks) {
|
||||
TEST(BluetoothRadioTest, CanEnable) {
|
||||
BluetoothRadio radio;
|
||||
EXPECT_TRUE(radio.IsAdapterValid());
|
||||
EXPECT_TRUE(radio.IsEnabled());
|
||||
EXPECT_TRUE(radio.Disable());
|
||||
EXPECT_FALSE(radio.IsEnabled());
|
||||
EXPECT_TRUE(radio.Enable());
|
||||
EXPECT_TRUE(radio.IsEnabled());
|
||||
@@ -38,21 +40,11 @@ TEST(BluetoothRadioTest, CanEnable) {
|
||||
TEST(BluetoothRadioTest, CanDisable) {
|
||||
BluetoothRadio radio;
|
||||
EXPECT_TRUE(radio.IsAdapterValid());
|
||||
EXPECT_FALSE(radio.IsEnabled());
|
||||
EXPECT_TRUE(radio.Enable());
|
||||
EXPECT_TRUE(radio.IsEnabled());
|
||||
EXPECT_TRUE(radio.Disable());
|
||||
EXPECT_FALSE(radio.IsEnabled());
|
||||
}
|
||||
|
||||
TEST(BluetoothRadioTest, CanToggle) {
|
||||
BluetoothRadio radio;
|
||||
EXPECT_TRUE(radio.IsAdapterValid());
|
||||
EXPECT_FALSE(radio.IsEnabled());
|
||||
EXPECT_TRUE(radio.Toggle());
|
||||
EXPECT_TRUE(radio.IsEnabled());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -130,16 +130,16 @@ TEST(BluetoothAdapterTest, ConstructorDestructorWorks) {
|
||||
TEST(BluetoothAdapterTest, CanSetName) {
|
||||
constexpr char kAdapterName[] = "MyBtAdapter";
|
||||
BluetoothAdapter adapter;
|
||||
EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kDisabled);
|
||||
EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kEnabled);
|
||||
EXPECT_TRUE(adapter.SetName(kAdapterName));
|
||||
EXPECT_EQ(adapter.GetName(), std::string(kAdapterName));
|
||||
}
|
||||
|
||||
TEST(BluetoothAdapterTest, CanSetStatus) {
|
||||
BluetoothAdapter adapter;
|
||||
EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kDisabled);
|
||||
EXPECT_TRUE(adapter.SetStatus(BluetoothAdapter::Status::kEnabled));
|
||||
EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kEnabled);
|
||||
EXPECT_TRUE(adapter.SetStatus(BluetoothAdapter::Status::kDisabled));
|
||||
EXPECT_EQ(adapter.GetStatus(), BluetoothAdapter::Status::kDisabled);
|
||||
}
|
||||
|
||||
TEST(BluetoothAdapterTest, CanSetMode) {
|
||||
|
||||
@@ -155,7 +155,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
|
||||
std::string mac_address_;
|
||||
ScanMode mode_ ABSL_GUARDED_BY(mutex_) = ScanMode::kNone;
|
||||
std::string name_ ABSL_GUARDED_BY(mutex_) = "unknown G3 BT device";
|
||||
bool enabled_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
bool enabled_ ABSL_GUARDED_BY(mutex_) = true;
|
||||
};
|
||||
|
||||
} // namespace g3
|
||||
|
||||
Reference in New Issue
Block a user