From 694a722d2c6f0c1b3bf4e83492bba63fb6947b7b Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Fri, 10 Jan 2025 12:05:57 -0800 Subject: [PATCH] Keep existing Wi-Fi hotspot device when having new connection comes in PiperOrigin-RevId: 714130787 --- .../implementation/windows/wifi_hotspot.h | 4 +++- .../windows/wifi_hotspot_medium.cc | 22 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/internal/platform/implementation/windows/wifi_hotspot.h b/internal/platform/implementation/windows/wifi_hotspot.h index cb4196f6..5202d4ad 100644 --- a/internal/platform/implementation/windows/wifi_hotspot.h +++ b/internal/platform/implementation/windows/wifi_hotspot.h @@ -329,7 +329,9 @@ class WifiHotspotMedium : public api::WifiHotspotMedium { WiFiDirectAdvertisementPublisher publisher_{nullptr}; WiFiDirectConnectionListener listener_{nullptr}; - WiFiDirectDevice wifi_direct_device_{nullptr}; + + // The list of WiFiDirectDevice is used to keep hotspot connection alive. + std::list wifi_direct_devices_; fire_and_forget OnStatusChanged( WiFiDirectAdvertisementPublisher sender, diff --git a/internal/platform/implementation/windows/wifi_hotspot_medium.cc b/internal/platform/implementation/windows/wifi_hotspot_medium.cc index c81453db..5b75f16c 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_medium.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_medium.cc @@ -322,7 +322,7 @@ bool WifiHotspotMedium::StopWifiHotspot() { publisher_.Stop(); listener_.ConnectionRequested(connection_requested_token_); publisher_.StatusChanged(publisher_status_changed_token_); - wifi_direct_device_ = nullptr; + wifi_direct_devices_.clear(); listener_ = nullptr; publisher_ = nullptr; LOG(INFO) << "succeeded to stop WiFi Hotspot"; @@ -377,7 +377,7 @@ fire_and_forget WifiHotspotMedium::OnStatusChanged( LOG(ERROR) << "Windows WiFi Hotspot cleanup."; listener_.ConnectionRequested(connection_requested_token_); publisher_.StatusChanged(publisher_status_changed_token_); - wifi_direct_device_ = nullptr; + wifi_direct_devices_.clear(); listener_ = nullptr; publisher_ = nullptr; } @@ -402,13 +402,19 @@ fire_and_forget WifiHotspotMedium::OnConnectionRequested( // solve the problem. Guess when this object is created, // [Microsoft-Windows-WLAN-AutoConfig] will recognise it as a valid device // and won't kick it away. - wifi_direct_device_ = WiFiDirectDevice::FromIdAsync( - connection_request.DeviceInformation().Id()) - .get(); - LOG(INFO) << "Registered the device in WLAN-AutoConfig"; + // We found new connection request comes in during horspot transfer. In this + // case, we should create a new WiFiDirectDevice for it. It will cause + // transfer failure if replace the old WiFiDirectDevice with it. + auto wifi_direct_device = + WiFiDirectDevice::FromIdAsync( + connection_request.DeviceInformation().Id()) + .get(); + wifi_direct_devices_.push_back(wifi_direct_device); + LOG(INFO) << "Registered the device " << winrt::to_string(device_name) + << " in WLAN-AutoConfig"; } catch (...) { - LOG(ERROR) << "Failed to registered the device in WLAN-AutoConfig"; - wifi_direct_device_ = nullptr; + LOG(ERROR) << "Failed to registered the device " + << winrt::to_string(device_name) << " in WLAN-AutoConfig"; connection_request.Close(); } return winrt::fire_and_forget();