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;