Wi-Fi Connector

We’ll learn how to find and connect to Wi-Fi networks using a Universal Windows Platform (UWP) app that makes use of the Windows.Devices.WiFi.WiFiAdapter API.

This is a headed sample. To better understand what headed mode is and how to configure your device to be headed, follow the instructions on the Headed and Headless mode site.

Load the project in Visual Studio

You can find the source code for this sample in our git repository. Clone the project to your machine and open the project from Visual Studio.

The code for the WiFi Connect sample can be found under: \WiFiConnect\CS\WiFiConnect

Connecting your WiFi adapter

You’ll need:

  • A Windows 10 IoT Core capable board with onboard WiFi, such as the Raspberry Pi 3 or DragonBoard 410c or
  • A USB WiFi adapter. For a list of supported Wi-Fi adapters see the Ecosystem Compatibility List

Deploy your app

If you’re building for Minnowboard Max, select x86 as the architecture. If you’re building for Raspberry Pi 2 or 3 or DragonBoard , select ARM.

Select Remote Machine to point to IoT device and hit F5 to deploy to your device. Go back to the basic ‘Hello World’ sample. if you need guidance

Test your app

The sample app when deployed displays a screen similar to the one below.

App Started

If you have a Wi-Fi adapter connected to your board, click on the “Scan Available WiFi Networks” button to start scanning and displaying the list of Wi-Fi networks in the vcinity.

Each WiFi network is identified by its SSID. Aditionally, some information about the SSID including the signal strength, channel frequency and authentication mode are displayed for each. The connection status is also shown for each.

WiFi SSID list

You can connect to the your SSID by clicking on the one you need. If a security key (password) is needed, a dialog will be displayed asking for it. Also, a checkbox asking if the WiFi service should attempt to reconnect to the particular SSID automatically such as at boot time.

WiFi SSID list

A message on the status bar will indicate if the connection has been successful, or if not the reason the connection could not be made.

Finally, because some networks may require additional information that can only be provided through a browser; e.g. Wi-Fi hot spots, the app will open a browser control to enable users to complete a connection as needed. Or, if Internet access is already available, the Bing.com page will be displayed.

WiFi SSID list

Let’s look at the code

The code for this sample makes use of the Windows.Devices.WiFi.WiFiAdapter API.

The WiFiAdapter class can be used to scan, find and connect to both open and secure WiFI networks by making use of the Wlan service running on Windows devices.

Required device capabiity

Accessing the Wi-Fi adapter from your code requires a device capability for WiFi devices added to the manifest. The wifiControl DeviceCapability is the one we need. So, we should add it to the AppX manifest, Package.appxmanifest file:

NOTE: While you can add other capabilities directly by double clicking and opening the Package.appxmanifest file in the UI editor, the wifiControl can only be added via the XML editor (Right Click on the file -> Open with -> XML (Text) Editor) and adding the device capability below:

    <Capability Name="internetClient" />
	<!-- Add the capability here -->
    <DeviceCapability Name="wifiControl" />

Scenario Code

The code of this sample is mostly contained within WiFiConnect_Scanrio.xaml and .cs files.

Requesting access

The first step to access the device is to request access using the static WiFiAdapter.RequestAccessAsync() method.

Note, in a headed app, the method must be called from the UI thread. However, in an IOT headless app (Background Application), which doesn’t have a UI thread, the method can be called from any thread.

When access is granted, any of the WiFiAdapter methods can now be used. So, we start by trying to find a Wi-Fi adapter on the current device, using the WiFiAdapter device selector.

var access = await WiFiAdapter.RequestAccessAsync();
if (access != WiFiAccessStatus.Allowed)
    rootPage.NotifyUser("Access denied", NotifyType.ErrorMessage);
    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 Available Wifi Networks");
        button.Click += Button_Click;
        rootPage.NotifyUser("No WiFi Adapters detected on this machine.", NotifyType.ErrorMessage);

Alternatively, WiFiAdapter.FindAllAdaptersAsync() can be used to find all WiFi adapters to achieve the same.

// Not part of the sample:
var result = await WiFiAdapter.FindAllAdaptersAsync();
if (result.Count >= 1)
    firstAdapter = result[0];
    // rest of the code

Scanning for Wi-Fi networks

The next step is to scan for available WiFi networks, this can be achieved using the WiFiAdapter.ScanAsync() method.

 private async void Button_Click(object sender, RoutedEventArgs e)
    await firstAdapter.ScanAsync();
    ConnectionBar.Visibility = Visibility.Collapsed;

When the scan is complete, the WiFiAdapter.NetworkReport property is updated which can then be used to get and display information to identify each of the wifi networks found using a read-only collection of WiFiAvailableNetworks. We add each to the result collection, so it can then be mapped to Xaml UI using properties of the WiFiNetworkDisplay helper class.

private void DisplayNetworkReport(WiFiNetworkReport report)
    rootPage.NotifyUser(string.Format("Network Report Timestamp: {0}", report.Timestamp), NotifyType.StatusMessage);


    foreach (var network in report.AvailableNetworks)
        ResultCollection.Add(new WiFiNetworkDisplay(network, firstAdapter));

Connecting to a Wi-Fi network

When a Wi-Fi network is selected from the ones displayed, we need to determine if we want to collect the password credential. The Wi-Fi network authentication type is what we need to determine that:

// Only show the password box if needed
if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == NetworkAuthenticationType.Open80211)
    NetworkKeyInfo.Visibility = Visibility.Collapsed;
    NetworkKeyInfo.Visibility = Visibility.Visible;

Finally, to connect to the selected network, an overload of WiFiAdapter.ConnectAsync() need to be used. The method’s overloads allow specifying the available network to connect to, password if needed and whehther or not to automatically reconnect to this network when in range.

The status returned in the async result indicates whether the connection was successful or. Only WiFiConnectionStatus.Success indicates success. Other returned values indicates the connection failure reason.

WiFiConnectionResult result;
if (selectedNetwork.AvailableNetwork.SecuritySettings.NetworkAuthenticationType == Windows.Networking.Connectivity.NetworkAuthenticationType.Open80211)
    result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind);
    // Only the password potion of the credential need to be supplied
    var credential = new PasswordCredential();
    credential.Password = NetworkKey.Password;

    result = await firstAdapter.ConnectAsync(selectedNetwork.AvailableNetwork, reconnectionKind, credential);

if (result.ConnectionStatus == WiFiConnectionStatus.Success)
    rootPage.NotifyUser(string.Format("Successfully connected to {0}.", selectedNetwork.Ssid), NotifyType.StatusMessage);

    // refresh the webpage
    webViewGrid.Visibility = Visibility.Visible;
    toggleBrowserButton.Content = "Hide Browser Control";
    refreshBrowserButton.Visibility = Visibility.Visible;
    rootPage.NotifyUser(string.Format("Could not connect to {0}. Error: {1}", selectedNetwork.Ssid, result.ConnectionStatus), NotifyType.ErrorMessage);

If you need to disconnect, WiFiAdapter.Disconnect() can be used.

To summarize:

  • To enable Wi-Fi device access, add the wifiControl DeviceCapability to the AppX manifest

  • In the code, first thing is to request access to WiFiAdapter methods using WiFiAdapter.RequestAccessAsync()

  • Find available WiFi adapters by enumerating wifi devices using WiFi adapter device selector or WiFiAdapter.FindAllAdaptersAsync()

  • Use the WiFiAdapter.ScanAsync() method to find all available networks

  • Finally, connect to an available network using WiFiAdapter.ConnectAsync() and check the connection result

Sample code

Language: C#

Tags: c#, beginner, wi-fi

Verified to work with:
Windows 10 IoT Core: Version 10.0.10586.0
Visual Studio 2015 update 2
Windows SDK: Version 10586
(Included with Visual Studio Update 2)

Edit this page on GitHub

Was this page helpful?
Additional feedback?
Thank you! We appreciate your feedback.
Follow us