From cad1ef6bf349e20afd4b97cf16750798e65158bd Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Tue, 13 Aug 2024 15:04:14 -0700 Subject: [PATCH] Fixed a bug in GATT client PiperOrigin-RevId: 662666561 --- .../platform/implementation/windows/ble_v2.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/platform/implementation/windows/ble_v2.cc b/internal/platform/implementation/windows/ble_v2.cc index be24f1a5..380cffea 100644 --- a/internal/platform/implementation/windows/ble_v2.cc +++ b/internal/platform/implementation/windows/ble_v2.cc @@ -1216,8 +1216,12 @@ void BleV2Medium::AdvertisementFoundHandler( bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address, GetRemotePeripheralCallback callback) { - absl::MutexLock lock(&mutex_); - BleV2Peripheral* peripheral = GetOrCreatePeripheral(mac_address); + BleV2Peripheral* peripheral = nullptr; + { + absl::MutexLock lock(&mutex_); + BleV2Peripheral* peripheral = GetOrCreatePeripheral(mac_address); + } + if (peripheral != nullptr && peripheral->Ok()) { callback(*peripheral); return true; @@ -1227,8 +1231,12 @@ bool BleV2Medium::GetRemotePeripheral(const std::string& mac_address, bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) { - absl::MutexLock lock(&mutex_); - BleV2Peripheral* peripheral = GetPeripheral(id); + BleV2Peripheral* peripheral = nullptr; + { + absl::MutexLock lock(&mutex_); + BleV2Peripheral* peripheral = GetPeripheral(id); + } + if (peripheral == nullptr) { NEARBY_LOGS(WARNING) << __func__ << ": No matched peripheral device."; return false;