How to enable Auto USB Tethering in Android programmatically or using shell for Android 7 and higher

1.1k Views Asked by At

I've tried different ways to do it:

  1. Tried Connectivity Manager class which is deprecated by Android because it doesn't have a setUsbTethering method.
  2. Tried this method:
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
public void switchOnTethering() {

    Object obj = getSystemService(Context.CONNECTIVITY_SERVICE);
    for (Method m : obj.getClass().getDeclaredMethods()) {

        if (m.getName().equals("tether")) {
            try {
                m.invoke(obj, "usb0");
            } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

  1. Tried adb shell service call connectivity 30,31,32,33,34 i32 1 command
  2. Tried
adb shell
su
echo "rndis" > /sys/class/android_usb/android0/functions
echo "1" > /sys/class/android_usb/android0/enable
  1. Tried
adb shell
su
netcfg rndis0 up
netcfg rndis0 dhcp
  1. The only thing that works for me Adb shell svc usb setFunctions rndis but it works only in Android 11

I need to make it for Android 7 and higher. It's okay if there is a way needs root because I have rooted device.

1

There are 1 best solutions below

2
Alex P. On

In Android 7.0 it was adb shell svc usb setFunction rndis

You can also try adb shell setprop sys.usb.config rndis