Home Archive Trilium Notes About

Activating Android USB tethering via adb

Posted on 2015-06-15

I have an oldish Android 2.3.6 phone (a Samsung Galaxy Mini). The last few months, I have been tethering it to my laptop via USB. It’s a bit tedious to manually activate tethering every time I connect the phone.

Luckily, Android contains a utility called service (which lives in /system/bin/service). This utility lets you issue calls to Android services through adb shell. You can list all available services by running service list. Apparently, services provide API endpoints identified by a numerical identifier, and every endpoint accepts some number of arguments. The service utility lets you pass strings or 32-bit integers, like this:

$ service call [service_name] [endpoint_id] s16 [string_arg] i32 [int_arg] ...

By Googling around, I found that Android phones have a connectivity service, which is probably implemented as an instance of android.net.IConnectivityManager. Unfortunately, I couldn’t find any API definition that would exactly match what my phone seemed to have. Maybe the API was updated a few times between my Android version and the current one?

By blindly probing and looking at adb logcat, I found the following interesting call IDs:

I aborted my search here, because I found what I came for: calling service call connectivity 15 s16 usb0 starts tethering via USB. So, to get an Android 2.3.6 phone to tether with your PC, you can call: adb shell su -c service call connectivity 15 s16 usb0. I’ll probably turn this snippet into a mini-script. Unfortunately, you need to use a rooted phone and the su part: otherwise, you’ll get back a Parcel with a permission complaint.

And, just as I finished my search, I found this interesting blog post, where the author describes a better way of getting at the service codes than wild probing: apparently if you know your Android version and the service name you want, you can find an AIDL (Android Interface Definition Language) with the service description on https://android.googlesource.com/.

YMMV. Cheers.