Use adb commands to control the screen of Android mobile devices through data cables

Table of contents

Step 1: Download and install Android SDK Platform-Tools

Step 2: Start adb and test the connection

Step 3: Control the phone 


Step 1: Download and install Android SDK Platform-Tools

Go to the Android developer website to find the download address of the ADB toolkit (included in Android SDK Platform-Tools). You can search the Android developer website in your browser to download it.

Here I give the URL I found:

https://developer.android.google.cn/studio/releases/platform-tools?hl=zh-cn

1. After entering the webpage, there will be several tool download links suitable for different platforms, as shown in the figure below. Select the appropriate download. I am using Windows. The following uses Windows as an example. Other systems are similar.

 2. After clicking the download link, the terms will pop up. Scroll to the end and click to agree.

3. After the download is completed, you will get a compressed folder named "platform-tools_r34.0.4-windows" (the version number may be different, it doesn't matter). After decompressing, place the folder in a suitable place. The final operation is It is best not to move the folder location. Enter the "platform-tools" directory as shown in the figure below. You should be able to see the file "adb.exe" and add it to the system environment (you can find the tutorial for configuring the environment. In short, add its folder path. Go to "Settings-System-About-Advanced System Settings-Environment Variables-System Variables-Path"), as shown below.

Step 2: Start adb and test the connection

1. Press and hold the win+R keys, enter "cmd" in the box and press Enter, or search for "cmd" in the search box at the bottom of the computer and open it. Enter "adb" and press Enter. If the previous steps are all correct, adb should be configured into the system environment at this moment, and the version information of the adb tool should be displayed at this moment, as shown in the figure below.

2. Turn on the "USB debugging" button on the mobile phone to be connected to the computer, and connect the computer and mobile phone with a data cable (charging cable). If any pop-up window pops up, in addition to selecting the mode as "Transfer Files", other You can leave it alone.

Then enter the following command in the cmd window:

adb devices

After waiting for a moment, you should be able to see the serial number of the device (which can be used to specify the operation object when connecting multiple devices at the same time later), which indicates that the connection is successful.

Step 3: Control the phone 

1. Now you can control it. I simulated the upward swipe operation of the mobile phone screen.

@echo off
:loop
adb shell input swipe 500 1500 500 100 1000
timeout /t 30 /nobreak >nul
goto loop

Copy the above code, create a new .txt file, paste the above code in, save and exit, change the file suffix to .bat, double-click to run, if you are watching a short video at this time, the phone screen will slide up to the next video (screen Different sizes have different effects, and parameters may need to be adjusted).

The detailed function of the above code is: simulate human hand operation, take the upper left corner of the mobile phone as the reference point, take the positive direction of the x-axis to the right, the positive direction of the y-axis with downwards, and slide from the (x=500, y=1500) point To the (x=500, y=100) point, the duration is 1000 milliseconds (1 second) and executed every 30 seconds.

Therefore, the mobile phone screen will be swiped up every 30 seconds, and each swipe up will be completed in 1 second.

2. If you want to use it in python, you can use the following code with the same effect:

import subprocess
import time

while True:
    subprocess.run(["adb", "shell", "input", "swipe", "500", "1500", "500", "100", "1000"])
    time.sleep(30)

 3. If you connect multiple devices at the same time and want to control multiple devices at the same time, it will not work to run it directly using the above code. The serial number of the controlled device needs to be specified in the code. Taking the device with the device serial number "1c24954c" as an example, the modified code is as follows:

.bat (batch file):

@echo off
:loop
adb -s 1c24954c shell input swipe 500 1500 500 100 1000
timeout /t 30 /nobreak >nul
goto loop

python:

import subprocess
import time

while True:
    subprocess.run(["adb", "-s", "1c24954c", "shell", "input", "swipe", "500", "1500", "500", "500", "1000"])
    time.sleep(30)

Guess you like

Origin blog.csdn.net/m0_64335679/article/details/133352001