我想知道如何检测WiFi tethering状态。我看过一篇文章:Android 2.3 wifi hotspot API,但它不工作!它总是返回WIFI_AP_STATE_DISABLED = 1。它不依赖于WiFi tethering的实际状态。
发布于 2012-01-31 03:16:31
使用反射:
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for (Method method: wmMethods) {
if (method.getName().equals("isWifiApEnabled")) {
try {
boolean isWifiAPenabled = method.invoke(wifi);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}如您所见,here
发布于 2013-12-07 03:27:38
首先,您需要获取WifiManager:
Context context = ...
final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);然后:
public static boolean isSharingWiFi(final WifiManager manager)
{
try
{
final Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true); //in the case of visibility change in future APIs
return (Boolean) method.invoke(manager);
}
catch (final Throwable ignored)
{
}
return false;
}此外,您还需要在AndroidManifest.xml中请求权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>发布于 2015-11-19 03:05:04
除了reflexion之外,要获取Wifi tethering状态更新,您还可以收听以下广播操作:
IntentFilter filter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");要更新所有系留选项,请执行以下操作:
IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");这些操作隐藏在Android源代码中
https://stackoverflow.com/questions/9065592
复制相似问题