我正在尝试调试摩托罗拉Droid上的应用程序,但通过USB连接到设备时遇到一些困难。我的开发服务器是一个在Hyper-V中运行的Windows 7 64位虚拟机,因此我无法在来宾或主机中通过USB直接连接。

我安装了两种不同的USB over TCP解决方案,但连接似乎有问题,因为ADB监视器反复报告“devicemonitor failed to start monitoring”。是否有一种方法可以直接从开发机器上的客户端连接到设备上的守护程序,使用网络而不是USB连接或其他可行的选项?


当前回答

adb可以通过tcp套接字与adb服务器通信。您可以通过telnet进行验证。

$ telnet 127.0.0.1 5037
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
000chost:version
OKAY00040020

通常,命令的格式为%04x%s,带有<message.length><msg>以下是ruby命令witch针对tcp套接字套接字发送adb命令cmd

def send_to_adb(socket, cmd)
  socket.printf("%04x%s", cmd.length, cmd)
end

第一个示例发送命令host:version,其长度为12(十六进制为000c)。您可以享受像framebuffer这样更令人兴奋的命令:它从framebuffer中获取屏幕截图,您可以从其名称中猜到。

其他回答

我需要让USB和TCPIP都为ADB工作(不要问),所以我做了以下工作(使用其他人从xda开发人员发布的指示)

使用adb shell:

su
#Set the port number for adbd
setprop service.adb.tcp.port 5555

#Run the adbd daemon *again* instead of doing stop/start, so there
#are two instances of adbd running.
adbd &

#Set the port back to USB, so the next time ADB is started it's
#on USB again.
setprop service.adb.tcp.port -1

exit

首先,您必须通过USB连接设备

然后将设备连接到WIFI并获取IP地址。当仍然通过usb连接时,在命令行或通过Android Studio终端键入

adb tcpip 5555
adb connect <device IP>:5555

您将看到以下消息:

restarting in TCP mode port: 5555
connected to 172.11.0.16:5555

现在卸下USB电缆,您仍会看到logcat正常

完成。享受

手动流程

从你的设备,如果它是根

根据xda开发人员的一篇帖子,您可以通过以下命令从设备通过Wi-Fi启用ADB:

su
setprop service.adb.tcp.port 5555
stop adbd
start adbd

您可以禁用它,并使用

setprop service.adb.tcp.port -1
stop adbd
start adbd

从计算机,如果您已经有USB访问权限(不需要root)

如果你已经有了USB,切换到使用Wi-Fi更容易。通过USB连接设备的计算机上的命令行发出命令

adb tcpip 5555
adb connect 192.168.0.101:5555

确保将192.168.0.101替换为实际分配给设备的IP地址。完成后,可以通过运行以下命令断开与adb-tcp会话的连接:

adb disconnect 192.168.0.101:5555

您可以通过两种方式查找平板电脑的IP地址:

手动IP发现:

进入Android的WiFi设置,单击操作栏中的菜单按钮(垂直省略号),点击高级,然后在屏幕底部看到IP地址。

使用ADB发现IP:

通过adb执行以下命令:

adb shell ip -f inet addr show wlan0

告诉ADB守护程序返回USB监听

adb usb

自动化流程的应用程序

Google Play上还有几个应用程序可以自动完成这一过程。快速搜索可以找到adbWireless、WiFi ADB和ADB WiFi。所有这些都需要root访问权限,但adbWireless需要更少的权限。

如果您想通过WiFi轻松连接设备以运行、调试或部署Android应用程序,可以使用我开发的开源IntelliJ插件。这里是代码,这里是可以使用的插件。

用法很简单。这里有一张gif图:

我创建了一个批处理文件,用于通过TCP自动启用ADB并将其连接到通过USB连接的设备。有了它,您不必手动输入IP。

@echo off
setlocal

REM Use a default env variable to find adb if possible
if NOT "%AndroidSDK%" == "" set PATH=%PATH%;%AndroidSDK%\platform-tools

REM If off is first parameter then we turn off the tcp connection.
if "%1%" == "off" goto off

REM Set vars
set port=%1
set int=%2
if "%port%" == "" set port=5557
if "%int%" == "" set int=wlan0

REM Enable TCP
adb -d wait-for-device tcpip %port%

REM Get IP Address from device
set shellCmd="ip addr show %int% | grep 'inet [0-9]{1,3}(\.[0-9]{1,3}){3}' -oE | grep '[0-9]{1,3}(\.[0-9]{1,3}){3}' -oE"
for /f %%i in ('adb wait-for-device shell %shellCmd%') do set IP=%%i

REM Connect ADB to device
adb connect %IP%:%port%

goto end

:fail
echo adbWifi [port] [interface]
echo adbWifi off
goto end

:off
adb wait-for-device usb

:end