How to detect WiFi tethering state

前端 未结 5 2071
悲哀的现实
悲哀的现实 2020-11-30 07:29

I want to know how to detect state of WiFi tethering. I\'ve seen an article: Android 2.3 wifi hotspot API But it doesn\'t work! It returns always WIFI_AP_STATE_DISABLED = 1.

5条回答
  •  粉色の甜心
    2020-11-30 07:44

    First, you need to get WifiManager:

    Context context = ...
    final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    

    Then:

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

    Also you need to request a permission in AndroidManifest.xml:

    
    

提交回复
热议问题