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.
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.
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.
View of xml page
- <?xml version="1.0" encoding="utf-8" ?>
- <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">
- <StackLayout x:Name="stkMain">
- <!-- Place new controls here -->
- </StackLayout>
- </ContentPage>
View of .cs page
- using Android.Content;
- using Android.Net.Wifi;
- using Xamarin.Forms;
- namespace App1 {
- publicpartialclassMainPage: ContentPage {
- public MainPage() {
- InitializeComponent();
- WifiManager wifi = (WifiManager) Android.App.Application.Context.GetSystemService(Context.WifiService);
- Button btnWifiON = new Button {
- Text = "WiFi ON",
- HorizontalOptions = LayoutOptions.CenterAndExpand,
- VerticalOptions = LayoutOptions.Center,
- };
- btnWifiON.Clicked += async (sender, e) => {
- wifi.SetWifiEnabled(true);
- };
- Button btnWifiOff = new Button {
- Text = "WiFi OFF",
- HorizontalOptions = LayoutOptions.CenterAndExpand,
- VerticalOptions = LayoutOptions.Center,
- };
- btnWifiOff.Clicked += (send, e) => {
- wifi.SetWifiEnabled(false);
- };
- StackLayout stkbtn = new StackLayout {
- HorizontalOptions = LayoutOptions.FillAndExpand,
- VerticalOptions = LayoutOptions.FillAndExpand,
- Orientation = StackOrientation.Horizontal
- };
- stkbtn.Children.Add(btnWifiON);
- stkbtn.Children.Add(btnWifiOff);
- stkMain.Children.Add(stkbtn);
- }
- }
- }
After adding code, now, run your project with the selected device.
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.
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.