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
Access_Coarse_Location Permission Gives a Cell Tower Precision on Android
My Android Camera Uri Is Returning a Null Value, But the Samsung Fix Is in Place, Help
Get Android Subscription Status, Failed with 403
Android Test Code Coverage with Jacoco Gradle Plugin
Proxy Which Requires Authentication with Android Emulator
Stop Fragment Refresh in Bottom Nav Using Navhost
Activity Killed/Oncreate Called After Taking Picture via Intent
Suggestions to Avoid Bitmap Out of Memory Error
How to Set an Imageview's Image from a String
Listview with Choice_Mode_Multiple Using Checkedtext in a Custom View
Size of Android Notification Bar and Title Bar
Adding Multiple Markers in Google Maps API V2 Android
Getting Context in Androidtestcase or Instrumentationtestcase in Android Studio's Unit Test Feature
How to Close the Status Bar/Notification Panel After Notification Button Click
Video Compression on Android Using New Mediacodec Library
Gcm Push Notification with ASP.NET
"This App Is Not Authorized to Use Firebase Authentication" in Emulator