Ask Learn
		
	
					Preview
					Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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.
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.
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:
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;
    }
}
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.
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in