14

I want to enable USB Tethering from my app on my Android 4.0 Device? The following code works for Android 2.2 but it does not work for 4.0. Can anyone help?

int USBTethering(boolean b) {
        try {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            Log.d(tag, "test enable usb tethering");
            Method[] wmMethods = cm.getClass().getDeclaredMethods();
            String str = "";
            if (b)
                str = "tether";
            else
                str = "untether";
            for (Method method : wmMethods) {
                Log.d("in usb tethering method",method.getName()+"<<nn>>");
                if (method.getName().equals(str)) {
                    Log.d(tag, "gg==" + method.getName());
                    Log.d("in if", " case matches "+method.getName()+"and str is "+str);
                    try {
                        Integer code = (Integer) method.invoke(cm, "usb0");
                    //  code = (Integer) method.invoke(cm, "setting TH");
                        Log.d(tag, "code===" + code);
                        return 1;
                    } catch (IllegalArgumentException e) {
                        Log.d(tag, "eroor== gg " + e.toString());
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        Log.d(tag, "eroor== gg " + e.toString());
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        Log.d(tag, "eroor== gg " + e.toString());
                        e.printStackTrace();
                    }
                }
            }
            return 0;

                       } catch (Exception e) {
            Log.e(tag, "" + e);
            return 0;
        }

    }
w5m
  • 2,286
  • 3
  • 34
  • 46
Gaurav Gupta
  • 575
  • 1
  • 5
  • 17
  • Maybe this link can help you: [Android - enable USB tethering programmatically - there is an app that did it for 2.3][1] [1]: http://stackoverflow.com/questions/9913645/android-enable-usb-tethering-programmatically-there-is-an-app-that-did-it-fo – GioLaq Jan 14 '13 at 17:09
  • @MahajanGuy Did you found a solution? With rooted or without rooted device? – Naskov Jan 17 '13 at 09:26
  • Root is required for android 4.0 and above to do that, if you have root, you have to add the permission: WRITE_SECURE_SETTINGS(http://stackoverflow.com/a/12209096/1683141) – Mdlc Apr 17 '13 at 16:04
  • What happens if you call getTetherableIfaces() and pass in the string you want into tether(String)? https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/ConnectivityManager.java @671 & 705 – Ethan Apr 18 '13 at 21:18
  • What device or devices have your tried? – David Wasser Apr 19 '13 at 11:23

2 Answers2

1

I've got code that is similar to this and it works on Android 4.0 (but only on some devices). Unfortunately, access to tethering is very vendor-specific. The one thing I did notice is that you don't make the method you are trying to call accessible. If, on the devices you are using, the method has been made private, this won't work. Try adding:

method.setAccessible(true);

before you call

Integer code = (Integer) method.invoke(cm, "usb0");

The other thing is that the interface name (in your case "usb0" is also vendor-specific. The interface name is different on different devices from different manufacturers. Make sure that you've got the correct interface name for the device you are testing.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

On my rooted device i got it working with this: (xamarin)

suProcess = Runtime.GetRuntime().Exec("su root service call connectivity 30 i32 1");

see https://android.stackexchange.com/questions/29954/can-i-change-some-android-settings-from-the-command-line

Community
  • 1
  • 1