WiFi ON/OFF On Button Click In Xamarin Android

Introduction

Recently, I used WifiManager class in my the code for turning the Wi-Fi on/off in Xamarin. This class provides the primary API for managing all aspects of Wi-Fi connectivity. Get an instance of this class by calling Context.GetSystemService(String).

This is the API to use when performing Wi-Fi specific operations. To perform operations that pertain to network connectivity at an abstract level, use ConnectivityManager.

ConnectivityManager is a class that answers queries about the state of network connectivity. It also notifies the applications when network connectivity changes. Get an instance of this class by calling Context.GetSystemService(String).

Steps to create a demo for Wi-Fi on/off on button click:

Create an instance of WifiManger class on your page.

WiFi ON/OFF On Button Click In Xamarin Android

Create button “WiFi ON” and on button click event call SetWifiEnabled function of the wifiManager class. And pass bool type parameter into that. For turning WiFi on, pass (true) into SetWifiEnabled function.

WiFi ON/OFF On Button Click In Xamarin Android

Create button “WiFi OFF” and on button click event call SetWifiEnabled function of a wifiManager class. And pass bool type parameter into that. For turning WiFi on pass (false) into SetWifiEnabled function.

WiFi ON/OFF On Button Click In Xamarin Android

View of xml page

  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:App1" x:Class="App1.MainPage">  
  3.     <StackLayout x:Name="stkMain">  
  4.         <!-- Place new controls here -->  
  5.     </StackLayout>  
  6. </ContentPage>  

View of .cs page

  1. using Android.Content;  
  2. using Android.Net.Wifi;  
  3. using Xamarin.Forms;  
  4. namespace App1 {  
  5.     publicpartialclassMainPage: ContentPage {  
  6.         public MainPage() {  
  7.             InitializeComponent();  
  8.             WifiManager wifi = (WifiManager) Android.App.Application.Context.GetSystemService(Context.WifiService);  
  9.             Button btnWifiON = new Button {  
  10.                 Text = "WiFi ON",  
  11.                     HorizontalOptions = LayoutOptions.CenterAndExpand,  
  12.                     VerticalOptions = LayoutOptions.Center,  
  13.             };  
  14.             btnWifiON.Clicked += async (sender, e) => {  
  15.                 wifi.SetWifiEnabled(true);  
  16.             };  
  17.             Button btnWifiOff = new Button {  
  18.                 Text = "WiFi OFF",  
  19.                     HorizontalOptions = LayoutOptions.CenterAndExpand,  
  20.                     VerticalOptions = LayoutOptions.Center,  
  21.             };  
  22.             btnWifiOff.Clicked += (send, e) => {  
  23.                 wifi.SetWifiEnabled(false);  
  24.             };  
  25.             StackLayout stkbtn = new StackLayout {  
  26.                 HorizontalOptions = LayoutOptions.FillAndExpand,  
  27.                     VerticalOptions = LayoutOptions.FillAndExpand,  
  28.                     Orientation = StackOrientation.Horizontal  
  29.             };  
  30.             stkbtn.Children.Add(btnWifiON);  
  31.             stkbtn.Children.Add(btnWifiOff);  
  32.             stkMain.Children.Add(stkbtn);  
  33.         }  
  34.     }  

After adding code, now, run your project with the selected device.

WiFi ON/OFF On Button Click In Xamarin Android

After debugging your project successfully, you will display two buttons on your app page.

Click on  “WiFi ON” button for enabling Wi-Fi and “WiFi OFF” for disabling Wi-Fi.

WiFi ON/OFF On Button Click In Xamarin Android  WiFi ON/OFF On Button Click In Xamarin Android 

Summary

Android.Net.Wifi.WifiManager class is used for Wi-Fi connectivity. It works fine to enable/disable Wi-Fi. It's the easy way to bypass the bool parameter into a SetWifiEnabled function of WifiManger class.