Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android turn On/Off WiFi HotSpot programmatically

Is there an API to turn On/Off the WiFi HotSpot on Android programmatically?

What methods should I call to turn it On/Off?

UPDATE:There's this option to have the HotSpot enabled, and just turn On/Off the WiFi, but this is not a good solution for me.

like image 515
mxg Avatar asked Jun 18 '11 07:06

mxg


People also ask

Can I set my hotspot to turn off automatically?

Open the Hotspot & tethering options. Go to the Wi-Fi hotspot. Check the Turn off hotspot automatically option.

How do you turn off hotspot on Android?

Go to Manage > Devices. Choose the device on which you want to disable Personal Hotspot. Go to Actions and select Disable Personal Hotspot. You will be asked if you want to stop sharing Personal Hotspot.

How do I temporarily disable my hotspot?

There is an option in settings, normally under the connections group to enable/disable the phone's WiFi hotspot. You can also configure the hotspot's network name (SSID) and pass phrase in the same part of the settings.


1 Answers

Warning This method will not work beyond 5.0, it was a quite dated entry.

Use the class below to change/check the Wifi hotspot setting:

import android.content.*; import android.net.wifi.*; import java.lang.reflect.*;  public class ApManager {  //check whether wifi hotspot on or off public static boolean isApOn(Context context) {     WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);          try {         Method method = wifimanager.getClass().getDeclaredMethod("isWifiApEnabled");         method.setAccessible(true);         return (Boolean) method.invoke(wifimanager);     }     catch (Throwable ignored) {}     return false; }  // toggle wifi hotspot on or off public static boolean configApState(Context context) {     WifiManager wifimanager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);     WifiConfiguration wificonfiguration = null;     try {           // if WiFi is on, turn it off         if(isApOn(context)) {                            wifimanager.setWifiEnabled(false);         }                        Method method = wifimanager.getClass().getMethod("setWifiApEnabled", WifiConfiguration.class, boolean.class);                            method.invoke(wifimanager, wificonfiguration, !isApOn(context));         return true;     }      catch (Exception e) {         e.printStackTrace();     }            return false; } } // end of class 

You need to add the permissions below to your AndroidMainfest:

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

Use this standalone ApManager class from anywhere as follows:

ApManager.isApOn(YourActivity.this); // check Ap state :boolean ApManager.configApState(YourActivity.this); // change Ap state :boolean 

Hope this will help someone

like image 52
Ashish Sahu Avatar answered Oct 02 '22 03:10

Ashish Sahu