In my previous post, we talked about the communication scenarios in windows runtime applications, categorized them in three main sections. We walked through applications of different scenarios for these domains on Windows Phone 8.1 platform. We also had a quick look at the External communication domain changes in UWP platform and Windows 10 Mobile.
Communication with the System
One of the areas that saw the introduction of many new features is the application’s communication line with the system and control over certain features.
UWP application now can access so-called Radios (networking peripherals), turn them on/off and even enumerate available networks, provided that the application has the right capability declared in the manifest.
In order for a mobile application to access the management features, the application first needs to call RequestAccessAsync function and wait for the consent of the user. Once the consent is provided, in-built functionality can be used to manage the radios.
For instance in order to turn the Wi-Fi adapter on:
// Request access to set the radio state // must be called at least once from the UI thread await Radio.RequestAccessAsync(); // Turn on WiFi radios var radios = await Radio.GetRadiosAsync(); foreach (var radio in radios) { if (radio.Kind == RadioKind.WiFi) { await radio.SetStateAsync(RadioState.On); } }
Another example implementation would be to Enumerate Wireless Network Connections using the Wi-Fi adapter:
var access = await WiFiAdapter.RequestAccessAsync(); if (access != WiFiAccessStatus.Allowed) { // TODO: throw exception? exit the current process? User denied access } else { var result = await Windows.Devices.Enumeration.DeviceInformation. FindAllAsync(WiFiAdapter.GetDeviceSelector()); if (result.Count >= 1) { firstAdapter = await WiFiAdapter.FromIdAsync(result[0].Id); } else { // TODO: throw exception? dialog? no Wi-Fi adapters. } }
Once we receive the Wi-Fi adapter, all we have to do is to scan the are and see if there are any available networks (and details about available networks such as SSID, BSSID, Network Strength, Freq.):
await firstAdapter.ScanAsync(); var report = firstAdapter.NetworkReport; var message = string.Format(“Network Report Timestamp: {0}”, report.Timestamp); foreach (var network in report.AvailableNetworks) { //Format the network information message += string.Format("\nNetworkName: {0}, BSSID: {1}, RSSI: {2}dBm, Channel Frequency: {3}kHz", network.Ssid, network.Bssid, network.NetworkRssiInDecibelMilliwatts, network.ChannelCenterFrequencyInKilohertz);
And connecting to a Wi-Fi network is as simple as connecting a network service with a client SDK:
WiFiAvailableNetwork availableNetwork = availableNetworks[0]; string passPhrase = “password”; var passwordCredentials = new PasswordCredential(null, null, passPhrase); var connectionResult = await firstAdapter.ConnectAsync(availableNetwork, WiFiConnectionKind.Permanent, passwordCredentials); if (connectionResult.ConnectionStatus != WiFiConnectionStatus.Success) { // Connection was unsuccessful }
Finally another attractive system management feature that was added to the Windows 10 mobile platform is the connection sharing. From the user perspective is a dream come through to be able to initialize the mobile hotspot on the mobile device with the click of a button from the pc. From the developers perspective, NetworkOperatorTetheringManager supplies us with the required methods to start connection sharing within your application.
NOTE: It is important to include the wiFiControl capability in the package manifest to be able to use the NetworkOperatorTetheringManager like any other manager from the Windows.Devices.WiFi namespace. |
In order to initialize the manager, we would have to check if a certain connection on the mobile device suitable for sharing, if so, just use the internet profile to start the manager:
NetworkOperatorTetheringManager tetheringManager = null; ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile(); if (InternetConnectionProfile != null && InternetConnectionProfile.IsWwanConnectionProfile) { tetheringManager = NetworkOperatorTetheringManager.CreateFromConnectionProfile(InternetConnectionProfile); }
The we can use the tethering manager to initiate the shared connection:
try { NetworkOperatorTetheringOperationResult result = await tetheringManager.StartTetheringAsync(); if (result.Status != TetheringOperationStatus.Success) { if (result.AdditionalErrorMessage != "") { // TODO: Additional Error Message } else { var errorStatus = result.Status; // TODO: Error Status } } else { // Success } } catch (Exception ex) { // TODO: }
This completes the system domain communication scenarios on UWP applications for Windows 10 Mobile. In the next post, we will be talking about App2App communication scenarios.
Happy coding everyone…
Pingback: Connected Apps With Windows 10 Mobile (UWP) – I | Can Bilgin
Hello,
Thats a helpful post. I am developing windows universal app and i want mac address of the network adapter, so i created tetheringManager object using tetheringManager = NetworkOperatorTetheringManager.CreateFromConnectionProfile code, but i am getting following error on this line of code
An exception of type ‘System.UnauthorizedAccessException’ occurred. Additional information: Access is denied.
Any ideas what could be the solution to this error. I tried giving all capabilities to the app from manifest file, but that does not work.
LikeLike
I think you need the wiFiControl capability (see Device Capabilities)… good point though I will update the post with the capabilities required.
LikeLike
This was very helpful! Thanks for posting!
LikeLike