Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以通过终端使用adb通过USB连接android设备?_Android_Bash_Ubuntu_Adb_Tethering - Fatal编程技术网

是否可以通过终端使用adb通过USB连接android设备?

是否可以通过终端使用adb通过USB连接android设备?,android,bash,ubuntu,adb,tethering,Android,Bash,Ubuntu,Adb,Tethering,我正在设置一些测试,它将需要一个体面的手机数量是usb连接和配置。我已经成功地以我想要的方式配置了它们,一旦它们被拴住了,但是每次我(重新)启动计算机或移动测试库时,通过导航菜单来拴住手机会非常乏味。我目前正在使用运行cyanogenmod v10.1.0的Nexus S手机,但测试库可能是三星Galaxy S4,可能与我手头的少数Nexus S手机混合使用 我想把它作为一个bash脚本来做,但我想先让它在命令行(ubuntu13.04)上运行,以便消除脚本编写过程中可能出现的问题。我应该能够自

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

我想把它作为一个bash脚本来做,但我想先让它在命令行(ubuntu13.04)上运行,以便消除脚本编写过程中可能出现的问题。我应该能够自己处理将其制作成脚本,但如果作为bash脚本提供答案很简单,请这样做。我试着打开设备(
adb-s$deviceID shell
)并运行:

setprop sys.usb.config rndis,adb
这立即将我从设备外壳中踢出,设备不再可访问。如果我运行一个
adb设备
,我会看到手机显示为“无权限”,此时我必须取出USB电缆,然后再次将其插入,并使用
adb kill server
adb start server
重新启动adb服务器。这将不起作用,因为我无法访问手机以进行所需的配置更改


我在谷歌上搜索过,但没有找到任何有成果的东西。有什么建议吗?

必须有root用户才能使用
setprop
更改值,而且我在没有rndis驱动程序的Mac操作系统上,因此无法测试您的USB连接方法。另一种方式,如果您有连接服务(
adb外壳服务列表
):

以下命令在Android 4.3中调用ConnectivityManager.setUsbTethering(布尔启用):

adb外壳su-c服务呼叫连接34 i32 1
开启USB连接

adb外壳su-c服务呼叫连接34 i32 0
关闭USB连接

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

4.4.4: 34
5.1.0: 30
6.0.1: 30
7.0.0: 33
对于Android 5.0+(棒棒糖、棉花糖)使用:

adb外壳su-c服务呼叫连接30 i32 1
打开USB连接

adb外壳su-c服务呼叫连接30 i32 0
关闭USB连接


请记住,这需要root用户。

对于带有Fairphone开放操作系统的Fairphone 2(默认情况下未安装的“没有谷歌的Android”版本),您需要:

  • 启用开发人员模式(默认情况下可能处于激活状态)
  • 搜索“root”设置并启用ADB的root访问
  • 在引号中输入bash命令并使用服务代码31:
    • 启用:
      adb外壳su-c“服务呼叫连接31 i32 1”
    • 禁用:
      adb外壳su-c“服务呼叫连接31 i32 0”

    • 在我的三星设备上,
      服务
      方法对我不起作用。不过,我通过直接配置网络接口找到了实现方法。下面是一个脚本,它设置了一台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
      

      accepted answer中的命令在Oreo上不起作用,因为现在应该是附加参数
      callerPkg
      ,如果将一些随机文本放在那里,它就会起作用

      int setUsbTethering(布尔启用,字符串调用者kg)

      因此,对于8.0/8.1奥利奥:

      服务呼叫连接34 i32 1 s16文本
      -打开USB连接

      服务呼叫连接34 i32 0 s16文本
      -关闭USB连接

      这对我来说很管用

      服务呼叫连接33 i32 1 s16文本
      -打开USB连接


      服务呼叫连接33 i32 0 s16 text
      -关闭USB连接

      您还可以编写输入脚本以启动设置应用程序并勾选复选框,如中所示

      以下是我的脚本(与链接中的脚本大致相同,但稍作修改):

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


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

      Android 4.2果冻豆:


      adb shell su-c服务呼叫连接33 i32 1

      仅供参考,这仅适用于Android 4.x(更可能的是,仅适用于特定版本)。数字“34”是IConnectivityManager.aidl中方法列表中的方法编号。作为参考,RNDIS似乎与USB相同,但它取决于您使用的设备。它是nexus s的RNDIS和galaxy s4的USB。不幸的是,我不再从事此项目,因此无法测试您的解决方案,但它似乎会起作用,因此我接受了您的答案。它记录在哪里?请使用adb中的“服务列表”和“dumpsys活动服务”来了解您的设备知道的服务概况。从这里开始,无论是谷歌还是浏览(AOSP)源代码,因为据我所知,这些都是“官方”记录的
      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