1

Is there any possibility to toggle wifi tethering in Android 4.2? I tried this but it seems that it doesn't work using Android 4.2!

Thanks in advance!

Edit: It DOES work I only forgot to set the right permission. Is there also a way to check if its already enabled so that I can use it as a toggle?

2 Answers 2

1

You should be able to do it in Android 4.2 make sure you have the permission

android.permission.CHANGE_WIFI_STATE

and we cannot help you unless you post your code.

I believe this will help you check if tethering is active

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();
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, Im sorry, I indeed forgot to set the right permission!:( Is there also a way to check if its already set, so that I can use it as a toggle?
1

I got it to work as a toggle myself! Here is the code:

 WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

            Method[] methods = wifiManager.getClass().getDeclaredMethods();
            boolean enabled=false;
            for (Method method : methods) {
                if (method.getName().equals("isWifiApEnabled")) {
                    try {
                       enabled = (Boolean) method.invoke(wifiManager);
                    } catch (Exception ex) {
                    }
                    break;
                }
            }
            for (Method method : methods) {
                if (method.getName().equals("setWifiApEnabled")) {
                    try {
                        method.invoke(wifiManager, null, !enabled);
                    } catch (Exception ex) {
                    }
                    break;
                }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.