Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/Disable USB or Wifi tethering programmatically on android

Tags:

Is there a way to enable or disable tethering (USB or wifi) on an android phone programmatically? Perhaps an API in android SDK or NDK or any even non-UI command to do that.

Thanks in advance.

like image 295
Gaurav Khurana Avatar asked Aug 20 '10 14:08

Gaurav Khurana


2 Answers

It is possible and without root access, I use the below code snipped in my apps:

private void setWifiTetheringEnabled(boolean enable) {
    WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

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

Your app should have the following permission:

android.permission.CHANGE_WIFI_STATE

like image 157
iTech Avatar answered Sep 18 '22 07:09

iTech


Take a look at this question.

Basically there are no public API's to do it. Take a look at the settings app to see how internal apps do it though. Might have some luck with that:

https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/TetherSettings.java

like image 22
matto1990 Avatar answered Sep 18 '22 07:09

matto1990