From cb8fef78755d95c0df6db3949127bc37fd6e65d9 Mon Sep 17 00:00:00 2001 From: aaronyujiaze Date: Tue, 24 May 2022 15:03:36 -0700 Subject: [PATCH] Add fix to check the availability of BluetoothAdapter This change makes sure BluetoothClassic and BLE medium can only be added to the CONNECTION_REQUEST frame if BluetoothAdapter is valid and enabled. PiperOrigin-RevId: 450777716 --- connections/implementation/mediums/ble.cc | 4 +++- connections/implementation/mediums/ble_test.cc | 4 ++-- connections/implementation/mediums/bluetooth_classic.cc | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/connections/implementation/mediums/ble.cc b/connections/implementation/mediums/ble.cc index 5e1fe879..80eb49c4 100644 --- a/connections/implementation/mediums/ble.cc +++ b/connections/implementation/mediums/ble.cc @@ -46,7 +46,9 @@ bool Ble::IsAvailable() const { return IsAvailableLocked(); } -bool Ble::IsAvailableLocked() const { return medium_.IsValid(); } +bool Ble::IsAvailableLocked() const { + return medium_.IsValid() && adapter_.IsValid() && adapter_.IsEnabled(); +} bool Ble::StartAdvertising(const std::string& service_id, const ByteArray& advertisement_bytes, diff --git a/connections/implementation/mediums/ble_test.cc b/connections/implementation/mediums/ble_test.cc index 17f53df2..ea60ad18 100644 --- a/connections/implementation/mediums/ble_test.cc +++ b/connections/implementation/mediums/ble_test.cc @@ -181,10 +181,10 @@ TEST_F(BleTest, CanConstructValidObject) { EXPECT_TRUE(ble_a.IsMediumValid()); EXPECT_TRUE(ble_a.IsAdapterValid()); - EXPECT_TRUE(ble_a.IsAvailable()); + EXPECT_FALSE(ble_a.IsAvailable()); EXPECT_TRUE(ble_b.IsMediumValid()); EXPECT_TRUE(ble_b.IsAdapterValid()); - EXPECT_TRUE(ble_b.IsAvailable()); + EXPECT_FALSE(ble_b.IsAvailable()); EXPECT_NE(&radio_a.GetBluetoothAdapter(), &radio_b.GetBluetoothAdapter()); env_.Stop(); } diff --git a/connections/implementation/mediums/bluetooth_classic.cc b/connections/implementation/mediums/bluetooth_classic.cc index 3f4c051f..143ee363 100644 --- a/connections/implementation/mediums/bluetooth_classic.cc +++ b/connections/implementation/mediums/bluetooth_classic.cc @@ -64,7 +64,7 @@ bool BluetoothClassic::IsAvailable() const { } bool BluetoothClassic::IsAvailableLocked() const { - return medium_.IsValid() && adapter_.IsValid(); + return medium_.IsValid() && adapter_.IsValid() && adapter_.IsEnabled(); } bool BluetoothClassic::TurnOnDiscoverability(const std::string& device_name) {