I'm setting up some tests and it will require a decent number of phones to be usb tethered and configured. I've been successful in configuring them the way I want to once they have been tethered, however it would be quite tedious to tether the phones through navigating the menus, each and every time I (re)start my computer or move the test bank. I am currently using Nexus S phones running cyanogenmod v10.1.0, however the test bank will likely be Samsung Galaxy S4's possibly mixed with the few Nexus S phones I have on hand.
I want to do this as a bash script, but I'm trying to get it work at the command line (Ubuntu 13.04) first so as to remove issues that could come from scripting. I should be able to handle making it into a script myself, but if it's simple to provide an answer as bash script, please do. I tried shelling into the device (adb -s $deviceID shell
) and running:
setprop sys.usb.config rndis,adb
This promptly kicks me out of the device shell and the device is no longer accessible. If I run an adb devices
I see the phone as "?????????? No Permissions" at which point I have to take the USB cable out and then plug it in again, and also restart the adb server with adb kill-server
adb start-server
. This will not work because I cannot access the phone to make the configuration changes that I need.
I've googled around but have been unable to find anything fruitful. Any suggestions?
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 ONservice call connectivity 34 i32 0 s16 text
- turn USB tethering OFFIt works for me Android Pie with
service call connectivity 33 i32 1 s16 text
- turn USB tethering ONservice call connectivity 33 i32 0 s16 text
- turn USB tethering OFFYou 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):
For Windows, just replace
sleep
withtimeout -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)
For Android 5.0+ (Lollipop, Marshmallow) use:
adb shell su -c service call connectivity 30 i32 1
to turn USB Tethering ONadb shell su -c service call connectivity 30 i32 0
to turn USB Tethering OFFKeep in mind that this requires root.
The
service
method did not work for me on my Samsung device. I figured out how to do it by configuring the network interface directly, though. Here is a script that sets up a Linux machine and a USB-connected rooted Android device for USB tethering. This does not set up DNS or NAT masquerading, but is sufficient to make the device accessible at 192.168.42.129:For Fairphone 2 with Fairphone Open OS (the "Android without Google" version, which is not installed by default) you need to:
adb shell su -c "service call connectivity 31 i32 1"
adb shell su -c "service call connectivity 31 i32 0"
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 followingsetUsbTethering
calling codes per Android version: