Is there a way to check whether tethering is active?

前端 未结 2 1021
面向向阳花
面向向阳花 2021-01-03 00:44

can I check programmaticall wheter the android device has tethering activated?

I just watched the WifiManager class. All vatraibles from the WifiInfo show the same v

2条回答
  •  有刺的猬
    2021-01-03 01:38

    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:

    
    

提交回复
热议问题