We’ll learn how to find and connect to Wi-Fi networks using a Universal Windows Platform (UWP) app that makes use of the
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.
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
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
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
The sample app when deployed displays a screen similar to the one below.
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.
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.
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.
The code for this sample makes use of the
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.
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:
The code of this sample is mostly contained within WiFiConnect_Scanrio.xaml and .cs files.
The first step to access the device is to request access using the static
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.
WiFiAdapter.FindAllAdaptersAsync() can be used to find all WiFi adapters to achieve the same.
The next step is to scan for available WiFi networks, this can be achieved using the
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.
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:
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.
If you need to disconnect,
WiFiAdapter.Disconnect() can be used.
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
Find available WiFi adapters by enumerating wifi devices using WiFi adapter device selector or
WiFiAdapter.ScanAsync() method to find all available networks
Finally, connect to an available network using
WiFiAdapter.ConnectAsync() and check the connection result