Connectivity APIs for UWP Apps
The Windows Universal Platform (UWP) provides a rich set of APIs for managing and querying network connectivity. Understanding these APIs is crucial for building robust applications that adapt to various network conditions, provide informative feedback to users, and optimize network resource usage.
Key Concepts
UWP applications can monitor network status changes and query the current connection profile to determine network capabilities, cost, and roaming status. This information helps in making informed decisions about network-dependent operations.
Network Status Notifications
Applications can subscribe to network status change events to be notified when the network connectivity changes. This allows for real-time adaptation to network availability and changes.
Connection Profiles
The ConnectionProfile object represents the current network connection. It provides details such as:
- Network signal strength
- Connectivity level (e.g., Internet, Local)
- Cost details (e.g., metered connection, roaming)
- Network adapter information
Core Classes and Interfaces
NetworkInformation Class
The central class for accessing network-related information. It provides static methods for retrieving network profiles, checking connectivity, and subscribing to notifications.
| Method/Property | Description |
|---|---|
GetInternetConnectionProfile() |
Retrieves the profile for the current internet connection. |
GetConnectionProfiles() |
Retrieves all available connection profiles. |
GetHostNames() |
Retrieves a list of host names associated with the device. |
GetInternetConnectivityStatus() |
Gets the current internet connectivity status. |
GetLanConnectivityStatus() |
Gets the current LAN connectivity status. |
NetworkStatusChanged event |
Raised when the network status changes. |
ConnectionProfile Class
Represents a network connection profile.
| Property | Description |
|---|---|
NetworkConnectivityLevel |
Gets the connectivity level (e.g., InternetAccess, LocalAccess). |
IsWwanConnectionProfile |
Indicates if this is a WWAN (cellular) connection profile. |
IsWlanConnectionProfile |
Indicates if this is a WLAN (Wi-Fi) connection profile. |
GetNetworkCostInfoAsInternetConnectionProfile() |
Retrieves network cost information. |
SignalStrength |
Gets the signal strength for wireless connections. |
NetworkCostType Enum
Specifies the cost associated with a network connection.
| Value | Description |
|---|---|
Unrestricted |
The network connection is not metered. |
Fixed |
The network connection has a fixed data limit. |
Variable |
The network connection has a variable data limit. |
Unknown |
The network cost is unknown. |
Common Scenarios
Checking for Internet Connectivity
A common task is to determine if the device has access to the internet.
// C# Example
using Windows.Networking.Connectivity;
// ...
var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (connectionProfile != null)
{
var connectivityLevel = connectionProfile.GetNetworkConnectivityLevel();
if (connectivityLevel == NetworkConnectivityLevel.InternetAccess)
{
// Internet is available
System.Diagnostics.Debug.WriteLine("Internet access is available.");
}
else
{
// Internet is not available
System.Diagnostics.Debug.WriteLine("Internet access is not available.");
}
}
else
{
// No connection profile found
System.Diagnostics.Debug.WriteLine("No network connection profile found.");
}
// JavaScript Example
// Note: In UWP JavaScript apps, you'd typically use WinJS or similar frameworks.
// This is a conceptual representation.
// Assuming access to Windows.Networking.Connectivity namespace via WinRT projection
Windows.Networking.Connectivity.NetworkInformation.getInternetConnectionProfile().done((connectionProfile) => {
if (connectionProfile) {
const connectivityLevel = connectionProfile.getNetworkConnectivityLevel();
if (connectivityLevel === Windows.Networking.Connectivity.NetworkConnectivityLevel.internetAccess) {
console.log("Internet access is available.");
} else {
console.log("Internet access is not available.");
}
} else {
console.log("No network connection profile found.");
}
});
Responding to Network Changes
You can subscribe to the NetworkStatusChanged event to react to network status modifications.
// C# Example
using Windows.Networking.Connectivity;
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
NetworkInformation.NetworkStatusChanged += NetworkStatusChangedHandler;
}
private void NetworkStatusChangedHandler(object sender)
{
var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (connectionProfile != null)
{
var connectivityLevel = connectionProfile.GetNetworkConnectivityLevel();
// Update UI or logic based on connectivityLevel
System.Diagnostics.Debug.WriteLine($"Network status changed. Connectivity level: {connectivityLevel}");
}
else
{
System.Diagnostics.Debug.WriteLine("Network status changed. No network connection profile found.");
}
}
// Remember to unsubscribe when the page is no longer needed
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
NetworkInformation.NetworkStatusChanged -= NetworkStatusChangedHandler;
base.OnNavigatedFrom(e);
}
}
Handling Metered Connections
It's good practice to be mindful of data usage, especially on metered connections.
// C# Example
using Windows.Networking.Connectivity;
// ...
var connectionProfile = NetworkInformation.GetInternetConnectionProfile();
if (connectionProfile != null)
{
var costInfo = connectionProfile.GetNetworkCostInfoAsInternetConnectionProfile();
if (costInfo != null && costInfo.NetworkCostType != NetworkCostType.Unrestricted)
{
// This is a metered connection (Fixed or Variable)
System.Diagnostics.Debug.WriteLine($"Network is metered. Cost type: {costInfo.NetworkCostType}");
// Consider disabling large downloads or background data usage.
}
else
{
// Network is unrestricted
System.Diagnostics.Debug.WriteLine("Network is unrestricted.");
}
}
Permissions
To use these networking APIs, your UWP application must declare the appropriate capabilities in its manifest file.
- Internet (Client & server): Required for general internet access.
- Home or Work Networking: Required for accessing devices on the local network.
You can edit these capabilities in Visual Studio by opening the Package.appxmanifest file and going to the "Capabilities" tab.