Android Debug Bridge (ADB for short). This article mainly records how to connect devices to ABD in WLAN network and some common ADB commands, as well as the composition and principle of ADB brief description.

The composition of the ADB

Client: Used to send commands. The client runs on the development machine and can call Android devices from the command line terminal through ADB. Daemons (ADBD) : Used to run commands on Android devices, daemons run as background processes on each device. Server: Used to manage communication between clients and daemons, the server runs as a background process on the development machine.

The principle of the ADB

When an ADB client is started, the client first checks to see if any ADB server processes are running. If not, it starts the server process. When started, the server binds to the local TCP port 5037 and listens for commands from ADB clients, all of which communicate with the ADB server through port 5037. MySQL > MySQL > port 3306

The server then establishes connections with all running devices. It finds emulators by scanning for odd-numbered ports between 5555 and 5585 (the range used by the first 16 emulators). Once the server discovers the ADB daemon (ADBD), it establishes a connection with the appropriate port. Note that each emulator uses a pair of ports in order – an even-numbered port for console connections and an odd-numbered port for ADB connections. Such as:

Simulator 1, console: 5554 Simulator 1, ADB: 5555 Simulator 2, console: 5556 simulator 2, ADB: 5557 and so onCopy the code

As shown above, the simulator that connects to ADB at port 5555 is the same one that the console listens on on port 5554. Once the server is connected to all the devices, you can access them using ADB commands. Because the server manages the connection to the device and handles commands from multiple ADB clients, any device can be controlled from any client (or from a script).

Use WLAN for ADB connection

The premise of using WLAN for ADB connection is still to have a USB cable, but as long as the IP under the WLAN of Android device is unchanged, you can drop the cable. 1. First connect the Android device and adb host to the same WLAN that both can access. Please note that not all access points are applicable; You may need to use an access point whose firewall is properly configured to support ADB.

2. Connect the device to the host using a USB cable.

Set the target device to listen for TCP/IP connections on port 5555:

adb tcpip 5555
Copy the code

4. Unplug the USB cable connected to the target device and find the IP address of the Android device. For example, choose Settings > WLAN Settings > Advanced > IP Address to find the IP address.

5. Connect to the device through the IP address:

adb connect ${device_ip_address}
Copy the code

6. Confirm that the host is connected to the target device:

adb devices -l
Copy the code

If adb connection is disconnected: Ensure that the host is still connected to the same WLAN network as the Android device and reconnect by performing the AdBConnect step again. If the problem is not resolved, you need to reset the ADB host:

adb kill-server
Copy the code

Here is my example of connecting two devices over Wi-Fi:

# adb Devices -l Used to view connected devices changlinzou@mac ~ % ADB Devices -l List of devices attached a9c5d217 device usb:339869696X product:gauguinpro model:M2007J17C device:gauguinpro transport_id:72 # changlinzou@mac ~ % adb tcpip 5555 restarting in TCP mode port: changlinzou@mac ~ % adb connect 10.73.13.237 connected to 10.73.13.237:5555 changlinzou@mac ~ % adb Devices -l List of Devices Attached 10.73.13.237:5555 Device Product: Gauguinpro Model :M2007J17C Device: Gauguinpro transport_id:77 # Connect a Huawei device via USB changlinzou@mac ~ % adb devices -l List of devices Attached PWHNW19709002241 Device USB :339869696X Product :GLK-AL00 Model :GLK_AL00 Device :HWGLK Transport_id :78 10.73.13.237:5555 Device Product: Gauguinpro Model :M2007J17C Device: Gauguinpro transport_id:77 # adb -s You can specify operations to be performed on the device changlinzou@mac ~ % adb -s PWHNW19709002241 shell HWGLK:/ $exit # set TCP/IP connection on port 5557:  changlinzou@mac ~ % adb -s PWHNW19709002241 tcpip 5557 restarting in TCP mode port: 5557 changlinzou@mac ~ % adb devices -l List of devices attached PWHNW19709002241 device usb:339869696X product:GLK-AL00 Model :GLK_AL00 Device :HWGLK Transport_id :79 10.73.13.237:5555 Device Product: Gauguinpro Model :M2007J17C Device: Gauguinpro transport_id:77 # Remove USB connection changlinzou@mac ~ % adb devices -l List of devices Attached 10.73.13.237:5555 Device Product: Gauguinpro Model :M2007J17C Device: Gauguinpro transport_id:77 changlinzou@mac ~ % adb Connect failed to connect to '10.73.14.11:5555': Connection refused changlinzou@mac ~ % ADB connect 10.73.14.11:5557 connected to 10.73.14.11:5557 # changlinzou@mac ~ % ADB devices -l List of Devices Attached 10.73.13.237:5555 Device Product: Gauguinpro Model :M2007J17C Device: Gauguinpro Transport_id :77 10.73.14.11:5557 Device Product :GLK-AL00 Model :GLK_AL00 Device :HWGLK transport_id:80Copy the code

It is also possible to see two devices successfully in AndroidStudio.

ADB common commands

Although it is easier to use the GUI, sometimes we need to use commands to do things for us, so it is better to remember the common ADB commands to look up.

Enable/disable ADB Disable adb kill-server Enable ADB start-server

Adb Devices adb Devices -l

Adb install -r Absolute path to the APk file To uninstall the application ADB Uninstall Name of the application package

Use ADB to read/write phone files Save the phone files to the local ADB pull phone path computer path Pass the local files to the phone ADB push computer path phone path

Enter shell mode adb shell

The following commands are executed inside the shell

List all installed APK packages PM List Packages List all installed third-party APK packages PM List Package-3 List the apK path PM list Package-f based on the package name Dumpsys package package-name Take a screenshot and save the file on the phone /system/bin/screencap -p /sdcard/xx.png Clear the cached data PM clear Com.xxx. XXX specifies the APP to generate random events 100 times