Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect WiFi tethering state

Tags:

android

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. It doesn't depend on real state of WiFi tethering.

like image 685
Nolesh Avatar asked Jan 30 '12 14:01

Nolesh


People also ask

Can tethering be detected?

In reality Mobile Network Internet Providers predominantly use Deep Package Inspection with URI fingerprinting to detect tethering. It is the only method that is feasible to use for large scale operation.

Can WiFi be tethered?

You can use your phone's mobile data to connect another phone, tablet, or computer to the internet. Sharing a connection this way is called tethering or using a hotspot. Some phones can share Wi-Fi connection by tethering. Most Android phones can share mobile data by Wi-Fi, Bluetooth, or USB.

How do I turn on WiFi tethering?

Navigate to Settings > Network & internet > Hotspot & tethering. Here, you can select to share a connection via Wi-Fi, USB, or Bluetooth. For a Wi-Fi connection, tap Wi-Fi hotspot and toggle it on. The hotspot name will be displayed on this screen.


4 Answers

Using reflection:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
Method[] wmMethods = wifi.getClass().getDeclaredMethods();
for (Method method: wmMethods) {
  if (method.getName().equals("isWifiApEnabled")) {

    try {
      boolean isWifiAPenabled = method.invoke(wifi);
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (InvocationTargetException e) {
      e.printStackTrace();
  }
}

As you can see here

like image 197
Jayyrus Avatar answered Oct 09 '22 01:10

Jayyrus


In addition to the reflexion, to get the Wifi tethering status update, you can listen to this broadcast Action :

IntentFilter filter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");

To get all tethering option update :

IntentFilter filter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");

Those actions are hidden inside the Android source code

like image 35
Tobliug Avatar answered Oct 09 '22 02:10

Tobliug


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"/>
like image 40
Denis Gladkiy Avatar answered Oct 09 '22 01:10

Denis Gladkiy


Here is the Xamarin C# version if anyone is looking:

    static Method isWifiApEnabledMethod;

    public static bool IsWifiApEnabled ()
    {
        var wifiManager = WifiManager.FromContext (Application.Context);
        if (isWifiApEnabledMethod == null)
        {
            try
            {
                isWifiApEnabledMethod = wifiManager.Class.GetDeclaredMethod ("isWifiApEnabled");
                isWifiApEnabledMethod.Accessible = true; //in the case of visibility change in future APIs
            }
            catch (NoSuchMethodException e)
            {
                Debug.WriteLine ("Can't get method by reflection" + e);
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine ("Can't get method by reflection" + ex);

            }
        }

        if (isWifiApEnabledMethod != null)
        {
            try
            {
                return (bool)isWifiApEnabledMethod.Invoke (wifiManager);
            }
            catch (System.Exception ex)
            {
                Debug.WriteLine ("Can't invoke by reflection" + ex);

            }
        }

        return false;
    }
like image 21
JamesMontemagno Avatar answered Oct 09 '22 01:10

JamesMontemagno