Automated USB tethering with Android-equipped smartphones
In absence of a decent internet offer in our new house, we decided to wait
for the fiber to reach the house by just using our 4G phones tethering.
However, tethering over Wi-Fi is annoying and drains battery, so I wondered
how hard would it be to automatically launch a script that would start the
USB tethering when plugging our phones in our Debian-equipped laptops.
Step 1 : preparation
Install adb, identify your phone USB identifiers (vendor &
model), and automatically accept the android adb key (~/.android/adbkey) by
tapping on your phone.
You can use scrcpy to click on your phone over the USB link of
adb, that tool is pretty cool.
user@debian:~$ lsusb | grep -i samsung
Bus 004 Device 005: ID 04e8:6864 Samsung Electronics Co., Ltd GT-I9070 (network tethering, USB debugging enabled)
Step 2 : udev rule (triggers a script when an USB device gets plugged)
In the file /etc/udev/rules.d/local.rules, place the following
contents, adapting your USB identifiers. I added two because my phone
resets USB and changes its identifiers whenever USB tethering is activated.
ACTION=="add", SUBSYSTEM=="usb", ENV{ID_VENDOR_ID}=="04e8", ENV{ID_MODEL_ID}=="6864", RUN+="/home/user/git/tether/usb.sh"
ACTION=="add", SUBSYSTEM=="usb", ENV{ID_VENDOR_ID}=="04e8", ENV{ID_MODEL_ID}=="6860", RUN+="/home/user/git/tether/usb.sh"
Then, in the file /home/user/git/tether/usb.sh (or wherever you want to place it) :
#!/bin/bash
sleep 2
{
date
id
echo "listing devices.."
# https://android.stackexchange.com/questions/29954/activate-usb-tethering-from-the-command-line
adb devices
echo "activating rndis"
adb shell svc usb setFunctions rndis
echo "checking rndis.."
adb shell svc usb getFunctions
} >> /tmp/e
The core of the program is calling adb shell svc usb setFunctions rndis, and some logs should land in /tmp/e.
Have a nice day.