From dba1deab3c597c2a484f7a6eb7cb1973baf5cbe5 Mon Sep 17 00:00:00 2001 From: hai007 Date: Thu, 10 Nov 2022 10:57:45 -0800 Subject: [PATCH] Check if the stored WIFI network is exist before restore WIFI connection PiperOrigin-RevId: 487585021 --- .../windows/wifi_hotspot_medium.cc | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/internal/platform/implementation/windows/wifi_hotspot_medium.cc b/internal/platform/implementation/windows/wifi_hotspot_medium.cc index 905d2f1a..56833d78 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_medium.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_medium.cc @@ -379,6 +379,10 @@ bool WifiHotspotMedium::ConnectWifiHotspot( } void WifiHotspotMedium::RestoreWifiConnection() { + if (!wifi_connected_network_ && wifi_adapter_) { + wifi_adapter_.Disconnect(); + return; + } if (wifi_adapter_) { ConnectionProfile profile = wifi_adapter_.NetworkAdapter().GetConnectedProfileAsync().get(); @@ -399,23 +403,20 @@ void WifiHotspotMedium::RestoreWifiConnection() { wifi_adapter_.Disconnect(); NEARBY_LOGS(INFO) << "Disconnected to current network."; - if (wifi_connected_network_) { - auto connect_result = wifi_adapter_ - .ConnectAsync(wifi_connected_network_, - WiFiReconnectionKind::Automatic) - .get(); + auto connect_result = wifi_adapter_ + .ConnectAsync(wifi_connected_network_, + WiFiReconnectionKind::Automatic) + .get(); - if (connect_result == nullptr || - connect_result.ConnectionStatus() != WiFiConnectionStatus::Success) { - NEARBY_LOGS(INFO) - << "Connecting to previous network failed with reason: " - << static_cast(connect_result.ConnectionStatus()); - } else { - NEARBY_LOGS(INFO) << "Restored the previous WIFI connection: " - << winrt::to_string(wifi_connected_network_.Ssid()); - } - wifi_connected_network_ = nullptr; + if (connect_result == nullptr || + connect_result.ConnectionStatus() != WiFiConnectionStatus::Success) { + NEARBY_LOGS(INFO) << "Connecting to previous network failed with reason: " + << static_cast(connect_result.ConnectionStatus()); + } else { + NEARBY_LOGS(INFO) << "Restored the previous WIFI connection: " + << winrt::to_string(wifi_connected_network_.Ssid()); } + wifi_connected_network_ = nullptr; } }