Jaa


Xamarin.Essentials: Connectivity

The Connectivity class lets you monitor for changes in the device's network conditions, check the current network access, and how it is currently connected.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

To access the Connectivity functionality the following platform specific setup is required.

The AccessNetworkState permission is required and must be configured in the Android project. This can be added in the following ways:

Open the AssemblyInfo.cs file under the Properties folder and add:

[assembly: UsesPermission(Android.Manifest.Permission.AccessNetworkState)]

OR Update Android Manifest:

Open the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Or right click on the Android project and open the project's properties. Under Android Manifest find the Required permissions: area and check the Access Network State permission. This will automatically update the AndroidManifest.xml file.

Using Connectivity

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

Check current network access:

var current = Connectivity.NetworkAccess;

if (current == NetworkAccess.Internet)
{
    // Connection to internet is available
}

Network access falls into the following categories:

  • Internet – Local and internet access.
  • ConstrainedInternet – Limited internet access. Indicates captive portal connectivity, where local access to a web portal is provided, but access to the Internet requires that specific credentials are provided via a portal.
  • Local – Local network access only.
  • None – No connectivity is available.
  • Unknown – Unable to determine internet connectivity.

You can check what type of connection profile the device is actively using:

var profiles = Connectivity.ConnectionProfiles;
if (profiles.Contains(ConnectionProfile.WiFi))
{
    // Active Wi-Fi connection.
}

Whenever the connection profile or network access changes you can receive an event when triggered:

public class ConnectivityTest
{
    public ConnectivityTest()
    {
        // Register for connectivity changes, be sure to unsubscribe when finished
        Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
    }

    void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
    {
        var access = e.NetworkAccess;
        var profiles = e.ConnectionProfiles;
    }
}

Limitations

It is important to note that it is possible that Internet is reported by NetworkAccess but full access to the web is not available. Due to how connectivity works on each platform it can only guarantee that a connection is available. For instance the device may be connected to a Wi-Fi network, but the router is disconnected from the internet. In this instance Internet may be reported, but an active connection is not available.

API

Find more Xamarin videos on Channel 9 and YouTube.