Monitor The Network Connectivity Changes In Xamarin Forms Application Using Xamarin Essentials For Android And UWP

The Connectivity class is available at Xamarin.Essentials API. It is used to monitor for changes in the device's network conditions, check the current network access, and how it is currently connected. Android, iOS, and UWP offer unique operating systems and platform APIs that developers have access to all in C#, leveraging Xamarin. Xamarin.Essentials provides a single cross-platform API that works with any Xamarin.Forms, Android, iOS, or UWP application that can be accessed from shared code no matter how the user interface is created.

Reading this article, you can learn how to check the changes of network connectivity conditions and current network access in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform with XAML and Visual C# in the cross-platform application development.

The following important tools are required for developing Xamarin Forms App,

  1. Windows 10 (Recommended)
  2. Visual Studio 2017
  3. Android API 19 or higher and UWP 10.0.16299.0 or higher.

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2017 -> Start -> New Project-> Select Cross-Platform (under Visual C#-> Mobile App (Xamarin.Forms)-> Give a suitable name for your app (XamFormConnectivity) ->OK

 

Step 2

Select the Cross-Platform template as a Blank APP ->Set platform as Android and UWP and code sharing strategy as .NET standard. Afterwards, Visual Studio creates 3 projects (Portable, Android, UWP)

 

Step 3

For adding a reference,

RightClick Your solution (XamFormConnectivity) and Select Manage NuGet Packages. For adding Xamarin.Essentials reference, choose Browse and search Xamarin.Essentials, select the package and select all the projects (portable, Android, UWP) and install it.

 

Step 4

Add the Label controls in Mainpage.Xaml for displaying Title and Netwok Access, Network Profile.

  1. <Label FontAttributes="Bold" Text="Check the Network Condition changes in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center"/>  
  2. <Label HorizontalOptions="Center" x:Name="lblNetStatus"></Label>  
  3. <Label HorizontalOptions="Center" x:Name="lblNetProfile"></Label>  

Step 5

Add the following code in theAssemblyInfo.csin XamFormConnectivity.Android project under Properties folder,

  1. [assembly: UsesPermission(Android.Manifest.Permission.BatteryStats)]  

Step 6

Add the following code in the MainActivity.cs in XamFormConnectivity.Android project,

In onCreate() method,

  1. Xamarin.Essentials.Platform.Init(this, savedInstanceState);  

Also add the below method,

  1. publicoverridevoid OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {  
  2.     Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  3.     base.OnRequestPermissionsResult(requestCode, permissions, grantResults);  
  4. }  

Step 7

Add the following namespace and code in Mainpage.Xaml.cs

  1. using Xamarin.Essentials;  
  2. public MainPage() {  
  3.     InitializeComponent();  
  4.     ConnectivityTest();  
  5. }  
  6. publicvoid ConnectivityTest() {  
  7.     Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;  
  8. }  
  9. void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e) {  
  10.     var access = e.NetworkAccess;  
  11.     var profiles = e.Profiles;  
  12.     if (access == NetworkAccess.Internet) {  
  13.         lblNetStatus.Text = "Local and internet access";  
  14.     }  
  15.     elseif(access == NetworkAccess.ConstrainedInternet) {  
  16.         lblNetStatus.Text = "Limited internet access";  
  17.     }  
  18.     elseif(access == NetworkAccess.Local) {  
  19.         lblNetStatus.Text = "Local network access only";  
  20.     }  
  21.     elseif(access == NetworkAccess.None) {  
  22.         lblNetStatus.Text = "No connectivity is available";  
  23.     }  
  24.     elseif(access == NetworkAccess.Unknown) {  
  25.         lblNetStatus.Text = "Unable to determine internet connectivity";  
  26.     }  
  27.     if (profiles.Contains(ConnectionProfile.WiFi)) {  
  28.         lblNetProfile.Text = profiles.FirstOrDefault().ToString();  
  29.     } else {  
  30.         lblNetProfile.Text = profiles.FirstOrDefault().ToString();  
  31.     }  
  32. }  

Note
Automatically the following code will be generated in XAML code view, while we are done in the design view.

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamFormConnectivity" x:Class="XamFormConnectivity.MainPage">  
  3.     <StackLayout>  
  4.         <Label FontAttributes="Bold" Text="Check the Network Condition changes in Xamarin Forms application using Xamarin Essentials for Android and UWP" VerticalOptions="Center" HorizontalOptions="Center" />  
  5.         <Label HorizontalOptions="Center" x:Name="lblNetStatus"></Label>  
  6.         <Label HorizontalOptions="Center" x:Name="lblNetProfile"></Label>  
  7.     </StackLayout>  
  8. </ContentPage>  

Step 8

Deploy your App in UWP and Android. When wifi connectivity is enabled in UWP, the output of the XamFormConnectivity App is:


 

When mobile data is enabled in Android,

 

When the wifi connectivity is disabled in UWP,

When mobile data is enabled in Android,

 

Summary

Now, you have successfully checked the device network changes in Xamarin Forms application using Xamarin Essentials for Android and Universal Windows Platform using Visual C# and Xamarin.


Similar Articles