Xamarin.Forms - Network Speed Monitor

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.
 

Network Speed Monitor

 
Network speed monitor is one of the most important things in mobile apps because avoiding the app crashes and slowness.
 
 
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 Platform.
 
 

Network Monitor

 
Now, create a network helper class for checking network connectivity in Xamarin.froms. You will use the http url ( http://google.com).
 
NetworkHelper.cs
  1. public class NetworkHelper  
  2.     {  
  3.   
  4.           
  5.         public async Task<string> CheckInternetSpeed()  
  6.         {  
  7.             DateTime dt1 = DateTime.Now;  
  8.             string internetSpeed;  
  9.             try  
  10.             {  
  11.                 var client = new HttpClient();  
  12.                 byte[] data = await client.GetByteArrayAsync("http://xamarinmonkeys.blogspot.com/");  
  13.                 DateTime dt2 = DateTime.Now;                Console.WriteLine("ConnectionSpeed: DataSize (kb) " + data.Length / 1024);  
  14.                 Console.WriteLine("ConnectionSpeed: ElapsedTime (secs) " + (dt2 - dt1).TotalSeconds);  
  15.                 internetSpeed = "ConnectionSpeed: (kb/s) " + Math.Round((data.Length / 1024) / (dt2 - dt1).TotalSeconds, 2);  
  16.             }  
  17.             catch (Exception ex)  
  18.             {  
  19.                 internetSpeed = "ConnectionSpeed:Unknown Exception-" + ex.Message;  
  20.             }  
  21.             Console.WriteLine(internetSpeed);  
  22.             return internetSpeed;  
  23.         }  
  24.     }  

Consuming Network helper

 
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, get the Nework speed as return type string and see the result in Toast. You can show an alert or whatever your need.
 
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.         private async void Button_Clicked(object sender, EventArgs e)  
  23.         {  
  24.               
  25.   
  26.             if (current == NetworkAccess.Internet)  
  27.             {  
  28.                   
  29.                 var speed=await CheckInternetSpeed();  
  30.                 DependencyService.Get<IToast>().ShowToast(speed);  
  31.   
  32.             }  
  33.             else  
  34.             {  
  35.                 DependencyService.Get<IToast>().ShowToast("No Internet Connection");  
  36.             }  
  37.         }  
  38.   }  
  39. }  
Run
 
 
Click the check button and get the network speed.
 
 
I hope you have understood  how to check network speed in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback.
 
Happy Coding :)


Similar Articles