Check the Connectivity of a Mobile Device with Xamarin.Essentials

Install

Here’s a step by steo procedure and code to check that if a mobile device is properly connected or not with Xamarin.Essentials.

To get started with the Xamarin.Essentials, as of the first step install a NuGet Package in the standard .Net library of your project if you are developing a iOS, Android, or a UWP technology based mobile app development project in the Xamarin environment.

Now moving on to the second step, in which you need add this small code in the android app to initialize the Xamarin.Essentials. Makr sure to add the code on the “OnCreate method” in any ‘Activity’ or ‘MainLauncher’ of the android project.

protected override void OnCreate(Bundle exampleState) {

//…

base.OnCreate(exampleState);

Xamarin.Essentials.Platform.Init(this, exampleState); // add this line to your code, it may also be called: bundle

//…

Add the below mentioned code to manage the runtime permission on android in Xamarin.Essentials on ‘OnRequestPermissionsResult’ on its activity classes.

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)

{

Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

}

Now check that the particular application has the correct permission to access the connected APIs. Remember that Android is the only platform which requires permission  which is accomploished by AssemblyInfo.cs or AndroidManifest.xml file. For this, open your project’s AssemblyInfo.cs file and add this code. You can easily goe this file in the project’s properties folder.

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

Alongside this, do not forget to update the Android Manifest file by mentioning the below code:

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

Check the network access:

While developing a mobile application, at the time of data posting, must check the current network access of the device. It enables a user to set a device on the airplane mode, no internet access etc.

Use the code:

Var current = Connectivity.NetworkAccess;

switch(current)

{

case NetworkAccess.Internet:

// Connected to internet

break;

case NetworkAccess.Local:

// Only local network access

break;

case NetworkAccess.ConstrainedInternet:

// Connected, but limited internet access such as behind a network login page

break;

case NetworkAccess.None:

// No internet available

break;

case NetworkAccess.Unknown:

// Internet access is unknown

break;

}

All done now, apply the code in your Xamarin project and enjoy running the program.

by admin

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>