首页 > 解决方案 > 如何查找设备是否配置为便携式热点

问题描述

如果设备配置为便携式热点,我在文档中找不到如何检索。

我已阅读如何检测网络是否(配置为)Android 上的移动热点?但我不知道该功能是否已实现?

标签: androidandroid-wifipersonal-hotspot

解决方案


您可以使用以下代码检查您的设备上是否启用了网络共享:

private static Method isWifiApEnabledMethod;

public static boolean isWifiApEnabled(WifiManager wifiManager) {
    if (isWifiApEnabledMethod == null) {
        try {
            isWifiApEnabledMethod = wifiManager.getClass().getDeclaredMethod("isWifiApEnabled");
            isWifiApEnabledMethod.setAccessible(true); //in the case of visibility change in future APIs
        } catch (NoSuchMethodException e) {
            Log.w(TAG, "Can't get method by reflection", e);
        }
    }

    if (isWifiApEnabledMethod != null) {
        try {
            return (Boolean) isWifiApEnabledMethod.invoke(wifiManager);
        } catch (IllegalAccessException e) {
            Log.e(TAG, "Can't invoke method by reflection", e);
        } catch (InvocationTargetException e) {
            Log.e(TAG, "Can't invoke method by reflection", e);
        }
    }

    return false;
}

不要忘记在下面添加权限AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

对于上述有问题的 SO链接:

此功能不可用,即尚未实施。有关更多信息,请查看Google 正式将状态标记为Status: Won't Fix (Obsolete)的链接

希望这会帮助你。谢谢!


推荐阅读