/// <summary> Get existing matching network </summary> private static WifiConfiguration GetConfiguredNetwork(WifiManager wifiManager, Ssid ssid) { var existingNetwork = wifiManager.ConfiguredNetworks .FirstOrDefault(x => x.Ssid == ssid.Quoted); Logger.Verbose(existingNetwork != null ? $"Found existing network {ssid}" : $"Existing network {ssid} not found"); if (existingNetwork == null) throw new InvalidOperationException($"Network {ssid} not configured"); return existingNetwork; }
public static async Task<ConnectionResult> ConnectAsync(Context context, Ssid ssid, TimeSpan checkInterval, TimeSpan timeout) { Logger.Verbose("WifiConnector.Connecting"); var wifiManager = context.GetSystemService(Context.WifiService).JavaCast<WifiManager>(); EnsureWifiEnabled(wifiManager); if (IsConnectedToNetwork(wifiManager, ssid)) { Logger.Info($"Network {ssid} already connected"); return ConnectionResult.AlreadyConnected; } var network = GetConfiguredNetwork(wifiManager, ssid); EnsureDifferentNetworksNotActive(wifiManager, network); EnsureNetworkReachable(wifiManager, ssid); ActivateNetwork(wifiManager, network); Reconnect(wifiManager); Logger.Verbose($"Connection to network {ssid} requested"); var result = await WaitUntilConnectedAsync(wifiManager, network, checkInterval, timeout); Logger.Info(result == ConnectionResult.Connected ? $"Connected to {ssid.Quoted}" : $"Not yet connected to {ssid.Quoted}. Try increase a connection timeout"); return result; }
private async void OnButtonClick(object sender, EventArgs e) { if (_button.Text == GetString(Resource.String.Settings)) StartActivity(typeof(SettingsActivity)); else { if (_isBusy) { ShowToast(GetString(Resource.String.Busy)); return; } try { _isBusy = true; _button.Text = GetString(Resource.String.Busy); Logger.Verbose("Connecting"); var settings = _settings; var ssid = new Ssid(settings.ConnectionSettings.Ssid); await WifiConnector.ConnectAsync(this, ssid, TimeSpan.FromMilliseconds(settings.OtherSettings.ConnectCheckInterval), TimeSpan.FromMilliseconds(settings.OtherSettings.ConnectTimeout)); await CiscoWebAuthManager.LoginAsync(settings.LoginPageSettings, settings.AuthSettings); if (settings.OtherSettings.ToastLevel != ToastLevel.None) ShowToast("Done"); OpenWebBrowserIfRequired(settings); } catch (Exception ex) { Logger.Error(ex.Message); } finally { _isBusy = false; UpdateActivityState(); } } }
private static void EnsureNetworkReachable(WifiManager wifiManager, Ssid ssid) { if (wifiManager.ScanResults.All(x => x.Ssid != ssid.Original)) throw new InvalidOperationException($"Network {ssid} is unreachable"); }
private static bool IsConnectedToNetwork(WifiManager wifiManager, Ssid ssid) { return wifiManager.IsWifiEnabled && wifiManager.ConnectionInfo != null && wifiManager.ConnectionInfo.SSID == ssid.Quoted; }