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

前端 未结 7 1907
终归单人心
终归单人心 2020-12-29 10:30

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

相关标签:
7条回答
  • 2020-12-29 10:43

    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

    0 讨论(0)
  • 2020-12-29 10:48

    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)

    0 讨论(0)
  • 2020-12-29 10:48

    For Fairphone 2 with Fairphone Open OS (the "Android without Google" version, which is not installed by default) you need to:

    • Enable Developer mode (may be actived by default)
    • Search setting for "root" and enable root access for ADB
    • Enter bash command in quotes and use service code 31:
      • Enable: adb shell su -c "service call connectivity 31 i32 1"
      • Disable: adb shell su -c "service call connectivity 31 i32 0"
    0 讨论(0)
  • 2020-12-29 10:51

    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.

    0 讨论(0)
  • 2020-12-29 10:54

    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
    
    0 讨论(0)
  • 2020-12-29 11:01

    Android 4.2 Jelly bean:

    adb shell su -c service call connectivity 33 i32 1

    0 讨论(0)
提交回复
热议问题