Ever wondered how screen mirroring tools that allow you to control a mobile screen from a PC actually work? Or how do you back up your personal data if your mobile screen is broken?
Hi developer, I'm Hemanta. In this tutorial, you will build your own android mobile screen mirroring tool from scratch using ADB (Android Debug Bridge). By the end, you will have a working model that will easily allow you to control your mobile screen from a computer— and you will understand how it works.
This project is inspired by the Android command-line tools, which are part of the Android Studio. (Note: I recommend that you use an old Android mobile. If your PC provides high performance, then you can also use an Android emulator such as BlueStacks or Box Player.)
📱 You can find the code for the Android Screen Mirroring Tool on Github.
Table of Contents
- Prerequisites
- Why ADB for Screen Mirroring?
- How do you connect an Android mobile to a computer for ADB?
- How to Set Up Your Environment?
- How to Run ADB Command With Python?
- How to Fast Your Old Phone Using ADB?
- How to Create a Screen Mirroring Tool Interface
- How to control Android phone from PC
What you will learn
- How phone farms actually work
- The insights behind the ADB tool
- Creating a connection between a mobile and computer
- Complete concept of mobile automation
Imagine you have an old mobile phone. Maybe the phone's screen is broken. Your mind immediately recalls YouTube videos about crypto mining that use many mobile phones. For several years, phone farms involving crypto mining and bot subscriptions using old mobile phones have gained popularity.
These farms work by linking multiple mobile phones to one computer. This setup allows many tasks to be performed at once with a single click. For example, they can easily give many likes and followers to TikTok videos with just one click. I do not cheer for such activities, because there is a possibility that the account may get banned or deleted.
However, automating old phones with a PC to perform crypto mining can be a good idea.
Prerequisites
You should have a basic familiarity with Python and some understanding of the PowerShell command line. If you are completely new or just starting to learn, don't worry— I will try to explain all the steps in detail.
Why ADB for Screen Mirroring?
Before diving into the code, let's understand why ADB (Android Debug Bridge) is particularly well-suited for this task.
Python is a programming language, so it cannot directly control a mobile device on its own. There are some Python libraries for mobile automation, such as Appium or uiautomator2.
Now you might think that these libraries can control the mobile device. 😅Haha, lol! These libraries actually use ADB to perform mobile control tasks. Therefore, in any step of your mobile control or automation, ADB is required.
Why Choose ABD?
- Control: Android devices can be controlled directly from a computer.
- Speed: It creates incredibly fast communication between mobile devices and PCs—even wirelessly.
- Simplicity: With just a few simple commands run through Python, you can manage devices and automate tasks.
- Performance: It's used not only for screen mirroring, but also mainly for debugging, app testing, file management, and device control.
Installing ADB with Chocolatey
In a previous post, I explained how to install Chocolatey (Windows package manager) and how to use it. With Chocolatey, you can install adb by running a single command in PowerShell.
If you have already installed Chocolatey, installing ADB is very easy.
Step 1: Install ADB with ChocolateyOpen PowerShell as Administrator (Win + X) and run:
This command will automatically download and install ADB on your system.
Step 2: Verify the InstallationAfter installation, confirm that ADB is working correctly by running the following command in Command Prompt or PowerShell:
If the installation was successful, this command will display the installed ADB version.
Installing ADB with Manually
If installing ADB with Chocolatey seems complicated, I will show you the steps to manually install ADB on Windows.
Step 1: Download Platform Tools (ADB)- Go to the SDK Platform Tools page on the official Android developer site.
- Download the
platform-tools-latest-windows.zipfile for Windows. - Extract it to a simple folder, such as:
C:\platform-tools
Check if the adb.exe file is in the folder.
- Press Windows + S, type "Environment Variables" to open Edit the system environment variables.
- Click the Environment Variables... button.
- Select Path from System variables and go to Edit → New → Add:
C:\platform-tools - Close all dialogs with OK.
After adding path to the environment variables, run adb version in the command panel to verify that it's working.
How do you connect an Android mobile to a computer for ADB?
Many people ask the question, do I need to root my phone for this tasks? To use basic commands with ADB, like taking screenshots, opening apps, or tapping on the screen, you don't need to root the phone. Rooting is not required for this tasks. (Note: Rooting your phone can cause security problems, app crashes, or license issues. Therefore, I would never recommend that you root your device. Rooting is a big step that beginners should not attempt unless it's completely necessary.)
For today's task, you only need to change a few settings on your phone. I would like you to do this task on an extra or old phone at home.
I am showing the settings from my old Realme 6 phone that runs Android 9. However, although the settings may look different on each Android phone, the options are almost the same.
Step 1: Enable Developer Options on Your Android Phone- Go to Settings → About Phone
- Tap the Build Number 7 times
- Developer Options will now be enabled
Step 2: Turn on USB Debugging
- Settings → Additional Settings → Developer Options
- Turn on USB Debugging
Step 3: Connect your phone to the computer using a USB cable.
- Plug your android phone into the PC using the USB cable.
- A popup will appear on your phone:
Allow USB debugging?
- ✔ Tap 'Allow'.
Now let's build our android screen mirroring tool.
How to Set Up Your Environment?
First, install the necessary Python libraries and module. Open your VS terminal, or run this in a Command Prompt:
Here’s a quick summary of what each module does:
subprocess— ADB commands to run directly from Python scriptstkinter— for creating window interfacespillow(PIL) — to load a screenshot inside the Tkinter interfaceio— for converting screenshot data into images
Once installation is complete, import everything at the top of your VS Code or CodeEditer. Keeping all imports at the starting of the file is considered a Python best practice - because it keeps your code organized.
How to Run ADB Command With Python?
ADB runs on the command prompt and PowerShell. To execute commands using Python, you need the subprocess module. So, at the beginning time, create a method that makes it easy to send ADB commands from a computer to an Android device and output back.
ADBHelper class will use the Android Debug Bridge (ADB) to execute multiple command methods.
run()method executes ADB commands using the subprocess module.screenshot()method to take a screenshot of the phone. This runs theadb exec-out screencap -pcommand and returns the screenshot data, which we'll later add to the Tkinter interface.tap()method to tap a specific location on the phone screen. Here, theadb shell input tap x ycommand is sent, where x and y are the screen locations.
Let's do a test to verify that ADB is installed correctly.
adb = ADBHelper()
# check adb
if adb.run("version") is None:
print("ADB not installed")
exit()
Output: If running the command adb.run("version") empty return, that mean ADB is either not installed or is not included in the PATH.
How to Fast Your Old Phone Using ADB?
Older phones slow down over time. Many people think that the phone's hardware power decreases or that companies intentionally slow down phones. But in reality, each new Android update has more features that exceed the capacity of an older phone's RAM and CPU.
While you cannot increase your phone RAM or CPU using ADB, you can slightly reduce the CPU load by executing a few simple commands.
- uninstall unused applications
adb shell pm uninstall --user 0 com.package.name
- delete cache and junk data
adb shell cmd package trim-caches 999G
- disable animations
adb shell settings put global window_animation_scale 0
adb shell settings put global transition_animation_scale 0
adb shell settings put global animator_duration_scale 0
- manage battery power
Show phone RAM with Python
The ADB command adb shell cat /proc/meminfo checks the phone's memory. This method to print the phone's memory using Python.
def get_ram_full():
result = adb.run("shell cat /proc/meminfo")
mem = {}
for line in result.split("\n"):
parts = line.split()
if len(parts) >= 2:
mem[parts[0].replace(":", "")] = int(parts[1])
total = mem.get("MemTotal", 0) / (1024 * 1024)
free = mem.get("MemAvailable", 0) / (1024 * 1024)
used = total - free
print(f"Total RAM: {total:.2f} GB")
print(f"Used RAM: {used:.2f} GB")
print(f"Free RAM: {free:.2f} GB")
cached = mem.get("Cached", 0) / (1024 * 1024)
buffers = mem.get("Buffers", 0) / (1024 * 1024)
print(f"Cached RAM: {cached:.2f} GB")
print(f"Buffers: {buffers:.2f} GB")
get_ram_full()
Output:
Total RAM: 2.76 GB
Used RAM: 2.10 GB
Free RAM: 0.66 GB
Cached RAM: 0.62 GB
Buffers: 0.02 GB
How to Create a Screen Mirroring Tool Interface?
Now, let's move on to the exciting part—displaying our phone screen on the computer. First, use the command adb shell wm size to get the phone's screen size and split() function separates the resolution values.
reduce_size variable is used to scale down the resolution. Then create a GUI window - whose size is easily fit on the computer screen.
update_image() function takes a screenshot of the phone screen using adb through the screencap command. After that, it converts the screenshot into an image using the Pillow (PIL) library and displays it in the Tkinter label.
The mobile screen display is not live, it's just a screenshot. However, it's updated every 5 seconds. If we reduce the delay to 1 second, the program may crash.
Now you might be wondering— why we aren't using screen recording? The reason is quite complex and relates to the load placed on the CPU. The live screen is recorded in the H.264 format. It's possible to use scrcpy for the live screen. In that case, we could no longer say that this tool was built using Python.
How to control Android phone from PC?
Now you are going to see how to control a mobile screen. Using ADB commands adb shell input tap x y and adb shell input swipe x1 y1 x2 y2, you can tap and drag actions on the mobile screen. If you connect these commands with mouse click events from a Tkinter label, you will be able to control the mobile screen using a computer.
tap_event()sends the place where you click with the mouse as a tap on the mobile.drag_start()remembers where the drag started.drag_end()checks whether it's a tap or a swipe when the drag ends, and then performs the action accordingly.
Next Steps
Want to improve this Android Screen Mirroring Tool? Here are some ideas:
- User interface more futures: You can add more options like the mobile power button and volume buttons.
- Add keyboard input support: Provide the ability to type on the mobile using the computer’s keyboard—this will make tasks like writing messages or searching much faster.
- Live screen show: I know this is not easy to do with Python, but still try to find a way.
- Add wireless connection support (Wi-Fi): Allow the mobile to connect to the computer using Wi-Fi without a USB cable.
Conclusion
You have built an Android Screen Mirroring tool that can control and display a mobile screen from a computer using Python and ADB. Through this process, you have gained practical knowledge of using ADB commands, managing device connections, and handling real-time screen data.
The real strength of this approach is its flexibility and potential for customization. With relatively small code, you have created an effective tool capable of performing complex tasks like screen mirroring, input control, and device info.
Now you can easily improve this project by adding Wi-Fi connection support, keyboard and mouse input, or making the UI more attractive.
Don’t hesitate to experiment with the code. Try adding new features, test different methods, and see how performance and usability change. This is the best way to deepen and strengthen your skills.





0 Comments