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

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. <StackLayout HorizontalOptions="Center" VerticalOptions="Start" Margin="0,150,0,0">
  9. <Label FontSize="Large" Text="Xamarin Monkeys"/>
  10. <Button Text="Check" Clicked="Button_Clicked"></Button>
  11. </StackLayout>
  12. </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. var current = Connectivity.NetworkAccess;
  22. if (current == NetworkAccess.Internet)
  23. {
  24. bool isConnectionFast = DependencyService.Get<INetwork>().IsConnectedFast();
  25. if (isConnectionFast)
  26. DependencyService.Get<IToast>().ShowToast("Network Connection is good");
  27. else
  28. DependencyService.Get<IToast>().ShowToast("Network Connection is slow");
  29. }
  30. else
  31. {
  32. DependencyService.Get<IToast>().ShowToast("No Internet Connection");
  33. }
  34. }
  35. }
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 :)