// Call this method before accessing WiFiAdapters Dictionary private async Task UpdateAdapters() { Log.Enter(); bool fInit = false; foreach (var adapter in WiFiAdapters) { if (adapter.Value == null) { // New Adapter plugged-in which requires Initialization fInit = true; } } if (fInit) { List <String> WiFiAdaptersID = new List <string>(WiFiAdapters.Keys); for (int i = 0; i < WiFiAdaptersID.Count; i++) { string id = WiFiAdaptersID[i]; try { WiFiAdapters[id] = await WiFiAdapter.FromIdAsync(id); } catch (Exception) { WiFiAdapters.Remove(id); } } } Log.Leave(); }
private async void btnConnect_Click(object sender, RoutedEventArgs e) { var access = await WiFiAdapter.RequestAccessAsync(); var wifiAdapterResults = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); WiFiAdapter wa = await WiFiAdapter.FromIdAsync(wifiAdapterResults[0].Id); WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Automatic; WiFiConnectionResult result; foreach (var availableNetwork in _report.AvailableNetworks) { if (availableNetwork.Ssid == _selectedSSID) { if (txtPassword.Text.Length > 1) { var credential = new PasswordCredential(); credential.Password = txtPassword.Text; result = await wa.ConnectAsync(availableNetwork, reconnectionKind, credential); } else { result = await wa.ConnectAsync(availableNetwork, reconnectionKind); } } } }
public async Task <IEnumerable <WifiNetwork> > GetWifiAsync() { _wifis = new List <WifiNetwork>(); _wifisOriginals = new List <WiFiNetworkDisplay>(); var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { //rootPage.NotifyUser("Access denied", NotifyType.ErrorMessage); } else { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { _firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); await ScanAsync(); return(_wifis.OrderByDescending(w => w.SignalBars)); } else { //No WiFi Adapters detected on this machine } } return(null); }
private async void MainPage_Loaded(object sender, RoutedEventArgs e) { Loaded -= MainPage_Loaded; Displays.Clear(); var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { Allowed = false; Toast.ShowError("不允许连接wifi,请确认已开启wifi"); } else { var result = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); await UpdateConnectivityStatusAsync(); NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged; OnRefreshRequested(null, null); } Allowed = true; } }
public async void Scan() { var access = await WiFiAdapter.RequestAccessAsync(); var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { nwAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); await nwAdapter.ScanAsync(); sp.Children.Clear(); foreach (var item in nwAdapter.NetworkReport.AvailableNetworks) { StackPanel s = new StackPanel(); s.Orientation = Orientation.Horizontal; Button b = new Button(); TextBlock t = new TextBlock(); t.Text = $"{item.NetworkRssiInDecibelMilliwatts} {item.ChannelCenterFrequencyInKilohertz} {item.IsWiFiDirect} {item.NetworkKind} {item.SecuritySettings.NetworkAuthenticationType} {item.SecuritySettings.NetworkAuthenticationType} {item.SignalBars} {item.Uptime}"; b.Content += item.Ssid; b.Click += B_Click; s.Children.Add(b); s.Children.Add(t); sp.Children.Add(s); } } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { rootPage = MainPage.Current; // RequestAccessAsync must have been called at least once by the app before using the API // Calling it multiple times is fine but not necessary // RequestAccessAsync must be called from the UI thread var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { ScenarioOutput.Text = "Access denied"; } else { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); RegisterButton.IsEnabled = true; } else { ScenarioOutput.Text = "No WiFi Adapters detected on this machine"; } } }
private async Task <bool> _getNetworks() { WiFiAccessStatus access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { _done = true; return(false); } else { DeviceInformationCollection adapterResults = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (adapterResults.Count() >= 1) { _WiFiAdapter = await WiFiAdapter.FromIdAsync(adapterResults[0].Id); } else { _done = true; return(false); } } await _WiFiAdapter.ScanAsync(); foreach (WiFiAvailableNetwork availableNetwork in _WiFiAdapter.NetworkReport.AvailableNetworks) { _WiFiNetworks.Add(availableNetwork.Ssid); } return(true); }
public static IAsyncAction WaitForWiFiConnection() { return(AsyncInfo.Run(async(cancel) => { if (!await EnableWiFi()) { return; } var accessAllowed = await WiFiAdapter.RequestAccessAsync(); if (accessAllowed == WiFiAccessStatus.Allowed) { var adapterList = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); var wifiAdapter = await WiFiAdapter.FromIdAsync(adapterList[0].Id); for (var i = 0; i < 5; i++) { cancel.ThrowIfCancellationRequested(); await wifiAdapter.ScanAsync(); await Task.Delay(100); if (await wifiAdapter.NetworkAdapter.GetConnectedProfileAsync() != null) { break; } } } })); }
protected override async void OnNavigatedTo(NavigationEventArgs e) { ResultCollection = new ObservableCollection <WiFiNetworkDisplay>(); rootPage = MainPage.Current; // RequestAccessAsync must have been called at least once by the app before using the API // Calling it multiple times is fine but not necessary // RequestAccessAsync must be called from the UI thread var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { rootPage.NotifyUser("Access denied", NotifyType.ErrorMessage); } else { DataContext = this; var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); var button = new Button(); button.Content = string.Format("Scan"); button.Click += Button_Click; Buttons.Children.Add(button); } else { rootPage.NotifyUser("No WiFi Adapters detected on this machine", NotifyType.ErrorMessage); } } }
private async Task <List <string> > GetWirelessSuits() { var access = await WiFiAdapter.RequestAccessAsync(); var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { var nwAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); await nwAdapter.ScanAsync(); //because spelling errors var nw = nwAdapter.NetworkReport.AvailableNetworks.Where(network => network.Ssid.Contains("NSVR") || network.Ssid.Contains("NVSR:")); return(nw.Select(suit => suit.Ssid).ToList()); //if (nw.Count() > 0) //{ // var suitAP = nw.First(); // var pass = SsidToPassword(suitAP.Ssid); // var conn = await nwAdapter.ConnectAsync(suitAP, WiFiReconnectionKind.Automatic, new Windows.Security.Credentials.PasswordCredential("none", "none", pass)); // return new Tuple<WiFiConnectionStatus, string>(conn.ConnectionStatus, pass); //} } return(new List <string>()); }
private async Task UpdateAdapters() { bool fInit = false; foreach (var adapter in _wifiAdapters) { if (adapter.Value == null) { fInit = true; } } if (fInit) { List <String> WiFiAdaptersID = new List <string>(_wifiAdapters.Keys); for (int i = 0; i < WiFiAdaptersID.Count; i++) { string id = WiFiAdaptersID[i]; try { _wifiAdapters[id] = await WiFiAdapter.FromIdAsync(id); } catch (Exception) { _wifiAdapters.Remove(id); } } } }
private async Task <Tuple <Windows.Devices.WiFi.WiFiConnectionStatus, string> > ConnectWifiAndGetPassword(string ssid) { var access = await WiFiAdapter.RequestAccessAsync(); var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { var nwAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); await nwAdapter.ScanAsync(); //because spelling errors var nw = nwAdapter.NetworkReport.AvailableNetworks.Where(network => network.Ssid.Contains(ssid)); if (nw.Count() > 0) { var suitAP = nw.First(); var pass = SsidToPassword(suitAP.Ssid); var conn = await nwAdapter.ConnectAsync(suitAP, WiFiReconnectionKind.Automatic, new Windows.Security.Credentials.PasswordCredential("none", "none", pass)); return(new Tuple <WiFiConnectionStatus, string>(conn.ConnectionStatus, pass)); } } return(new Tuple <WiFiConnectionStatus, string>(WiFiConnectionStatus.NetworkNotAvailable, "")); }
protected override async void OnNavigatedTo(NavigationEventArgs e) { // RequestAccessAsync must have been called at least once by the app before using the API // Calling it multiple times is fine but not necessary // RequestAccessAsync must be called from the UI thread var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { ScenarioOutput.Text = "Access denied"; } else { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); var button = new Button(); button.Content = string.Format("Scan"); button.Click += Button_Click; Buttons.Children.Add(button); } else { ScenarioOutput.Text = "No WiFi Adapters detected on this machine"; } } }
/// <summary> /// Инициализация адаптера /// </summary> /// <returns></returns> private async Task <bool> InitializeFirstAdapter() { return(await Task.Run(async() => { if (_wifiAdapter == null) { bool res = false; WiFiAccessStatus access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { throw new Exception("WiFiAccessStatus not allowed"); } else { var wifiAdapterResults = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (wifiAdapterResults.Count >= 1) { _wifiAdapter = await WiFiAdapter.FromIdAsync(wifiAdapterResults[0].Id); res = true; } else { throw new Exception("WiFi Adapter not found."); } } return res; } else { return true; } })); }
public async Task <WiFiAdapter> GetAdapter(string id) { if (id == null) { return(null); } DeviceId = id; return(await WiFiAdapter.FromIdAsync(id)); }
async void _watcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args) { WiFiAdapter device = await WiFiAdapter.FromIdAsync(args.Id); if (device != null) { // remove the device from the list lock (this) { _deviceList.Remove(device); } } }
private async Task <WiFiAdapter> GetWifiAdapter() { if (_wifiAdapter != null) { return(_wifiAdapter); } //var accessStatus = await WiFiAdapter.RequestAccessAsync(); var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); _wifiAdapter = await WiFiAdapter.FromIdAsync(devices[0].Id); return(_wifiAdapter); }
public async Task <bool> Initialise() { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } else { return(false); } return(true); }
/// <summary> /// Gets connection ssid for the current Wifi Connection. /// </summary> /// <returns> string value of current ssid/></returns> /// public static async Task <string> GetNetwoksSSid() { try { WiFiAdapter firstAdapter; var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { return("Acess Denied"); } else { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } else { return("No WiFi Adapters Detected"); } var connectedProfile = await firstAdapter.NetworkAdapter.GetConnectedProfileAsync(); if (connectedProfile != null) { if (connectedProfile.ProfileName.ToLower().Equals("vit2.4g") || connectedProfile.ProfileName.ToLower().Equals("vit5g")) { return("OK"); } else { return(connectedProfile.ProfileName); } } else if (connectedProfile == null) { return("WiFi adapter disconnected"); } } } catch { } return(null); }
public async Task <WiFiScanResultTypes> LoadData() { Enabled_btnRefresh = false; ShowRunning(); WifiNetworks = new ObservableCollection <WiFiAvailableNetwork>(); var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { Enabled_btnRefresh = true; HideRunning(); return(WiFiScanResultTypes.NO_ACCESS_TO_WIFI_CARD); } var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count > 0) { PrimaryAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } else { Enabled_btnRefresh = true; HideRunning(); return(WiFiScanResultTypes.NO_WIFI_CARD); } await PrimaryAdapter.ScanAsync(); foreach (var network in PrimaryAdapter.NetworkReport.AvailableNetworks) { WifiNetworks.Add(network); } WifiNetworks = new ObservableCollection <WiFiAvailableNetwork>(WifiNetworks.OrderByDescending(a => a.SignalBars)); Enabled_btnRefresh = true; HideRunning(); return(WiFiScanResultTypes.SUCCESS); }
/// <summary> /// 异步初始化WiFi适配器 /// </summary> /// <returns>成功与否</returns> private async Task <bool> InitializeWiFiAdapterAsync() { var WiFiAdapterResults = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (WiFiAdapterResults.Count >= 1) { WiFi = await WiFiAdapter.FromIdAsync(WiFiAdapterResults.FirstOrDefault().Id); WiFi.AvailableNetworksChanged += WiFi_AvailableNetworksChanged; } else { return(false); } return(true); }
protected override async void OnNavigatedTo(NavigationEventArgs e) { var access = await WiFiAdapter.RequestAccessAsync(); if (access == WiFiAccessStatus.Allowed) { DataContext = this; var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { wifiAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } } }
public IAsyncOperation <OnboardingConnectResult> ConnectAsync(AllJoynMessageInfo info) { return(Task.Run(async() => { // Find the network with the specified Ssid var adapter = await WiFiAdapter.FromIdAsync(_wlanAdapterId); foreach (var network in adapter.NetworkReport.AvailableNetworks) { if (network.Ssid == _personalApSsid) { _softAccessPoint?.Stop(); this.ConnectToNetwork(adapter, network); } } return OnboardingConnectResult.CreateSuccessResult(); }).AsAsyncOperation()); }
private async void HandleAdapterAdded(DeviceWatcher sender, DeviceInformation information) { if (_wlanAdapterId == null) { _wlanAdapterId = information.Id; _wlanAdapter = await WiFiAdapter.FromIdAsync(_wlanAdapterId); lock (_stateLock) { if (_state != OnboardingState.ConfiguredValidated) { _softAccessPoint.Start(); } } _onboardingProducer.Start(); _iconProducer.Start(); } }
private async void AdaptersEnumCompleted(DeviceWatcher sender, object args) { List <String> WiFiAdaptersID = new List <string>(WiFiAdapters.Keys); for (int i = 0; i < WiFiAdaptersID.Count; i++) { string id = WiFiAdaptersID[i]; try { WiFiAdapters[id] = await WiFiAdapter.FromIdAsync(id); } catch (Exception) { WiFiAdapters.Remove(id); } } EnumAdaptersCompleted.Set(); }
/// <summary> /// Gets connection level for the current Wifi Connection. /// </summary> /// <returns> string value of level/></returns> /// public static async Task <NetworkConnectivityLevel> GetNetworkLevel() { try { WiFiAdapter firstAdapter; var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { return(0); } else { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } else { return(0); } var connectedProfile = await firstAdapter.NetworkAdapter.GetConnectedProfileAsync(); if (connectedProfile != null) { var x = connectedProfile.GetNetworkConnectivityLevel(); return(x); } else { return(0); } } } catch { return(0); } // return 0; }
public IAsyncOperation <OnboardingGetScanInfoResult> GetScanInfoAsync(AllJoynMessageInfo info) { return(Task.Run(async() => { var adapter = await WiFiAdapter.FromIdAsync(_wlanAdapterId); await adapter.ScanAsync(); var availableNetworks = new List <OnboardingScanListItem>(); foreach (var network in adapter.NetworkReport.AvailableNetworks) { var listItem = new OnboardingScanListItem { Value1 = network.Ssid }; switch (network.SecuritySettings.NetworkAuthenticationType) { case NetworkAuthenticationType.Open80211: { listItem.Value2 = (short)AuthType.Open; break; } case NetworkAuthenticationType.Wpa: { listItem.Value2 = (short)AuthType.WPA_AUTO; break; } default: { listItem.Value2 = (short)AuthType.Any; break; } } if (availableNetworks.Find(x => x.Value1 == listItem.Value1 && x.Value2 == listItem.Value2) == null) { availableNetworks.Add(listItem); } } return OnboardingGetScanInfoResult.CreateSuccessResult(0, availableNetworks); }).AsAsyncOperation()); }
private static async Task <WiFiAdapter> FetchWiFiAdapter() { WiFiAdapter wifiAdapter = null; var result = await DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { try { wifiAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } catch { } } return(wifiAdapter); }
private async void UpdateViewData() { var accessAllowed = await WiFiAdapter.RequestAccessAsync(); if (accessAllowed == WiFiAccessStatus.Allowed) { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { _wifiAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } string blah = GetCurrentWifiNetwork(); GetCurrentWifiNetwork(); ScanAvailableNetworks(); } }
//Here savedProfileName will have network ssid its connected. //Also connectedProfile.IsWlanConnectionProfile will be true if connected over wifi //connectedProfile.IsWwanConnectionProfile will be true if connected over cellular private async Task GetNet() { var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); if (firstAdapter.NetworkAdapter.GetConnectedProfileAsync() != null) { connectedProfile = await firstAdapter.NetworkAdapter.GetConnectedProfileAsync(); if (connectedProfile != null) //&& !connectedProfile.ProfileName.Equals(savedProfileName) { savedProfileName = connectedProfile.ProfileName; if (connectedProfile.IsWlanConnectionProfile) { nettype = $"Wifi \"{savedProfileName}\""; } if (connectedProfile.IsWwanConnectionProfile) { nettype = "cellular"; } Logger.LogInformation($"Connected to internet: {nettype}. Battery: {GetBattery()}%"); } else { Logger.LogInformation($"No connectedProfile"); } } else { Logger.LogInformation($"No network-profil"); } } else { Logger.LogInformation($"No network-devices"); } }