【问题标题】:Is it possible to USB tether an android device using adb through the terminal?是否可以通过终端使用 adb 连接安卓设备?
【发布时间】:2013-12-12 04:15:20
【问题描述】:

我正在设置一些测试,它需要相当数量的手机进行 USB 连接和配置。一旦它们被绑定,我已经成功地按照我想要的方式配置它们,但是每次我(重新)启动计算机或移动测试库时,通过导航菜单来绑定手机会非常乏味.我目前使用的是运行 cyanogenmod v10.1.0 的 Nexus S 手机,但测试库可能是三星 Galaxy S4,可能与我手头的少数 Nexus S 手机混合使用。

我想以 bash 脚本的形式执行此操作,但我试图首先在命令行 (Ubuntu 13.04) 上运行它,以消除可能来自脚本的问题。我应该能够自己处理将其制作成脚本,但如果以 bash 脚本的形式提供答案很简单,请这样做。我尝试炮击设备 (adb -s $deviceID shell) 并运行:

setprop sys.usb.config rndis,adb

这会立即将我踢出设备外壳,并且无法再访问该设备。如果我运行adb devices,我会看到手机为“?????????? No Permissions”,此时我必须拔出 USB 电缆,然后重新插入,并重新启动 adb 服务器与adb kill-serveradb start-server。这不起作用,因为我无法访问手机来进行所需的配置更改。

我在 Google 上四处搜索,但找不到任何富有成果的东西。有什么建议吗?

【问题讨论】:

标签: android bash ubuntu adb tethering


【解决方案1】:

必须有 root 才能使用 setprop 更改值,而且我在没有 rndis 驱动程序的 Mac OS 上,所以我无法测试你的 USB 网络共享方法。另一种方式,如果您有连接服务 (adb shell service list):

以下命令在 Android 4.3 中调用 ConnectivityManager.setUsbTethering(boolean enable)

adb shell su -c service call connectivity 34 i32 1 开启 USB 网络共享。

adb shell su -c service call connectivity 34 i32 0 关闭 USB 网络共享。

对于其他 Android 版本,将 34 替换为每个 Android 版本的以下 setUsbTethering 调用代码:

4.4.4: 34
5.1.0: 30
6.0.1: 30
7.0.0: 33

【讨论】:

  • 仅供参考,这仅适用于 Android 4.x(更有可能,仅适用于特定版本)。数字“34”是IConnectivityManager.aidl中方法列表中的方法号
  • 作为参考,RNDIS好像和USB一样,不过要看你用的是什么设备。 nexus 是 RNDIS,galaxy s4 是 USB。不幸的是,我不再从事这个项目,所以我无法测试你的解决方案,但它似乎可以工作,所以我接受了你的回答。
【解决方案2】:

对于 Android 5.0+(棒棒糖、棉花糖)使用:

adb shell su -c service call connectivity 30 i32 1 开启 USB 网络共享

adb shell su -c service call connectivity 30 i32 0 关闭 USB 网络共享

请记住,这需要 root。

【讨论】:

  • 记录在哪里?
  • 在 adb 中使用“服务列表”和“dumpsys 活动服务”来获取设备感知的服务概览。从这里开始,无论是谷歌还是浏览(AOSP)源代码,因为据我所知,这些都不是“官方”记录的。
【解决方案3】:

对于带有 Fairphone Open OS(“Android without Google”版本,默认未安装)的 Fairphone 2,您需要:

  • 启用开发者模式(可能默认激活)
  • 搜索“root”设置并为 ADB 启用 root 访问权限
  • 在引号中输入 bash 命令并使用服务代码 31:
    • 启用:adb shell su -c "service call connectivity 31 i32 1"
    • 禁用:adb shell su -c "service call connectivity 31 i32 0"

【讨论】:

    【解决方案4】:

    service 方法不适用于我的三星设备。不过,我想出了如何通过直接配置网络接口来做到这一点。这是一个脚本,用于设置 Linux 机器和 USB 连接的根 Android 设备以进行 USB 网络共享。这不会设置 DNS 或 NAT 伪装,但足以使设备可在 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
    

    【讨论】:

      【解决方案5】:

      接受答案中的命令在 Oreo 上不起作用,因为现在应该是附加参数 callerPkg,如果放在那里一些随机文本它可以工作。

      int setUsbTethering(boolean enable, String callerPkg);

      所以,对于 8.0 / 8.1 Oreo:

      service call connectivity 34 i32 1 s16 text - 打开 USB 网络共享

      service call connectivity 34 i32 0 s16 text - 关闭 USB 网络共享

      它适用于我的 Android Pie

      service call connectivity 33 i32 1 s16 text - 打开 USB 网络共享

      service call connectivity 33 i32 0 s16 text - 关闭 USB 网络共享

      【讨论】:

        【解决方案6】:

        您还可以编写输入脚本以启动“设置”应用并勾选复选框,例如 https://github.com/medvid/android-tether/blob/master/tether#L83

        这是我的脚本(与链接中的几乎相同,但稍作修改):

        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
        

        对于 Windows,只需将 sleep 替换为 timeout -t

        适用于我的运行 Android Pie (9) 的 OnePlus 3T(使用 Google 的设置应用程序(运行 Pixel Experience ROM);无法验证它是否适用于其他设置应用程序)

        【讨论】:

          【解决方案7】:

          Android 4.2 果冻豆:

          adb shell su -c service call connectivity 33 i32 1

          【讨论】: