源码中,热点的几种状态定义:
public static final int WIFI_AP_STATE_DISABLING = 10;
public static final int WIFI_AP_STATE_DISABLED = 11;
public static final int WIFI_AP_STATE_ENABLING = 12;
public static final int WIFI_AP_STATE_ENABLED = 13;
public static final int WIFI_AP_STATE_FAILED = 14;
public static boolean isWifiApOn(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
Method method = null;
Field field = null;
int i = 0;
//方法 1
try {
method = wifiManager.getClass().getMethod("getWifiApState");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
try {
i = (Integer) method.invoke(wifiManager);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//方法2
try {
field = wifiManager.getClass().getDeclaredField("WIFI_AP_STATE_ENABLED");
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
if (field != null) {
try {
i = (int) field.get(wifiManager);
}catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return i == 13; // 13 为已打开
}