From 1aeb1093a9864c73394d27598684f2e9287d6e4e Mon Sep 17 00:00:00 2001 From: hai007 Date: Wed, 8 Jun 2022 11:37:28 -0700 Subject: [PATCH] Delete temporary Hotspot profiles after the session is done. Reduce max rescan from 3 to 2 times. Experiment shows if second time scan fail, it always fail to find the Hotspot. PiperOrigin-RevId: 453729656 --- .../implementation/windows/wifi_hotspot.h | 4 +++ .../windows/wifi_hotspot_medium.cc | 36 ++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/internal/platform/implementation/windows/wifi_hotspot.h b/internal/platform/implementation/windows/wifi_hotspot.h index d9c9f778..69ea6921 100644 --- a/internal/platform/implementation/windows/wifi_hotspot.h +++ b/internal/platform/implementation/windows/wifi_hotspot.h @@ -72,6 +72,7 @@ using ::winrt::Windows::Security::Cryptography::CryptographicBuffer; using ::winrt::Windows::Networking::HostName; using ::winrt::Windows::Networking::HostNameType; using ::winrt::Windows::Networking::Connectivity::NetworkInformation; +using ::winrt::Windows::Networking::Connectivity::ConnectionProfile; using ::winrt::Windows::Networking::Sockets::StreamSocket; using ::winrt::Windows::Networking::Sockets::StreamSocketListener; using ::winrt::Windows::Networking::Sockets:: @@ -285,6 +286,9 @@ class WifiHotspotMedium : public api::WifiHotspotMedium { // Medium Status int medium_status_ = kMediumStatusIdle; + // Hotspot profiles that Discoverer has connected in the whole session; + std::vector hotspot_profiles_ ABSL_GUARDED_BY(mutex_); + // Keep the server socket listener pointer WifiHotspotServerSocket* server_socket_ptr_ ABSL_GUARDED_BY(mutex_) = nullptr; }; diff --git a/internal/platform/implementation/windows/wifi_hotspot_medium.cc b/internal/platform/implementation/windows/wifi_hotspot_medium.cc index 3dbeadee..bdf393e4 100644 --- a/internal/platform/implementation/windows/wifi_hotspot_medium.cc +++ b/internal/platform/implementation/windows/wifi_hotspot_medium.cc @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "absl/strings/string_view.h" #include "internal/platform/implementation/windows/wifi_hotspot.h" // Nearby connections headers @@ -26,7 +27,7 @@ namespace windows { namespace { constexpr int kMaxRetries = 3; constexpr int kRetryIntervalMilliSeconds = 300; - constexpr int kMaxScans = 3; + constexpr int kMaxScans = 2; } // namespace WifiHotspotMedium::WifiHotspotMedium() { @@ -280,8 +281,8 @@ bool WifiHotspotMedium::ConnectWifiHotspot( NEARBY_LOGS(INFO) << "Scanning for Nearby Hotspot SSID: " << hotspot_credentials_->GetSSID(); - // First time scan may not find our target hotspot, try 3 times can almost - // guarantee to find the Hotspot + // First time scan may not find our target hotspot, try 2 more times can + // almost guarantee to find the Hotspot for (int i = 0; i < kMaxScans; i++) { for (const auto& network : wifi_adapter_.NetworkReport().AvailableNetworks()) { @@ -307,7 +308,7 @@ bool WifiHotspotMedium::ConnectWifiHotspot( auto connect_result = wifi_adapter_ - .ConnectAsync(nearby_softap, WiFiReconnectionKind::Automatic, creds) + .ConnectAsync(nearby_softap, WiFiReconnectionKind::Manual, creds) .get(); if (connect_result.ConnectionStatus() != WiFiConnectionStatus::Success) { @@ -316,14 +317,39 @@ bool WifiHotspotMedium::ConnectWifiHotspot( return false; } - NEARBY_LOGS(INFO) << "Connected to: " << hotspot_credentials_->GetSSID(); + std::string last_ssid = hotspot_credentials_->GetSSID(); + NEARBY_LOGS(INFO) << "Connected to: " << last_ssid; medium_status_ |= kMediumStatusConnected; + + Sleep(50); + auto profile = + wifi_adapter_.NetworkAdapter().GetConnectedProfileAsync().get(); + if (profile.IsWlanConnectionProfile()) { + if (winrt::to_string( + profile.WlanConnectionProfileDetails().GetConnectedSsid()) == + last_ssid) { + hotspot_profiles_.push_back(profile); + NEARBY_LOGS(INFO) << "Save WiFi profile with SSID: " << last_ssid; + } + } + return true; } bool WifiHotspotMedium::DisconnectWifiHotspot() { absl::MutexLock lock(&mutex_); + if (!hotspot_profiles_.empty()) { + for (auto profile : hotspot_profiles_) { + NEARBY_LOGS(INFO) + << "Delete WiFi profile with SSID: " + << winrt::to_string( + profile.WlanConnectionProfileDetails().GetConnectedSsid()) + << ", result: " << static_cast(profile.TryDeleteAsync().get()); + } + hotspot_profiles_.clear(); + } + if (!IsConnected()) { NEARBY_LOGS(WARNING) << "Cannot diconnect SoftAP because it is not connected.";