10

Possible Duplicate:
How to detect WiFi tethering state

I need to check if hotspot is on or not on my device through my app?

Community
  • 1
  • 1
Gaurav Gupta
  • 575
  • 1
  • 5
  • 17

1 Answers1

16

Code: Java

Method method = wifiManager.getClass().getDeclaredMethod("getWifiApState");
method.setAccessible(true);
int actualState = (Integer) method.invoke(wifiManager, (Object[]) null);

Code: Kotlin

    fun getHotspotState(): String {
    val wifiManager =
        applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
    val method: Method = wifiManager.javaClass.getMethod(
        "getWifiApState"
    )
    method.isAccessible = true
    val invoke = method.invoke(wifiManager)
    println(invoke)
    return invoke.toString();
}

the actual state should be:

public static int AP_STATE_DISABLING = 10;
public static int AP_STATE_DISABLED = 11;
public static int AP_STATE_ENABLING = 12;
public static int AP_STATE_ENABLED = 13;
public static int AP_STATE_FAILED = 14;

WifiManger source code

hope this might be help you!

lava
  • 6,020
  • 2
  • 31
  • 28
AJit
  • 1,283
  • 1
  • 13
  • 34