12

我正在设置一些测试,它将需要相当数量的手机进行 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我看到手机为“????????????没有权限”,此时我必须拔出 USB 电缆,然后重新插入,并使用 .重新启动 adb 服务器adb kill-server adb start-server。这将不起作用,因为我无法访问电话以进行所需的配置更改。

我用谷歌搜索,但一直找不到任何富有成果的东西。有什么建议么?

4

7 回答 7

20

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

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

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

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

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

4.4.4: 34
5.1.0: 30
6.0.1: 30
7.0.0: 33
于 2014-06-21T21:40:54.963 回答
7

接受答案中的命令在 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 网络共享

于 2018-08-29T10:01:20.213 回答
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,只需替换sleeptimeout -t.

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

于 2018-11-24T15:15:36.193 回答
6

对于 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。

于 2015-12-22T15:50:34.683 回答
2

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
于 2018-02-16T14:54:28.120 回答
0

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

  • 开启开发者模式(可能默认开启)
  • 搜索“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"
于 2017-06-01T06:05:00.837 回答
0

Android 4.2 果冻豆:

adb shell su -c service call connectivity 33 i32 1

于 2020-09-18T05:23:08.277 回答