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
This commit is contained in:
hai007
2022-06-08 11:39:32 -07:00
committed by Copybara-Service
parent 9069451c08
commit 1aeb1093a9
2 changed files with 35 additions and 5 deletions
@@ -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<ConnectionProfile> hotspot_profiles_ ABSL_GUARDED_BY(mutex_);
// Keep the server socket listener pointer
WifiHotspotServerSocket* server_socket_ptr_ ABSL_GUARDED_BY(mutex_) = nullptr;
};
@@ -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<int>(profile.TryDeleteAsync().get());
}
hotspot_profiles_.clear();
}
if (!IsConnected()) {
NEARBY_LOGS(WARNING)
<< "Cannot diconnect SoftAP because it is not connected.";