【Android】ADB wirelessly connects Android devices

Introduction

Android Debug Bridge , or adb for short , is a versatile command-line tool that allows you to communicate with your device. The adb command can be used to perform various device operations, such as installing and debugging apps, and provides access to a Unix shell that can be used to run various commands on the device. It is a client-server program consisting of the following three components:

  • Client : used to send commands. The client runs on the development machine. You can invoke the client from the command line terminal by issuing the adb command.
  • Daemon (adbd) : used to run commands on the device. The daemon runs as a background process on each device.
  • Server : used to manage communication between clients and daemons. The server runs as a background process on the development machine.

Wireless connection conditions

To use adb to connect devices wirelessly, the following conditions should be met:

  • A computer with Android Studio development tools installed.
  • An Android device that is in the same network segment as the computer (generally, the same network segment can be achieved by connecting to the same wifi and broadband).
  • Use cmd on the computer to execute ping <Android device ip address> and the ping will succeed.

adb connect device

method one

When the conditions mentioned above are met, the connection can be started.

Open cmd.exe or the Terminal tool at the bottom of the Android Studio software and enter the following command:

adb connect <ip地址>:5555

The connected devices can adb devicesbe viewed by executing the command

adb devices

Once the device is connected, you can view and install and debug the device.

A connected device can be adb disconnect <ip地址>:5555disconnected by executing

adb disconnect <ip地址>:5555

The default port of some devices is not 5555 and needs to be modified manually. Please see the modification port number below.

Method Two

Use method two to connect to adb. It should be that the adb connection cannot be connected using method one. In this case, you can use method two to connect.

  1. You need to connect a USB cable (a USB cable that can transmit data) to the Android and the development computer, turn on USB debugging in the developer options, and configure USB as MTP (Multimedia Transfer).
  2. Open the cmd command line window on the computer and enter adb devices. If a device appears, it means that the computer has been connected to the phone through USB.
  3. Then enter two commands in the cmd command line window:
    adb root   <!-- 使用root权限重新启动adbd -->
    adb shell  <!-- 进入到Android系统的shell -->
    
  4. Enter the command and change the device port to 5555. 5555Changing -1it to means turning on the USB debugging function of adb.
    setprop.service.adb.tcp.port 5555
    
  5. Enter the exit command to exit the shell.
  6. Set up the target device to listen on port 5555 for TCP/IP connections.
    adb tcpip 5555
    
  7. Unplug the USB cable and enter connectthe connection command to complete the connection.
    adb connect <ip地址>:5555
    

Modify port number

method one

To modify the port number, use AndroidUtilCode with a star rating of 31.5k on Github . This tool provides related classes to execute cmd commands on Android devices.

Introduction:

implementation 'com.blankj:utilcodex:1.30.1'

Java code:

ShellUtils.CommandResult openAdb = ShellUtils.execCmd("setprop service.adb.tcp.port 5555", AppUtils.isAppRoot());

AndroidUtilCode usage tutorial

Method Two

Connect the device using a data cable and execute the command adb tcpip 5555to reset the port number to 5555.

adb tcpip 5555

After modifying the port number, unplug the data cable and execute the adb command and a connection failure occurs. This means that the modification has failed. You can try the second point in the problem set below to solve the problem.

It is worth noting that Android officially launched an upgraded version of wireless debugging for Android 11+ devices. At that time, you can directly scan the QR code generated by the Android studio development tool to connect, which is simple and convenient.

Auxiliary tools

The prerequisite for using the adb tool is that the device must be connected first

android-tool

android-tool is a program written by Flutter that supports Windows, Mac, and Linux systems. It encapsulates many adb commands into small functions, which greatly improves the efficiency of performing adb-related operations.

Link: Use Flutter to develop a desktop ADB tool application

scrcpy-gui

scrcpy-gui is a tool that can control Android devices on PC without root permissions.

Link: scrcpy-gui

problem set

1. missing port in specification: tcp:192.168.31.209
Reason : Missing port.
Solution : Add the port, the default is 5555

2. cannot connect to 192.168.31.209:5555: Unable to connect because the target computer actively refuses. (10061)
Reason : The port number is incorrect.
Solution : Install an app and use the adb command to configure the port number. It is best to use a data cable to connect the device, execute adb tcpip 5555the command to monitor the TCP/IP connection on port 5555, respond to restarting in TCP mode port: 5555 , unplug the data cable and execute the connection command.

3. What should I do if the ping fails?

Solution :

  • Check whether the pinged IP address is correct, and whether the PC IP address and the mobile IP address are in the same network segment.
  • If it prompts that the target host cannot be accessed, if the IP address and network segment are correct, the mobile device is not rooted yet and you are not allowed to access it. You need to download the
    Insert image description here
    relevant root tools to root the device. If the root is successful, you can ping.
    The device is rooted successfully. If you still cannot ping, please switch the WiFi of the wireless connection and try again.

Other problems have not been encountered yet. If the method provided in this article cannot be solved, please check the reference document below. If you cannot connect, please abandon the solution of using adb wireless connection and use a data cable that can transmit data to connect.

Reference documentation

おすすめ

転載: blog.csdn.net/baidu_41616022/article/details/127617953
おすすめ