Fixed the logic issue to handle peripheral in BLE v2

PiperOrigin-RevId: 522520410
This commit is contained in:
Guogang Li
2023-04-06 21:40:41 -07:00
committed by Copybara-Service
parent 87e044fa89
commit 461985419f
2 changed files with 12 additions and 9 deletions
+6 -4
View File
@@ -82,12 +82,15 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid,
[this](api::ble_v2::BlePeripheral& peripheral,
BleAdvertisementData advertisement_data) {
MutexLock lock(&mutex_);
if (!peripherals_.contains(&peripheral)) {
if (peripherals_.contains(&peripheral)) {
NEARBY_LOGS(INFO)
<< "There is no need to callback due to peripheral impl="
<< &peripheral << ", which already exists.";
return;
} else {
peripherals_.insert(&peripheral);
}
BleV2Peripheral proxy(&peripheral);
NEARBY_LOGS(INFO)
<< "New peripheral imp=" << &peripheral
@@ -134,9 +137,8 @@ BleV2Medium::StartScanningTmp(
service_uuid, tx_power_level,
api::ble_v2::BleMedium::ScanCallback{
.advertisement_found_cb =
[this, &callback](
api::ble_v2::BlePeripheral& peripheral,
BleAdvertisementData advertisement_data) {
[this, &callback](api::ble_v2::BlePeripheral& peripheral,
BleAdvertisementData advertisement_data) {
MutexLock lock(&mutex_);
if (!peripherals_.contains(&peripheral)) {
NEARBY_LOGS(INFO)
@@ -534,8 +534,9 @@ void BleMedium::WatcherHandler(
break;
}
// No matter the reason, should clean up the watcher if it is not empty.
// The BLE V1 interface doesn't have API to return the error to upper layer.
// No matter the reason, I should clean up the watcher if it is not empty.
// The BLE V1 interface doesn't have an API to return the error to the upper
// layer.
if (watcher_ != nullptr) {
NEARBY_LOGS(ERROR) << "Nearby BLE Medium cleaned the watcher.";
watcher_.Stopped(watcher_token_);
@@ -590,14 +591,14 @@ void BleMedium::AdvertisementReceivedHandler(
service_id_) != advertisement_data) {
NEARBY_LOGS(INFO) << "BLE reports lost device: " << peripheral_name;
// Lost the device first and then report discovered the device.
// Lost the device first and then the report discovered the device.
advertisement_received_callback_.peripheral_lost_cb(
/*ble_peripheral*/ *peripheral_map_[peripheral_name],
/*service_id*/ service_id_);
} else {
// The device already reported to discovery, don't need to call it
// again.
// The device is already reported to discover, so don't need to
// call it again.
return;
}
}