instagram

Android Enable Disable HotSpot Programmatically

This is an example of how to enable disable Hotspot programmatically.

NOTE: Before enable hotspot you need to turn off Wifi.

Check How to enable disable wifi in android

Following method will enable disable hotspot.

/**
	 * Turn on or off Hotspot.
	 * 
	 * @param context
	 * @param isTurnToOn
	 */
	public static void turnOnOffHotspot(Context context, boolean isTurnToOn) {
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiApControl apControl = WifiApControl.getApControl(wifiManager);
		if (apControl != null) {

                 // TURN OFF YOUR WIFI BEFORE ENABLE HOTSPOT
			//if (isWifiOn(context) && isTurnToOn) {
			//	turnOnOffWifi(context, false);
			//}

			apControl.setWifiApEnabled(apControl.getWifiApConfiguration(),
					isTurnToOn);
		}
	}

Following permission require :

android.permission.ACCESS_WIFI_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.ACCESS_NETWORK_STATE

But to run this method you need WifiApControl.java

WifiApControl.java

package com.omt.remote.util.net;

import java.lang.reflect.Method;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * This class is use to handle all Hotspot related information.
 * 
 *
 * 
 */
public class WifiApControl {
	private static Method getWifiApState;
	private static Method isWifiApEnabled;
	private static Method setWifiApEnabled;
	private static Method getWifiApConfiguration;

	public static final String WIFI_AP_STATE_CHANGED_ACTION = "android.net.wifi.WIFI_AP_STATE_CHANGED";

	public static final int WIFI_AP_STATE_DISABLED = WifiManager.WIFI_STATE_DISABLED;
	public static final int WIFI_AP_STATE_DISABLING = WifiManager.WIFI_STATE_DISABLING;
	public static final int WIFI_AP_STATE_ENABLED = WifiManager.WIFI_STATE_ENABLED;
	public static final int WIFI_AP_STATE_ENABLING = WifiManager.WIFI_STATE_ENABLING;
	public static final int WIFI_AP_STATE_FAILED = WifiManager.WIFI_STATE_UNKNOWN;

	public static final String EXTRA_PREVIOUS_WIFI_AP_STATE = WifiManager.EXTRA_PREVIOUS_WIFI_STATE;
	public static final String EXTRA_WIFI_AP_STATE = WifiManager.EXTRA_WIFI_STATE;

	static {
		// lookup methods and fields not defined publicly in the SDK.
		Class<?> cls = WifiManager.class;
		for (Method method : cls.getDeclaredMethods()) {
			String methodName = method.getName();
			if (methodName.equals("getWifiApState")) {
				getWifiApState = method;
			} else if (methodName.equals("isWifiApEnabled")) {
				isWifiApEnabled = method;
			} else if (methodName.equals("setWifiApEnabled")) {
				setWifiApEnabled = method;
			} else if (methodName.equals("getWifiApConfiguration")) {
				getWifiApConfiguration = method;
			}
		}
	}

	public static boolean isApSupported() {
		return (getWifiApState != null && isWifiApEnabled != null
				&& setWifiApEnabled != null && getWifiApConfiguration != null);
	}

	private WifiManager mgr;

	private WifiApControl(WifiManager mgr) {
		this.mgr = mgr;
	}

	public static WifiApControl getApControl(WifiManager mgr) {
		if (!isApSupported())
			return null;
		return new WifiApControl(mgr);
	}

	public boolean isWifiApEnabled() {
		try {
			return (Boolean) isWifiApEnabled.invoke(mgr);
		} catch (Exception e) {
			Log.v("BatPhone", e.toString(), e); // shouldn't happen
			return false;
		}
	}

	public int getWifiApState() {
		try {
			return (Integer) getWifiApState.invoke(mgr);
		} catch (Exception e) {
			Log.v("BatPhone", e.toString(), e); // shouldn't happen
			return -1;
		}
	}

	public WifiConfiguration getWifiApConfiguration() {
		try {
			return (WifiConfiguration) getWifiApConfiguration.invoke(mgr);
		} catch (Exception e) {
			Log.v("BatPhone", e.toString(), e); // shouldn't happen
			return null;
		}
	}

	public boolean setWifiApEnabled(WifiConfiguration config, boolean enabled) {
		try {
			return (Boolean) setWifiApEnabled.invoke(mgr, config, enabled);
		} catch (Exception e) {
			Log.v("BatPhone", e.toString(), e); // shouldn't happen
			return false;
		}
	}
}
Share