Save scan callbacks before starting scan session.

This reduces flakiness. We can start receiving advertisements before StartScan() has finished.

PiperOrigin-RevId: 485904791
This commit is contained in:
Janusz Sobczak
2022-11-03 10:13:02 -07:00
committed by Copybara-Service
parent 74f7f5ec5c
commit d268b077c9
2 changed files with 11 additions and 8 deletions
+7 -8
View File
@@ -66,6 +66,13 @@ ScanSession ScanManager::StartScan(ScanRequest scan_request, ScanCallback cb) {
[this](BlePeripheral& peripheral, BleAdvertisementData data) {
NotifyFoundBle(data, peripheral);
}};
// We will not be needing the start_scan_cb anymore, so cb is ok to use here.
AddScanCallback(id, MapElement{
.request = scan_request,
.callback = cb,
.decoder = AdvertisementDecoder(credential_manager_,
scan_request),
});
std::unique_ptr<ScanningSession> scanning_session =
mediums_->GetBle().StartScanning(scan_request, std::move(callback));
auto modified_scanning_session = ScanSession(
@@ -82,14 +89,6 @@ ScanSession ScanManager::StartScan(ScanRequest scan_request, ScanCallback cb) {
}
return Status{.value = Status::Value::kSuccess};
});
absl::MutexLock lock(&mutex_);
// We will not be needing the start_scan_cb anymore, so cb is ok to use here.
scanning_callbacks_.emplace(id, MapElement{
.request = scan_request,
.callback = cb,
.decoder = AdvertisementDecoder(
credential_manager_, scan_request),
});
return modified_scanning_session;
}
+4
View File
@@ -57,6 +57,10 @@ class ScanManager {
ScanCallback callback;
AdvertisementDecoder decoder;
};
void AddScanCallback(uint64_t id, MapElement element) {
absl::MutexLock lock(&mutex_);
scanning_callbacks_.insert({id, element});
}
mutable absl::Mutex mutex_;
Mediums* mediums_;
CredentialManager* credential_manager_;