Merge branch 'google3' to roll forward up to cl/355961673.

This commit is contained in:
hai007
2021-02-08 04:23:31 -08:00
2 changed files with 23 additions and 19 deletions
+18 -19
View File
@@ -33,18 +33,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_);
@@ -82,17 +76,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();
}
@@ -283,6 +283,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