WiFiAdapter is a new class available in Windows 10 API which is used to control the WiFi connection with in the Universal Windows App. The user can get the available WiFi Adapter in the device and then using the WiFi Adapter the list of all available WiFi networks can be scanned. Once the list of WiFi networks available are identified and the user can connect or disconnect to those WiFi networks. The big advantage of this class is with in the Universal Windows App the user can control the WiFi Connections of the device. It is available in Windows.Devices.WiFi namespace.
Download the Source code from GitHub : https://github.com/ArunSuryaPrakash/WiFiScan
In this sample I have created a simple Universal Windows App which displays scan and disconnect buttons. When you clicks on the scan button it provides the list of all WiFi network available and he can select the WiFi network and click Connect to get connected.
Lets see how to connect to the WiFi Network from the below steps.
Step 1: Check for the Request has been given to interact with the device by using the static method RequestAccessAsync, checking this request is important before invoking any API related to the WiFiAdapter.
var accessAllowed = WiFiAdapter.RequestAccessAsync();
Step 2: If the access is denied then provide the device capabilities in the Apps package.appxmanifest file.
<DeviceCapability Name="wiFiControl" />
Step 3: If the access is allowed then get the list of device selectors using the static method GetDeviceSelector. If the results are received then provide the received value to the FromIdAsync static method to create the object for the WiFiAdapter class.
var result = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(WiFiAdapter.GetDeviceSelector()); wifiAdapter = await WiFiAdapter.FromIdAsync(result[0].Id);
Step 4: When the user clicks on the Scan button, the WiFi networks can be scanned using ScanAsync method. Once it is completed then the user can get the list of WiFi networks available.
await wifiAdapter.ScanAsync(); var wifiList = new List<string>; foreach(var network in networkReport.AvailableNetworks) { wifiList.Add(network.Ssid); } WiFiList.ItemsSource = wifiList;
Step 5: when the user clicks on the connect button, connect the selected network using ConnectAsync method. This method takes 2 parameters by default, one is WiFiAvailableNetwork and the other one is WiFiReconnectionKind. If the WiFi is secured then the third parameter is used to provide the PasswordCredential. The developer can check what type of connection is it weather it is secured or open using the NetworkAuthenticationType .
WiFiReconnectionKind reconnectionKind = WiFiReconnectionKind.Manual; if (ConnectAutomatically.IsChecked.HasValue && ConnectAutomatically.IsChecked.Value) { reconnectionKind = WiFiReconnectionKind.Automatic; } if (availableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Open80211) { await wifiAdapter.ConnectAsync(availableNetwork, reconnectionKind); } else { var credential = new PasswordCredential(); if (!string.IsNullOrEmpty(securityKey.Password)) { credential.Password = securityKey.Password; } await wifiAdapter.ConnectAsync(availableNetwork, reconnectionKind, credential); }
Step 6: When the user clicks on the Disconnect button then the connected WiFi connection should be disconnected using the Disconnect method.
wifiAdapter.Disconnect();
Step 7: Run the App.
That’s it, now the user can connect and disconnect the WiFi networks from the App itself.
Hope this post might have helped in understanding the WiFiAdpater class.
Pingback: Visual Studio – Developer Top Ten for Oct 8th, 2015 - Dmitry Lyalin
Pingback: Szumma #010 – 2015 40. hét | d/fuel