Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to USB tether an android device using adb through the terminal?

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?

like image 386
turbo Avatar asked Nov 26 '13 20:11

turbo


People also ask

How do I enable USB tethering with ADB?

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.


2 Answers

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:

#!/bin/bash
set -euo pipefail

# Set up USB tethering for an Android device.
# Usage: adb-usb-tether [USB-VENDOR USB-PRODUCT]
# If USB vendor/product is unspecified, use first USB network interface.
# On the Android side, tethering is enabled via adb shell.

if [[ $# -eq 2 ]]
then
    any=false
    vendor=$1
    product=$2
else
    any=true
fi

function find_if() {
    local path if
    for path in /sys/class/net/*
    do
        if=$(basename "$path")
        if [[ "$(readlink "$path")" == */usb* ]]
        then
            local ifproduct ifvendor
            ifproduct=$(cat "$(realpath "$path")/../../../idProduct")
            ifvendor=$(cat "$(realpath "$path")/../../../idVendor")
            if $any || [[ "$ifproduct" == "$product" && "$ifvendor" == "$vendor" ]]
            then
                echo "Found interface: $if" 1>&2
                echo "$if"
                return
            fi
        fi
    done
}

function adb_shell() {
    adb shell "$(printf " %q" "$@")"
}

function adb_su() {
    local quoted
    quoted="$(printf " %q" "$@")"
    adb shell su -c "$(printf %q "$quoted")"
}

if=$(find_if)
if [[ -z "$if" ]]
then
    echo "Requesting interface:" 1>&2
    adb_su setprop sys.usb.config rndis,adb
    echo " >> OK" 1>&2
fi

while [[ -z "$if" ]]
do
    echo "Waiting for network device..." 1>&2
    sleep 1
    if=$(find_if)
done

while ! ( ip link | grep -qF "$if" )
do
    echo "Waiting for interface..." 1>&2
    sleep 1
done

function configure_net() {
    local name="$1"
    local if="$2"
    local ip="$3"
    local table="$4"
    local cmdq="$5" # Query command
    local cmdx="$6" # Configuration command

    if ! ( "$cmdq" ip addr show dev "$if" | grep -qF 192.168.42."$ip" )
    then
        echo "Configuring $name interface address:" 1>&2
        "$cmdx" ip addr add 192.168.42."$ip"/24 dev "$if"
        echo " >> OK" 1>&2
    fi

    if ( "$cmdq" ip addr show dev "$if" | grep -qF 'state DOWN' )
    then
        echo "Bringing $name interface up:" 1>&2
        "$cmdx" ip link set dev "$if" up
        sleep 1
        echo " >> OK" 1>&2
    fi

    if ! ( "$cmdq" ip route show table "$table" | grep -qF "192.168.42.0/24 dev $if" )
    then
        echo "Configuring $name route:" 1>&2
        "$cmdx" ip route add table "$table" 192.168.42.0/24 dev "$if"
        echo " >> OK" 1>&2
    fi
}

configure_net local  "$if"   128 main  command   sudo
configure_net device rndis0  129 local adb_shell adb_su
like image 172
Vladimir Panteleev Avatar answered Oct 02 '22 14:10

Vladimir Panteleev


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
like image 24
PRNDL Development Studios Avatar answered Oct 02 '22 14:10

PRNDL Development Studios