Search code examples
androidadb

How to send fast live input commands to android device using USB debugging?


I want to configure some keys on my pc such that when they are pressed they trigger a specific touch input action on my android device.

Ex:- pressing K means touch input the centre of the screen and so on. Using the mouse to control the screen.

However, there are two problems which I can't solve:-

(1) adb shell is too slow to be used. It has delay of over a second because of the way it works by using java.

I need it to be as fast as possible.

(2) I can't find a way to send live touch input, most of the tools just record the gestures and perform them.


Solution

  • You can achieve this in below steps

    Develop an app which runs as server and listens for your command on a port inside your device

    The app can be invoked from adb shell instrumentation command/service. Some code like below to receive the commands(strings) from your PC and do actions you need.

    public void startServer() throws Exception {
    
        try {
            serverSocket = new ServerSocket(8080);
            CLIENT_SOCKET = serverSocket.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    CLIENT_SOCKET.getInputStream()));
    
            String inputLine;
            // Starting server
            while ((inputLine = in.readLine()) != null) {
                //out(inputLine);
                // do whatever with inputLine, handle touches for 'K'
            }
        } catch (IOException e) {
               //err in connection, handle
        }
    

    forward your local port to port inside adb shell(that is your device port where the app listens)

    adb forward tcp:8080 tcp:8080
    

    Above command forwards local PC port 8080 to 8080 port inside your adb shell device/emulator.

    A client(your PC) side program or script which connects, send commands to local port, which in turn reach the shell

    Example code in python

     import socket
     soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     soc.connect(('127.0.0.1', 8080))
     soc.send('k\n') # this will reach inside the startServer function of app.
    

    Above is just some example code, There will be lot of other complete examples for above steps online.