如何使用 ADB 通过 TCP 连接到 Android?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2604727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:31:46  来源:igfitidea点击:

How can I connect to Android with ADB over TCP?

androidnetworkingtcpdebuggingadb

提问by JDM

I am attempting to debug an application on a Motorola Droid, but I am having some difficulty connecting to the device via USB. My development server is a Windows 7 64-bit VM running in Hyper-V, and so I cannot connect directly via USB in the guest or from the host.

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

I installed a couple of different USB-over-TCP solutions, but the connection appears to have issues since the ADBmonitor reports "devicemonitor failed to start monitoring" repeatedly. Is there a way to connect directly from the client on the development machine to the daemon on the device using the network instead of the USB connection or possibly another viable options?

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

采纳答案by JDM

I ended up getting the Eltima USB to Ethernet software working after finally giving up on the possibility of a direct to device connection over TCP. I have pretty much decided that it is not possible to connect to a device across the network only an emulator.

在最终放弃通过 TCP 直接连接到设备的可能性之后,我最终让 Eltima USB 转以太网软件工作。我几乎已经决定不可能仅通过模拟器连接到网络上的设备。

回答by Brian

Manual Process

手动流程

From your device, if it is rooted

从您的设备,如果它已植根

According to a post on xda-developers, you can enable ADB over Wi-Fi from the device with the commands:

根据xda-developers 上的帖子,您可以使用以下命令从设备通过 Wi-Fi 启用 ADB:

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

And you can disable it and return ADB to listening on USB with

您可以禁用它并返回 ADB 以在 USB 上收听

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

From a computer, if you have USB access already (no root required)

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

It is even easier to switch to using Wi-Fi, if you already have USB. From a command line on the computer that has the device connected via USB, issue the commands

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

adb tcpip 5555
adb connect 192.168.0.101:5555

Be sure to replace 192.168.0.101with the IP address that is actually assigned to your device. Once you are done, you can disconnect from the adb tcp session by running:

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

adb disconnect 192.168.0.101:5555

You can find the IP address of a tablet in two ways:

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

Manual IP Discovery:

手动 IP 发现:

Go into Android's WiFi settings, click the menu button in the action bar (the vertical ellipsis), hit Advancedand see the IP address at the bottom of the screen.

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

Use ADB to discover IP:

使用 ADB 发现 IP:

Execute the following command via adb:

通过 adb 执行以下命令:

adb shell ip -f inet addr show wlan0

To tell the ADB daemon return to listening over USB

告诉 ADB 守护进程返回通过 USB 侦听

adb usb

Apps to automate the process

自动化流程的应用程序

There are also several apps on Google Play that automate this process. A quick search suggests adbWireless, WiFi ADBand ADB WiFi. All of these require root access, but adbWirelessrequires fewer permissions.

Google Play 上还有几个应用程序可以自动执行此过程。快速搜索建议adbWirelessWiFi ADBADB WiFi。所有这些都需要 root 访问权限,但adbWireless需要较少的权限。

回答by norman

This is really simple if your phone is rooted.

如果您的手机已植根,这真的很简单。

Download a terminal emulator from Google Play(there are lots that are free). Make sure that your Android device is connected to your Wi-Fi and get the Wi-FiIP address. Open the terminal program and type:

Google Play下载终端模拟器(有很多是免费的)。确保您的 Android 设备已连接到您的 Wi-Fi 并获取Wi-FiIP 地址。打开终端程序并输入:

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

Now go to your computer (assuming that you are using Windows) and create a shortcut on the desktop for "cmd.exe" (without the quotations).

现在转到您的计算机(假设您使用的是 Windows)并在桌面上为“cmd.exe”创建一个快捷方式(不带引号)。

Right click on the cmd shortcut and choose "Run as Administrator"

右键单击cmd快捷方式并选择 "Run as Administrator"

Change to your android-sdk-windows\toolsfolder

更改到您的android-sdk-windows\tools文件夹

Type:

类型:

adb connect ***wifi.ip.address***:5555

(example: adb connect 192.168.0.105:5555)

adb should now say that you are connected.

adb 现在应该说您已连接。

Note:if you are too fast to give the connect command it may fail. So try at least two times five seconds apart before you say this doesn't work.

注意:如果你太快而无法发出连接命令,它可能会失败。所以在你说这不起作用之前至少尝试两次间隔五秒。

回答by Ribin Haridas

  1. Connect device via USB and make sure debugging is working, then run:

    adb tcpip 5555
    adb connect <DEVICE_IP_ADDRESS>:5555
    
  2. Disconnect USB and proceed with wireless debugging.

  3. When you're done and want to switch back to USB debugging, run:

    adb -s <DEVICE_IP_ADDRESS>:5555
    
  1. 通过 USB 连接设备并确保调试正常,然后运行:

    adb tcpip 5555
    adb connect <DEVICE_IP_ADDRESS>:5555
    
  2. 断开 USB 并继续进行无线调试。

  3. 当你完成并想切换回 USB 调试时,运行:

    adb -s <DEVICE_IP_ADDRESS>:5555
    

To find the IP address of your device, go to Settings > Wi-Fi > Advanced > IP Addresson your device or run adb shell netcfg.

要查找设备的 IP 地址,请转到Settings > Wi-Fi > Advanced > IP Address设备上的 或运行adb shell netcfg

No root required. Only one device can be debugged at a time.

无需root。一次只能调试一台设备。

See this XDA post.

请参阅此 XDA 帖子

The adbcommand is located in the platform-toolsfolder of the Android SDK.

adb命令位于platform-toolsAndroid SDK的文件夹中。

回答by MapleLover

Assume you saved adb path into your Windows environment path

假设您将 adb 路径保存到您的 Windows 环境路径中

  1. Activate debug mode in Android

  2. Connect to PC via USB

  3. Open command prompt type: adb tcpip 5555

  4. Disconnect your tablet or smartphone from pc

  5. Open command prompt type: adb connect IPADDRESS(IPADDRESS is the DHCP/IP address of your tablet or smartphone, which you can find by Wi-Fi -> current connected network)

  1. 在 Android 中激活调试模式

  2. 通过 USB 连接到 PC

  3. 打开命令提示符类型: adb tcpip 5555

  4. 断开平板电脑或智能手机与 PC 的连接

  5. 打开命令提示符类型:adb connect IPADDRESS(IPADDRESS 是平板电脑或智能手机的 DHCP/IP 地址,您可以通过 Wi-Fi -> 当前连接的网络找到)

Now in command prompt you should see the result like: connected to xxx.xxx.xxx.xxx:5555

现在在命令提示符中,您应该看到如下结果:connected to xxx.xxx.xxx.xxx:5555

回答by Nathan

From adb --help:

来自adb --help

connect <host>:<port>         - Connect to a device via TCP/IP

That's a command-line option by the way.

顺便说一下,这是一个命令行选项。

You should try connecting the phone to your Wi-Fi, and then get its IP address from your router. It's not going to work on the cell network.

您应该尝试将手机连接到 Wi-Fi,然后从路由器获取其 IP 地址。它不会在蜂窝网络上工作。

The port is 5554.

端口是 5554。

回答by Raghav Thakkar

For Windows users:

对于 Windows 用户:

Step 1:
You have to enable Developer options in your Android phone.
You can enable Developer options using this way.
? Open Settings> About> Software Information> More.
? Then tap “Build number” seven times to enable Developer options.
? Go back to Settings menu and now you'll be able to see “Developer options” there.
? Tap it and turn on USB Debugging from the menu on the next screen.

第 1 步:
您必须在 Android 手机中启用开发人员选项。
您可以使用这种方式启用开发人员选项。
? 打开设置>关于>软件信息>更多。
? 然后点击“内部版本号”七次以启用开发人员选项。
? 返回设置菜单,现在您将能够在那里看到“开发人员选项”。
? 点击它并从下一个屏幕的菜单中打开 USB 调试。

Step 2:

第2步:

Open cmd and type adb.
if you find that adb is not valid command then you have to add a path to the environment variable.

打开cmd并输入adb。
如果您发现 adb 不是有效命令,则必须添加环境变量的路径。

?First go to you SDK installed folder
Follow this path and this path is just for an example. D:\softwares\Development\Andtheitroadd\SDK\sdk\platform-tools\; D:\softwares\Development\Andtheitroadd\SDK\sdk\tools;
? Now search on windows system advanced setting

?首先到你的SDK安装文件夹
按照这个路径,这个路径只是一个例子。D:\softwares\Development\Andtheitroadd\SDK\sdk\platform-tools\; D:\softwares\Development\Andtheitroadd\SDK\sdk\tools;
? 现在搜索windows系统高级设置

? enter image description here

? 在此处输入图片说明

Open the Environment variable.

打开环境变量。

enter image description here

在此处输入图片说明

then open path and paste the following path this is an example.
You SDK path is different from mine please use yours. D:\softwares\Development\Andtheitroadd\SDK\sdk\platform-tools\;
D:\softwares\Development\Andtheitroadd\SDK\sdk\tools;

然后打开路径并粘贴以下路径这是一个示例。
您的 SDK 路径与我的不同,请使用您的。D:\softwares\Development\Andtheitroadd\SDK\sdk\platform-tools\;
D:\softwares\Development\Andtheitroadd\SDK\sdk\tools;

enter image description here

在此处输入图片说明

Step 3:

第 3 步:

Open cmd and type adb. if you still see that adb is not valid command then your path has not set properly follow above steps.

打开cmd并输入adb。如果您仍然看到 adb 无效命令,那么您的路径设置不正确,请按照上述步骤操作。

enter image description here

在此处输入图片说明

Now you can connect your android phone to PC.

现在您可以将您的 Android 手机连接到 PC。

Open cmd and type adb devices and you can see your device. Find you phone ip address.

打开 cmd 并输入 adb devices ,您可以看到您的设备。 找到你的电话IP地址。

enter image description here

在此处输入图片说明

Type:- adb tcpip 5555

类型:- adb tcpip 5555

enter image description here

在此处输入图片说明

Get the IP address of your phone

获取手机的IP地址

adb shell netcfg

Now,

现在,

adb connect "IP address of your phone"

Now run your android project and if not see you device then type again adb connect IP address of your phone

现在运行你的 android 项目,如果没有看到你的设备,然后再次输入你手机的 adb 连接 IP 地址

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

For Linux and macOS users:

对于 Linux 和 macOS 用户

Step 1: open terminal and install adb using

第 1 步:打开终端并使用安装 adb

sudo apt-get install android-tools-adb android-tools-fastboot

须藤 apt-get 安装 android-tools-adb android-tools-fastboot

Connect your phone via USB cable to PC. Type following command in terminal

通过 USB 数据线将您的手机连接到 PC。在终端输入以下命令

adb tcpip 5555

Using adb, connect your android phone ip address.

使用 adb,连接您的 android 手机 IP 地址。

Remove your phone.

取出手机。

回答by Zoe

From a computer on a non-rooted device

从无根设备上的计算机

(Note that this can be done using a rooted device as well, but you can use a shell on a rooted device which doesn't require a USB connection)

(请注意,这也可以使用 root 设备完成,但您可以在不需要 USB 连接的 root 设备上使用 shell)

Firstly, open command prompt (CMD). If you use Android Studio or IntelliJ there is a console included in there you can use.

首先,打开命令提示符(CMD)。如果您使用 Android Studio 或 IntelliJ,则可以使用其中包含的控制台。

If you have adb added to the path, you can skip the cd part.

如果您在路径中添加了 adb,则可以跳过 cd 部分。



If possible, open the SDK location, right click, and press "start command prompt here". Not all have this option so you have to do this (/these) commands as well:

如果可能,打开 SDK 位置,右键单击,然后按“在此处启动命令提示符”。并非所有人都有此选项,因此您也必须执行此(/这些)命令:

Windows: change the drive (if applicable)

Windows:更换驱动器(如果适用)

D: 

And access the sdk and platform tools. Replace this path with your SDK location:

并访问 sdk 和平台工具。将此路径替换为您的 SDK 位置:

cd /sdk/path/here/platform-tools

Now you have access to the Android debug bridge.

现在您可以访问 Android 调试桥。



With the device connected to the computer, do:

将设备连接到计算机后,执行以下操作:

adb tcpip <port> 
adb connect <ip>:<port>

Where <port>is the port you want to connect to (default is 5555) and <ip>is the IP of the device you want to connect to.

<port>您要连接的端口在哪里(默认为5555),<ip>是您要连接的设备的 IP。

Please note: 5555is the default port and just writing the IP address connects it. If you use a custom port you can at least improve the security a bit. USB debugging over Wi-Fi can be abused, but only if the device is connected to the computer who wants to abuse the device. Using a non-default port at least makes it a bit harder to connect.

请注意:5555是默认端口,只需写入 IP 地址即可连接它。如果您使用自定义端口,您至少可以提高一点安全性。可以滥用通过 Wi-Fi 进行的 USB 调试,但前提是该设备已连接到想要滥用该设备的计算机。使用非默认端口至少会使连接变得有点困难。

If you use a custom port, make sure to add it after the IP. Writing no port connects to 5555and if you didn't use that the connection will fail.

如果您使用自定义端口,请确保将其添加到 IP 之后。写入没有连接到的端口5555,如果您没有使用连接将失败。

You can find the IP address of a device in two ways:

您可以通过两种方式找到设备的 IP 地址:

  • Depending on your device, the exact names may vary. Open settings and go to About device-> Status-> IP address

  • Use ADB to get the IP

  • 根据您的设备,确切的名称可能会有所不同。打开设置并转到关于设备->状态-> IP 地址

  • 使用ADB获取IP

From the console, do:

从控制台,执行:

adb shell ip -f inet addr show wlan0


And once you are finished with the connection, you can disconnect the device from your computer by doing:

完成连接后,您可以通过执行以下操作将设备与计算机断开连接:

adb disconnect <ip>:<port>

Or no IP to disconnect all devices. If you used a custom port, you must specify which port to disconnect from. The default is 5555 here as well.

或者没有 IP 断开所有设备。如果您使用自定义端口,则必须指定要从哪个端口断开连接。此处的默认值也是 5555。

To disable the port (if that is something you want to do) you do this command with the device connected:

要禁用端口(如果这是您想要做的事情),请在连接的设备上执行以下命令:

adb usb

Or you can restart the device to remove the tcpip connection

或者您可以重新启动设备以删除 tcpip 连接

From a computer on a rooted device

从有根设备上的计算机

Firstly, you need access to the shell. You either connect the device using a usb cable and use adb shellor download an app from Google Play, FDroid, or some other source.

首先,您需要访问shell。您可以使用 USB 电缆连接设备,然后使用adb shell或从 Google Play、FDroid 或其他来源下载应用程序。

Then you do:

然后你做:

su
setprop service.adb.tcp.port <port>
stop adbd
start adbd

And to connect the device, you do as in the non-rooted version by doing adb connect <ip>:<port>.

要连接设备,您可以通过执行adb connect <ip>:<port>.

And if you want to disable the port and go back to USB listening:

如果您想禁用端口并返回 USB 侦听:

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


You can also use an Android Studio plugin to do it for you (don't remember the name right now), and for rooted users there's also the option of downloading an Android app to set up the phone connection (adb connect is probably still required).

您还可以使用 Android Studio 插件为您完成此操作(现在不记得名称了),对于 root 用户,还可以选择下载 Android 应用程序来设置电话连接(可能仍然需要 adb connect )。

Some phones have a setting in developer options (this applies to someunrooted phones, though probably some rooted phones too) that allows for toggling ADB over Wi-Fi from the device itself without root or a computer connection to start it. Though there are few phones that have that

一些手机在开发者选项中有一个设置(这适用于一些无根手机,但也可能是一些有根手机),允许在没有根或计算机连接的情况下从设备本身通过 Wi-Fi 切换 ADB 来启动它。虽然很少有手机有这个

回答by transistor1

I needed to get bothUSB and TCPIP working for ADB(don't ask), so I did the following (using directions others have posted from xda-developers)

我需要得到两个USB和TCPIP的工作ADB(不要问),所以我做了以下(使用方向其他人从XDA开发转贴)

Using adb shell:

使用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

回答by rinkesh

To connect your tablet using TCP port. Make sure your system and device is connected to same network.

使用 TCP 端口连接平板电脑。确保您的系统和设备连接到同一网络。

  1. Open console cmd.exe
  2. Type adb tcpip 5555
  3. Go to System -> Development option -> USB debugging--> Uncheck it for TCPIP connection
  4. Type adb connect 192.168.1.2this is your device IP address
  5. Connected to 192.168.1.2
  1. 打开控制台cmd.exe
  2. 类型 adb tcpip 5555
  3. 转到系统 -> 开发选项 -> USB 调试--> 取消选中 TCPIP 连接
  4. 输入adb connect 192.168.1.2这是您的设备 IP 地址
  5. 连接到 192.168.1.2

Connected using port forwardTry to do port forwarding,

使用端口转发连接尝试做端口转发,

adb forward tcp:<PC port>tcp:<device port>

adb 转发 tcp: <PC port>tcp:<device port>

like:

喜欢:

adb forward tcp:5555 tcp:5555.

C:\Users\abc>adb forward tcp:7612 tcp:7612

C:\Users\abc>adb tcpip 7612 restarting in TCP mode port: 7612

C:\Users\abc>adb connect 10.0.0.1:7612

connected to 10.0.0.1:7612

adb 转发 tcp:5555 tcp:5555。

C:\Users\abc>adb 转发 tcp:7612 tcp:7612

C:\Users\abc>adb tcpip 7612 在 TCP 模式下重启端口:7612

C:\Users\abc>adb 连接 10.0.0.1:7612

连接到 10.0.0.1:7612

If you get message error: device not foundconnect a usb device to system then follow same procedure.
for a rooted device

如果您收到消息错误:找不到设备将 USB 设备连接到系统,然后按照相同的步骤操作。
对于有根设备

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

回答by Christoph

I do not know how to connect the device without any USB connection at all, but if you manage to connect it maybe at another computer you can switch the adbd to TCP mode by issuing

我不知道如何在没有任何 USB 连接的情况下连接设备,但是如果您设法在另一台计算机上连接它,您可以通过发出将 adbd 切换到 TCP 模式

adb tcpip <port>

from a terminal and connect to your device over wifi from any PC on the network by:

从终端并通过 wifi 从网络上的任何 PC 通过以下方式连接到您的设备:

adb connect <ip>:<port>

Maybe it is also possible to switch to TCP mode from a terminal on the device.

也许也可以从设备上的终端切换到 TCP 模式。