Is it possible to USB tether an android device using adb through the terminal?
Must have root to change values with setprop
, and I am on a Mac OS without a rndis driver so I could not test your method of USB tethering. Another way, if you have the connectivity service (adb shell service list
):
The following commands call ConnectivityManager.setUsbTethering(boolean enable)
in Android 4.3:
adb shell su -c service call connectivity 34 i32 1
turns on USB tethering.
adb shell su -c service call connectivity 34 i32 0
turns off USB tethering.
For other Android versions replace 34
with the following setUsbTethering
calling codes per Android version:
4.4.4: 34
5.1.0: 30
6.0.1: 30
7.0.0: 33
For Android 5.0+ (Lollipop, Marshmallow) use:
adb shell su -c service call connectivity 30 i32 1
to turn USB Tethering ON
adb shell su -c service call connectivity 30 i32 0
to turn USB Tethering OFF
Keep in mind that this requires root.
You can also script the inputs to start the Settings app and tick the checkbox, like in https://github.com/medvid/android-tether/blob/master/tether#L83.
Here's my script (pretty much the same as in the link, but slightly adapted):
adb shell am force-stop com.android.settings
adb shell input keyevent 3 # Home
sleep 2
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.TetherSettings
sleep 2
adb shell input keyevent 19 # Up
adb shell input keyevent 20 # Down
adb shell input keyevent 66 # Enter
sleep 2
adb shell input keyevent 3 # Home
For Windows, just replace sleep
with timeout -t
.
Works fine for my OnePlus 3T running Android Pie (9) (with Google's Settings app (running the Pixel Experience ROM); can't verify if it works with other Settings apps or not)
Commands in accepted answer not work on Oreo because now should be additional parameter callerPkg
and if put there some random text it works.
int setUsbTethering(boolean enable, String callerPkg);
So, for 8.0 / 8.1 Oreo:
service call connectivity 34 i32 1 s16 text
- turn USB tethering ON
service call connectivity 34 i32 0 s16 text
- turn USB tethering OFF
It works for me Android Pie with
service call connectivity 33 i32 1 s16 text
- turn USB tethering ON
service call connectivity 33 i32 0 s16 text
- turn USB tethering OFF