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. public async Task<string> CheckInternetSpeed()
  4. {
  5. DateTime dt1 = DateTime.Now;
  6. string internetSpeed;
  7. try
  8. {
  9. var client = new HttpClient();
  10. byte[] data = await client.GetByteArrayAsync("http://xamarinmonkeys.blogspot.com/");
  11. DateTime dt2 = DateTime.Now; Console.WriteLine("ConnectionSpeed: DataSize (kb) " + data.Length / 1024);
  12. Console.WriteLine("ConnectionSpeed: ElapsedTime (secs) " + (dt2 - dt1).TotalSeconds);
  13. internetSpeed = "ConnectionSpeed: (kb/s) " + Math.Round((data.Length / 1024) / (dt2 - dt1).TotalSeconds, 2);
  14. }
  15. catch (Exception ex)
  16. {
  17. internetSpeed = "ConnectionSpeed:Unknown Exception-" + ex.Message;
  18. }
  19. Console.WriteLine(internetSpeed);
  20. return internetSpeed;
  21. }
  22. }

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. <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, 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. private async void Button_Clicked(object sender, EventArgs e)
  22. {
  23. if (current == NetworkAccess.Internet)
  24. {
  25. var speed=await CheckInternetSpeed();
  26. DependencyService.Get<IToast>().ShowToast(speed);
  27. }
  28. else
  29. {
  30. DependencyService.Get<IToast>().ShowToast("No Internet Connection");
  31. }
  32. }
  33. }
  34. }
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 :)