Xamarin.Forms - Mobile Network Speed Check (Slow Or Fast) In Android

Introduction

 
Xamarin.Forms code runs on multiple platforms, each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You wíll learn more by going through the steps yourself.
 
Create a new or existing Xamarin forms (.Net standard) Project. with Android and iOS Platforms.
 
 
Create a Interface
  1. public interface INetwork  
  2.     {  
  3.         bool IsConnected();  
  4.         bool IsConnectedFast();  
  5.     }   

Create a NetworkConnectivity

 
The following code will check the Mobile network connection and check the network status.
 
NetworkConnectivity.cs  
  1. public class NetworkConnectivity  
  2.     {  
  3.         public static NetworkInfo GetNetworkInfo(Context context)  
  4.         {  
  5.             ConnectivityManager cm = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);  
  6.             return cm.ActiveNetworkInfo;  
  7.         }  
  8.   
  9.         /** 
  10.          * Check if there is any connectivity 
  11.          * @param context 
  12.          * @return 
  13.          */  
  14.         public static bool IsConnected(Context context)  
  15.         {  
  16.             NetworkInfo info = NetworkConnectivity.GetNetworkInfo(context);  
  17.             return (info != null && info.IsConnected);  
  18.         }  
  19.   
  20.         /** 
  21.          * Check if there is any connectivity to a Wifi network 
  22.          * @param context 
  23.          // @param type 
  24.          * @return 
  25.          */  
  26.         public static bool IsConnectedWifi(Context context)  
  27.         {  
  28.             NetworkInfo info = NetworkConnectivity.GetNetworkInfo(context);  
  29.             return (info != null && info.IsConnected && info.Type == ConnectivityType.Wifi);  
  30.         }  
  31.   
  32.         /** 
  33.          * Check if there is any connectivity to a mobile network 
  34.          * @param context 
  35.          // @param type 
  36.          * @return 
  37.          */  
  38.         public static bool IsConnectedMobile(Context context)  
  39.         {  
  40.             NetworkInfo info = NetworkConnectivity.GetNetworkInfo(context);  
  41.             return (info != null && info.IsConnected && info.Type == ConnectivityType.Mobile);  
  42.         }  
  43.   
  44.         /** 
  45.          * Check if there is fast connectivity 
  46.          * @param context 
  47.          * @return 
  48.          */  
  49.         public static bool IsConnectedFast(Context context)  
  50.         {  
  51.             NetworkInfo info = NetworkConnectivity.GetNetworkInfo(context);  
  52.             TelephonyManager tm = TelephonyManager.FromContext(context);  
  53.             return (info != null && info.IsConnected && NetworkConnectivity.IsConnectionFast(info.Type, tm.NetworkType));  
  54.         }  
  55.   
  56.         /** 
  57.          * Check if the connection is fast 
  58.          * @param type 
  59.          * @param subType 
  60.          * @return 
  61.          */  
  62.         public static bool IsConnectionFast(ConnectivityType type, NetworkType subType)  
  63.         {  
  64.             if (type == ConnectivityType.Wifi)  
  65.             {  
  66.                 return true;  
  67.             }  
  68.             else if (type == ConnectivityType.Mobile)  
  69.             {  
  70.                 switch (subType)  
  71.                 {  
  72.                     //case TelephonyManager.NETWORK_TYPE_1xRTT:  
  73.                     case NetworkType.OneXrtt:  
  74.                         return false// ~ 50-100 kbps  
  75.                                       //case TelephonyManager.NETWORK_TYPE_CDMA:  
  76.                     case NetworkType.Cdma:  
  77.                         return false// ~ 14-64 kbps  
  78.                                       //case TelephonyManager.NETWORK_TYPE_EDGE:  
  79.                     case NetworkType.Edge:  
  80.                         return false// ~ 50-100 kbps  
  81.                                       //case TelephonyManager.NETWORK_TYPE_EVDO_0:  
  82.                     case NetworkType.Evdo0:  
  83.                         return true// ~ 400-1000 kbps  
  84.                                      //case TelephonyManager.NETWORK_TYPE_EVDO_A:  
  85.                     case NetworkType.EvdoA:  
  86.                         return true// ~ 600-1400 kbps  
  87.                                      //case TelephonyManager.NETWORK_TYPE_GPRS:  
  88.                     case NetworkType.Gprs:  
  89.                         return false// ~ 100 kbps  
  90.                                       //case TelephonyManager.NETWORK_TYPE_HSDPA:  
  91.                     case NetworkType.Hsdpa:  
  92.                         return true// ~ 2-14 Mbps  
  93.                                      //case TelephonyManager.NETWORK_TYPE_HSPA:  
  94.                     case NetworkType.Hspa:  
  95.                         return true// ~ 700-1700 kbps  
  96.                                      //case TelephonyManager.NETWORK_TYPE_HSUPA:  
  97.                     case NetworkType.Hsupa:  
  98.                         return true// ~ 1-23 Mbps  
  99.                                      //case TelephonyManager.NETWORK_TYPE_UMTS:  
  100.                     case NetworkType.Umts:  
  101.                         return true// ~ 400-7000 kbps  
  102.                     /* 
  103.                      * Above API level 7, make sure to set android:targetSdkVersion 
  104.                      * to appropriate level to use these 
  105.                      */  
  106.                     //case TelephonyManager.NETWORK_TYPE_EHRPD: // API level 11  
  107.                     case NetworkType.Ehrpd:  
  108.                         return true// ~ 1-2 Mbps  
  109.                                      //case TelephonyManager.NETWORK_TYPE_EVDO_B: // API level 9  
  110.                     case NetworkType.EvdoB:  
  111.                         return true// ~ 5 Mbps  
  112.                                      //case TelephonyManager.NETWORK_TYPE_HSPAP: // API level 13  
  113.                     case NetworkType.Hspap:  
  114.                         return true// ~ 10-20 Mbps  
  115.                                      //case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8  
  116.                     case NetworkType.Iden:  
  117.                         return false// ~25 kbps  
  118.                                       //case TelephonyManager.NETWORK_TYPE_LTE: // API level 11  
  119.                     case NetworkType.Lte:  
  120.                         return true// ~ 10+ Mbps  
  121.                                      // Unknown  
  122.                                      //case TelephonyManager.NETWORK_TYPE_UNKNOWN:  
  123.                     case NetworkType.Unknown:  
  124.                         return false;  
  125.                     default:  
  126.                         return false;  
  127.                 }  
  128.             }  
  129.             else  
  130.             {  
  131.                 return false;  
  132.             }  
  133.         }  
  134.   
  135.         public static bool IsHostReachable(string host)  
  136.         {  
  137.             if (string.IsNullOrEmpty(host))  
  138.                 return false;  
  139.   
  140.             bool isReachable = true;  
  141.   
  142.             Thread thread = new Thread(() =>  
  143.             {  
  144.                 try  
  145.                 {  
  146.                     //isReachable = InetAddress.GetByName(host).IsReachable(2000);  
  147.   
  148.                     /*  
  149.                      * It's important to note that isReachable tries ICMP ping and then TCP echo (port 7). 
  150.                      * These are often closed down on HTTP servers. 
  151.                      * So a perfectly good working API with a web server on port 80 will be reported as unreachable 
  152.                      * if ICMP and TCP port 7 are filtered out! 
  153.                      */  
  154.   
  155.                     //if (!isReachable){  
  156.                     URL url = new URL("http://" + host);  
  157.   
  158.                     URLConnection connection = url.OpenConnection();  
  159.   
  160.                     //if(connection.ContentLength != -1){  
  161.                     //isReachable = true;  
  162.                     if (connection.ContentLength == -1)  
  163.                     {  
  164.                         isReachable = false;  
  165.                     }  
  166.                     //}  
  167.   
  168.                 }  
  169.                 catch (UnknownHostException e)  
  170.                 {  
  171.                     isReachable = false;  
  172.                 }  
  173.                 catch (IOException e)  
  174.                 {  
  175.                     isReachable = false;  
  176.                 }  
  177.   
  178.             });  
  179.             thread.Start();  
  180.   
  181.             return isReachable;  
  182.         }  
  183.   
  184.     }  
Android Implementation
 
NetworkHelper.cs 
 
Here, implement the Interface and return the network status.
  1. [assembly: Xamarin.Forms.Dependency(typeof(NetworkHelper))]  
  2. namespace NetworkPOC.Droid  
  3. {  
  4.     public class NetworkHelper : INetwork  
  5.     {  
  6.         Context context = Android.App.Application.Context;  
  7.         public bool IsConnected()  
  8.         {  
  9.             return NetworkConnectivity.IsConnected(context);  
  10.         }  
  11.   
  12.         public bool IsConnectedFast()  
  13.         {  
  14.             return NetworkConnectivity.IsConnectedFast(context);  
  15.         }  
  16.     }  
  17. }  

Consume the NetworkHelper

 
Here, you will call the Network helper class and you will get the network speed.
 
MainPage.Xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:d="http://xamarin.com/schemas/2014/forms/design"  
  5.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  6.              mc:Ignorable="d"  
  7.              x:Class="NetworkPOC.MainPage">  
  8.   
  9.     <StackLayout HorizontalOptions="Center" VerticalOptions="Start" Margin="0,150,0,0">  
  10.         <Label FontSize="Large" Text="Xamarin Monkeys"/>  
  11.         <Button Text="Check" Clicked="Button_Clicked"></Button>  
  12.     </StackLayout>  
  13.   
  14. </ContentPage>  
Here I show the result in Toast.
 
MainPage.xaml.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Xamarin.Forms;  
  8. using Xamarin.Essentials;  
  9. using System.Net.Http;  
  10. namespace NetworkPOC  
  11. {  
  12.     // Learn more about making custom code visible in the Xamarin.Forms previewer  
  13.     // by visiting https://aka.ms/xamarinforms-previewer  
  14.     [DesignTimeVisible(false)]  
  15.     public partial class MainPage : ContentPage  
  16.     {  
  17.         public MainPage()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         var current = Connectivity.NetworkAccess;  
  23.   
  24.             if (current == NetworkAccess.Internet)  
  25.             {  
  26.   
  27.                 bool isConnectionFast = DependencyService.Get<INetwork>().IsConnectedFast();  
  28.                 if (isConnectionFast)  
  29.                     DependencyService.Get<IToast>().ShowToast("Network Connection is good");  
  30.                 else  
  31.                     DependencyService.Get<IToast>().ShowToast("Network Connection is slow");  
  32.   
  33.             }  
  34.             else  
  35.             {  
  36.                 DependencyService.Get<IToast>().ShowToast("No Internet Connection");  
  37.             }    
  38.     }  
  39. }  
Run
 
 
I hope you have understood how to check your mobile network speed (slow or fast) using Android native in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback.
 
Happy Coding :)


Similar Articles