9

可能重复:
如何检测 WiFi 网络共享状态

我需要通过我的应用程序检查热点是否在我的设备上?

4

1 回答 1

15

代码: Java

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

代码: 科特林

    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();
}

实际状态应该是:

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 源代码

希望这可能对您有所帮助!

于 2013-01-18T18:32:15.893 回答