We’ll learn how to find and connect to WiFi 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 WiFi adapter connected to your board, click on the “Scan Available WiFi Networks” button to start scanning and displaying the list of WiFi networks in the vicinity.
Each WiFi network is identified by its SSID along with an icon showing the signal strength.
The selected network shows some information about the SSID including the signal strength, channel frequency and authentication mode. 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.
If WiFi Protected Setup (WPS) is supported a WPS button is shown. To use this button first click on the WPS button on your router and then on the WPS button in the application.
For networks that support Extensible Authentication Protocol (EAP), also known as Enterprise WiFi authentication, a password checkbox will be shown. If your enterprise network device is configured with the required certificates, you can simply click Connect.
If you must enter a user name and password to connected your network then select the Password checkbox and then enter the User and Password. The Domain is optional and only needs to be entered on networks that require it.
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.
Because some networks, like WiFi hotspots, may require additional information that can only be provided through a browser, 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.
Selecting the currently connected network also displays a Disconnect button that can be used to disconnect from the network.
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 WiFi 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_Scenario.xaml and .cs files. Values that are bound to properties in ListView items can be found in DisplayHelper.cs.
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 WiFi 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 WiFi network is selected from the ones displayed, we need to determine if we want to collect the password credential. The WiFi network authentication type is what we need to determine that:
To determine if the network supports WPS we can check if the current network supports WiFiWpsKind.PushButton.
And finally, to determine if EAP network support is available we can check to see if the authentication type is Rsna or Wpa.
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 whether 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 WiFi 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