private async Task InitializeNetworksAsync(WiFiNetworkReport networkReport) { WiFiNetworks.Clear(); foreach (var nw in networkReport.AvailableNetworks) { var item = new WiFiNetworkViewModel(nw, _wifiAdapter); await item.UpdateAsync(); if (IsConnected(nw)) { WiFiNetworks.Insert(0, item); SelectedWiFiNetwork = WiFiNetworks[0]; OnSelect?.Invoke(this, EventArgs.Empty); } else { WiFiNetworks.Add(item); } } }
public async Task ConnectAsync() { if (SelectedWiFiNetwork == null) { OnError?.Invoke(this, new ArgumentException("Network not selected")); return; } WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Manual; if (SelectedWiFiNetwork.ConnectAutomatically) { reconnectionKind = WiFiReconnectionKind.Automatic; } Task <WiFiConnectionResult> didConnect = null; WiFiConnectionResult result = null; if (SelectedWiFiNetwork.IsEapAvailable) { if (SelectedWiFiNetwork.UsePassword) { var credential = new PasswordCredential(); if (!String.IsNullOrEmpty(SelectedWiFiNetwork.Domain)) { credential.Resource = SelectedWiFiNetwork.Domain; } credential.UserName = SelectedWiFiNetwork.UserName ?? ""; credential.Password = SelectedWiFiNetwork.Password ?? ""; didConnect = _wifiAdapter.ConnectAsync(SelectedWiFiNetwork.AvailableNetwork, reconnectionKind, credential).AsTask(); } else { didConnect = _wifiAdapter.ConnectAsync(SelectedWiFiNetwork.AvailableNetwork, reconnectionKind).AsTask(); } } else if (SelectedWiFiNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Open80211 && SelectedWiFiNetwork.AvailableNetwork.SecuritySettings.NetworkEncryptionType == NetworkEncryptionType.None) { didConnect = _wifiAdapter.ConnectAsync(SelectedWiFiNetwork.AvailableNetwork, reconnectionKind).AsTask(); } else { // Only the password potion of the credential need to be supplied if (String.IsNullOrEmpty(SelectedWiFiNetwork.Password)) { didConnect = _wifiAdapter.ConnectAsync(SelectedWiFiNetwork.AvailableNetwork, reconnectionKind).AsTask(); } else { var credential = new PasswordCredential(); credential.Password = SelectedWiFiNetwork.Password ?? ""; didConnect = _wifiAdapter.ConnectAsync(SelectedWiFiNetwork.AvailableNetwork, reconnectionKind, credential).AsTask(); } } OnConnecting?.Invoke(this, EventArgs.Empty); if (didConnect != null) { result = await didConnect; } if (result != null && result.ConnectionStatus == WiFiConnectionStatus.Success) { WiFiNetworks.Remove(SelectedWiFiNetwork); WiFiNetworks.Insert(0, SelectedWiFiNetwork); OnSelect?.Invoke(this, EventArgs.Empty); OnConnected?.Invoke(this, EventArgs.Empty); } else { OnError?.Invoke(this, new Exception("Could not connect to network")); OnDisconnected?.Invoke(this, EventArgs.Empty); } // Since a connection attempt was made, update the connectivity level displayed for each foreach (var network in WiFiNetworks) { await network.UpdateConnectivityLevel(); } }