Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check programmatically if hotspot is enabled or disabled? [duplicate]

Tags:

android

Possible Duplicate:
How to detect WiFi tethering state

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

like image 230
Gaurav Gupta Avatar asked Sep 13 '12 07:09

Gaurav Gupta


People also ask

Why is my hotspot showing disabled?

Android: Open Settings > Connections > Mobile Hotspot and Tethering. If you don't see these options, open Settings > Network & internet > Hotspot & tethering. Tap Mobile Hotspot > Configure > Band or Wi-Fi hotspot.

How do I permanently turn off my hotspot on my 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.


1 Answers

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!

like image 105
AJit Avatar answered Sep 30 '22 16:09

AJit