Fix crash in BleV2MediumTest::StartScanningTmp

This fixes flakiness in the tests.

PiperOrigin-RevId: 551009093
This commit is contained in:
Janusz Sobczak
2023-07-25 15:09:47 -07:00
committed by Copybara-Service
parent ae69b8ff4e
commit dfdb5a6405
+8 -9
View File
@@ -89,7 +89,7 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
MutexLock lock(&mutex_);
if (!peripherals_.contains(&peripheral)) {
NEARBY_LOGS(INFO) << "Peripheral impl=" << &peripheral
<< " is not existed; adds it to the map.";
<< " does not exist; add it to the map.";
peripherals_.insert(&peripheral);
}
@@ -134,25 +134,24 @@ std::unique_ptr<api::ble_v2::BleMedium::ScanningSession>
BleV2Medium::StartScanningTmp(
const Uuid& service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
api::ble_v2::BleMedium::ScanningCallback callback) {
// auto scan_callback = std::move(callback.advertisement_found_cb);
MutexLock lock(&mutex_);
if (impl_->StartScanning(
service_uuid, tx_power_level,
api::ble_v2::BleMedium::ScanCallback{
.advertisement_found_cb =
[this, &callback](api::ble_v2::BlePeripheral& peripheral,
BleAdvertisementData advertisement_data) {
[this,
found_callback = std::move(callback.advertisement_found_cb)](
api::ble_v2::BlePeripheral& peripheral,
BleAdvertisementData advertisement_data) mutable {
MutexLock lock(&mutex_);
if (!peripherals_.contains(&peripheral)) {
NEARBY_LOGS(INFO)
<< "There is no need to callback due to peripheral "
"impl="
<< &peripheral << ", which already exists.";
<< "Peripheral impl=" << &peripheral
<< " does not exist; add it to the map.";
peripherals_.insert(&peripheral);
}
callback.advertisement_found_cb(peripheral,
advertisement_data);
found_callback(peripheral, advertisement_data);
},
})) {
callback.start_scanning_result(absl::OkStatus());