diff --git a/connections/implementation/p2p_cluster_pcp_handler.cc b/connections/implementation/p2p_cluster_pcp_handler.cc index 2dcc66be..35bad1a3 100644 --- a/connections/implementation/p2p_cluster_pcp_handler.cc +++ b/connections/implementation/p2p_cluster_pcp_handler.cc @@ -453,6 +453,15 @@ void P2pClusterPcpHandler::BluetoothNameChangedHandler( << ", service_id=" << service_id << "]: processing new name " << device_name_string; + // Make sure the Bluetooth device name points to a valid + // endpoint we're discovering. + if (!IsRecognizedBluetoothEndpoint(device_name_string, service_id, + device_name)) { + NEARBY_LOGS(INFO) << "Found unrecognized BluetoothDeviceName " + << device_name_string; + return; + } + // By this point, the BluetoothDevice passed to us has a different // name than what we may have discovered before. We need to iterate // over the found BluetoothEndpoints and compare their addresses to @@ -482,15 +491,6 @@ void P2pClusterPcpHandler::BluetoothNameChangedHandler( } } - // Make sure the Bluetooth device name points to a valid - // endpoint we're discovering. - if (!IsRecognizedBluetoothEndpoint(device_name_string, service_id, - device_name)) { - NEARBY_LOGS(INFO) << "Found unrecognized BluetoothDeviceName " - << device_name_string; - return; - } - // Report the discovered endpoint to the client. NEARBY_LOGS(INFO) << "Found BluetoothDeviceName " << device_name_string << " (with endpoint_id=" diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.cc b/internal/platform/implementation/windows/bluetooth_classic_medium.cc index a012a468..8fff2eb7 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.cc @@ -490,9 +490,14 @@ api::BluetoothDevice* BluetoothClassicMedium::GetRemoteDevice( << " is not in list. create it"; auto bluetooth_device = std::make_unique(mac_address); - mac_address_to_bluetooth_device_map_[mac_address] = - std::move(bluetooth_device); - return mac_address_to_bluetooth_device_map_[mac_address].get(); + if (IsWatcherStarted()) { + mac_address_to_bluetooth_device_map_[mac_address] = + std::move(bluetooth_device); + return mac_address_to_bluetooth_device_map_[mac_address].get(); + } else { + cached_bluetooth_devices_map_[mac_address] = std::move(bluetooth_device); + return cached_bluetooth_devices_map_[mac_address].get(); + } } LOG(INFO) << __func__ << ": Bluetooth device " << mac_address @@ -511,6 +516,15 @@ bool BluetoothClassicMedium::StartScanning() { mac_address_to_bluetooth_device_map_.clear(); removed_bluetooth_devices_map_.clear(); + if (!cached_bluetooth_devices_map_.empty()) { + for (auto& [mac_address, bluetooth_device] : + cached_bluetooth_devices_map_) { + mac_address_to_bluetooth_device_map_[mac_address] = + std::move(bluetooth_device); + } + + cached_bluetooth_devices_map_.clear(); + } // The Start method can only be called when the DeviceWatcher is in the // Created, Stopped or Aborted state. diff --git a/internal/platform/implementation/windows/bluetooth_classic_medium.h b/internal/platform/implementation/windows/bluetooth_classic_medium.h index d90e071a..f12ec2cd 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_medium.h +++ b/internal/platform/implementation/windows/bluetooth_classic_medium.h @@ -159,6 +159,10 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium { absl::flat_hash_map> removed_bluetooth_devices_map_; + // The caller may call to create Bluetooth device when scanning is off. + absl::flat_hash_map> + cached_bluetooth_devices_map_; + BluetoothAdapter& bluetooth_adapter_; BluetoothAdapter::ScanMode scan_mode_ = BluetoothAdapter::ScanMode::kUnknown;