Connectivity

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IConnectivity interface to inspect the network accessibility of the device. The network connection may have access to the internet. Devices also contain different kinds of network connections, such as Bluetooth, cellular, or WiFi. The IConnectivity interface has an event to monitor changes in the devices connection state.

The default implementation of the IConnectivity interface is available through the Connectivity.Current property. Both the IConnectivity interface and Connectivity class are contained in the Microsoft.Maui.Networking namespace.

Get started

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:

  • Add the assembly-based permission:

    Open the Platforms/Android/MainApplication.cs file and add the following assembly attributes after using directives:

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

    - or -

  • Update the Android Manifest:

    Open the Platforms/Android/AndroidManifest.xml file and add the following in the manifest node:

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

Using Connectivity

You can determine the scope of the current network by checking the NetworkAccess property.

NetworkAccess accessType = Connectivity.Current.NetworkAccess;

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

Network access falls into the following categories:

  • Internet — Local and internet access.
  • ConstrainedInternet — Limited internet access. This value means that there's a captive portal, where local access to a web portal is provided. Once the portal is used to provide authentication credentials, internet access is granted.
  • 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:

IEnumerable<ConnectionProfile> profiles = Connectivity.Current.ConnectionProfiles;

if (profiles.Contains(ConnectionProfile.WiFi))
{
    // Active Wi-Fi connection.
}

Whenever the connection profile or network access changes, the ConnectivityChanged event is raised:

public class ConnectivityTest
{
    public ConnectivityTest() =>
        Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;

    ~ConnectivityTest() =>
        Connectivity.ConnectivityChanged -= Connectivity_ConnectivityChanged;

    void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
    {
        if (e.NetworkAccess == NetworkAccess.ConstrainedInternet)
            Console.WriteLine("Internet access is available but is limited.");

        else if (e.NetworkAccess != NetworkAccess.Internet)
            Console.WriteLine("Internet access has been lost.");

        // Log each active connection
        Console.Write("Connections active: ");

        foreach (var item in e.ConnectionProfiles)
        {
            switch (item)
            {
                case ConnectionProfile.Bluetooth:
                    Console.Write("Bluetooth");
                    break;
                case ConnectionProfile.Cellular:
                    Console.Write("Cell");
                    break;
                case ConnectionProfile.Ethernet:
                    Console.Write("Ethernet");
                    break;
                case ConnectionProfile.WiFi:
                    Console.Write("WiFi");
                    break;
                default:
                    break;
            }
        }

        Console.WriteLine();
    }
}

Limitations

It's important to know that it's possible that Internet is reported by NetworkAccess but full access to the web isn't available. Because of 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 isn't available.