How to Detect Wifi Tethering State

How to detect WiFi tethering state

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

Is there a way to check whether tethering is active?

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)

How to find the WiFi tethering state through ADB?

you can use adb shell dumpsys wifi and search for curState=TetheredState
to check wifi tethering is enabled.

how can I to determine whether the device is open wifi hotspot on android

Try this way

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_view);

prefs = new UserPrefs(this);

}

final WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
public static boolean isSharingWiFi(final WifiManager manager)
{
try
{
final Method method = manager.getClass().getDeclaredMethod("isWifiApEnabled");
method.setAccessible(true);
return (Boolean) method.invoke(manager);
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), ""+e, Toast.LENGTH_LONG).show();
}

return false;
}

AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Unity : Detect if mobile is on Tether connection

Thanks to wottle links and help, I manage to find the solution

 public bool isWifiApEnabled()
{
using (AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity"))
{
try
{
using (var wifiManager = activity.Call<AndroidJavaObject>("getSystemService", "wifi"))
{
return wifiManager.Call<bool>("isWifiApEnabled");
}
}
catch (System.Exception e)
{

}
}
return false;
}


Related Topics



Leave a reply



Submit