From 8876ceb8fb2974f397e4b5c5750aed13756896f8 Mon Sep 17 00:00:00 2001 From: hai007 Date: Mon, 8 Feb 2021 04:22:58 -0800 Subject: [PATCH] Roll forward up to cl/355961673. --- cpp/platform/public/bluetooth_classic.cc | 37 +++++++++---------- cpp/platform/public/bluetooth_classic_test.cc | 5 +++ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cpp/platform/public/bluetooth_classic.cc b/cpp/platform/public/bluetooth_classic.cc index a4fc80f0..af53b863 100644 --- a/cpp/platform/public/bluetooth_classic.cc +++ b/cpp/platform/public/bluetooth_classic.cc @@ -19,18 +19,12 @@ BluetoothSocket BluetoothClassicMedium::ConnectToService( } bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) { - { - MutexLock lock(&mutex_); - if (discovery_enabled_) { - NEARBY_LOG(INFO, "BT Discovery already enabled; impl=%p", &GetImpl()); - return false; - } - discovery_callback_ = std::move(callback); - devices_.clear(); - discovery_enabled_ = true; - NEARBY_LOG(INFO, "BT Discovery enabled; impl=%p", &GetImpl()); + MutexLock lock(&mutex_); + if (discovery_enabled_) { + NEARBY_LOG(INFO, "BT Discovery already enabled; impl=%p", &GetImpl()); + return false; } - return impl_->StartDiscovery({ + bool success = impl_->StartDiscovery({ .device_discovered_cb = [this](api::BluetoothDevice& device) { MutexLock lock(&mutex_); @@ -68,17 +62,22 @@ bool BluetoothClassicMedium::StartDiscovery(DiscoveryCallback callback) { discovery_callback_.device_lost_cb(context.device); }, }); + if (success) { + discovery_callback_ = std::move(callback); + devices_.clear(); + discovery_enabled_ = true; + NEARBY_LOG(INFO, "BT Discovery enabled; impl=%p", &GetImpl()); + } + return success; } bool BluetoothClassicMedium::StopDiscovery() { - { - MutexLock lock(&mutex_); - if (!discovery_enabled_) return true; - discovery_enabled_ = false; - discovery_callback_ = {}; - devices_.clear(); - NEARBY_LOG(INFO, "BT Discovery disabled: impl=%p", &GetImpl()); - } + MutexLock lock(&mutex_); + if (!discovery_enabled_) return true; + discovery_enabled_ = false; + discovery_callback_ = {}; + devices_.clear(); + NEARBY_LOG(INFO, "BT Discovery disabled: impl=%p", &GetImpl()); return impl_->StopDiscovery(); } diff --git a/cpp/platform/public/bluetooth_classic_test.cc b/cpp/platform/public/bluetooth_classic_test.cc index f13322b6..b03e964c 100644 --- a/cpp/platform/public/bluetooth_classic_test.cc +++ b/cpp/platform/public/bluetooth_classic_test.cc @@ -269,6 +269,11 @@ TEST_F(BluetoothClassicMediumTest, CanListenForService) { server_socket.Close(); } +TEST_F(BluetoothClassicMediumTest, FailIfDiscovering) { + EXPECT_TRUE(bt_a_->StartDiscovery({})); + EXPECT_FALSE(bt_a_->StartDiscovery({})); +} + } // namespace } // namespace nearby } // namespace location