7

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 values as iff the WIFI is turned off on the device.

Thnaks, best regards

MaxChinni
  • 1,206
  • 14
  • 21
softwaresupply
  • 1,908
  • 3
  • 20
  • 34
  • The tethering functionality is in the ConnectivityManager class, but hidden and not in the public API. If you intend to use the "unpublished API" you're going to need a modified framework JAR or use reflection. The method you're looking for is probably String[] ConnectivityManager#getTetheredIfaces(), which returns the currently tethered network interfaces. – Jens Nov 04 '11 at 10:00

2 Answers2

10

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:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Denis Gladkiy
  • 2,084
  • 1
  • 26
  • 40
  • What is the purpose of all the finals? Are these required in some instances? – diedthreetimes Mar 31 '15 at 01:00
  • @diedthreetimes: "What is the purpose of all the finals? Are these required in some instances?"—Coding style, each to their own. See [should I use final](https://programmers.stackexchange.com/q/48413/158721) and [final abuse](https://programmers.stackexchange.com/q/98691/158721). – Daniel Jul 01 '15 at 17:12
  • A little addition: one should check if the "getSystemService" returned null. – Denis Gladkiy May 19 '17 at 04:52
  • Does not work for me. I am getting an error: "Expected receiver of type android.net.wifi.WifiManager, but got java.lang.reflect.Method" – Mikhail Jun 23 '22 at 11:21
8

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)

Reno
  • 33,594
  • 11
  • 89
  • 102
  • There is no need to iterate all declared methods: the Class class have "getDeclaredMethod" method. Check out the code in my answer. – Denis Gladkiy Dec 11 '13 at 02:26