Xamarin Essentials Connectivity
The Connectivity class lets you monitor for changes in the device's network conditions, check the current network access, and how it is currently connected.
Last session we've made an example with Battery Stats, so let's create an example of Connectivity Status.
- Open the Essentials Solution in Visual Studio
-
Right Click on Essentials.Android and go to Properties then go to Android Manifest and search in the list of permissions ACCESS_NETWORK_STATE and check it.
- Then just save doing ctrl + s, now you're able to request the network state permission
- Let's create a folder inside the Portable Proyect with the name of Features
-
Let's create a class inside the Features with the name ConnectivityHelper and paste this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
using System.Collections.Generic; using System.Linq; using Xamarin.Essentials; using Xamarin.Forms; namespace Essentials.Features { public class ConnectivityHelper { public NetworkAccess GetNetworkAccess() { NetworkAccess networkAccess = Connectivity.NetworkAccess; return networkAccess; } public List<ConnectionProfile> GetConnectionProfile() { List<ConnectionProfile> profiles = Connectivity.ConnectionProfiles.ToList(); return profiles; } } }
-
After that just create another ContentPage inside Views folder with the name ConnectivityPage and add this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="Essentials.Views.ConnectivityPage" xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"> <ContentPage.Content> <Grid> <Grid.RowDefinitions> <RowDefinition Height="80" /> <RowDefinition Height="1" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> <RowDefinition Height="50" /> </Grid.RowDefinitions> <Label FontSize="30" HorizontalOptions="Center" Text="Connectivity Status" TextColor="{StaticResource NavigationPrimary}" VerticalOptions="Center" /> <BoxView Grid.Row="1" BackgroundColor="{StaticResource SecondColor}" /> <Label x:Name="networkAccessLabel" Grid.Row="3" FontSize="20" HorizontalOptions="Center" Text="Internet" TextColor="{StaticResource NavigationPrimary}" VerticalOptions="Center" /> <Label x:Name="ConnectionProfileLabel" Grid.Row="5" FontSize="20" HorizontalOptions="Center" Text="Bluetooth" TextColor="{StaticResource NavigationPrimary}" VerticalOptions="Center" /> </Grid> </ContentPage.Content> </ContentPage>
-
finally at ConnectivityPage.xaml.cs paste this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
using Essentials.Features; using System.Collections.Generic; using System.Linq; using Xamarin.Essentials; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace Essentials.Views { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class ConnectivityPage : ContentPage { public static NetworkAccess access; public static List<ConnectionProfile> profiles; public ConnectivityPage() { InitializeComponent(); var networkAccess = new ConnectivityHelper().GetNetworkAccess(); var profile = (new ConnectivityHelper().GetConnectionProfile()).First(); networkAccessLabel.Text = $"Network Access: {networkAccess}"; connectionProfileLabel.Text = $"Profile: {profile}"; Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged; } private void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e) { networkAccessLabel.Text = $"Network Access: {e.NetworkAccess}"; connectionProfileLabel.Text = $"Profile: {e.ConnectionProfiles.First()}"; } } }
That's it, now you have to take a look into this last file, the networkAccess and the profile variables are the main part of this feature, and in every change they update our labels declared in the ConnectivityPage.xaml Page.
Results
Related Links
Xamarin Essentials NuGet Essentials
Xamarin Essentials Documentation Microsoft Xamarin.Essentials Documentation
Xamarin Essentials Connectivity Documentation Connectivity Stats
Completed Solution Essentials Repo