From 461985419fa0f372fea89a07c5a6d4875773f822 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 6 Apr 2023 21:39:15 -0700 Subject: [PATCH] Fixed the logic issue to handle peripheral in BLE v2 PiperOrigin-RevId: 522520410 --- internal/platform/ble_v2.cc | 10 ++++++---- .../platform/implementation/windows/ble_medium.cc | 11 ++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/internal/platform/ble_v2.cc b/internal/platform/ble_v2.cc index f7c197a1..6fd759c9 100644 --- a/internal/platform/ble_v2.cc +++ b/internal/platform/ble_v2.cc @@ -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) diff --git a/internal/platform/implementation/windows/ble_medium.cc b/internal/platform/implementation/windows/ble_medium.cc index 389afc70..9af49ba4 100644 --- a/internal/platform/implementation/windows/ble_medium.cc +++ b/internal/platform/implementation/windows/ble_medium.cc @@ -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; } }