Is there a way to check whether tethering is active?

前端 未结 2 1055
面向向阳花
面向向阳花 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:39

    Try using reflection, like so:

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    Method[] wmMethods = wifi.getClass().getDeclaredMethods();
    for(Method method: wmMethods){
    if(method.getName().equals("isWifiApEnabled")) {
    
    try {
      method.invoke(wifi);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
    }
    }
    

    (It returns a Boolean)


    As Dennis suggested it is better to use this :

        final Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
        method.setAccessible(true); //in the case of visibility change in future APIs
        return (Boolean) method.invoke(manager);
    

    (manager is the WiFiManager)

提交回复
热议问题