简体   繁体   中英

Enable port 5555 for adb network admining programmatically

I was wondering how I can enable port 5555 for daemon adb on android? Basically, I have been setting up a home network with an old router so I can keep all my devices connected. But here's the issue: I don't want to have to connect to USB every time to enable those ports, that defeats the whole purpose.

I was wondering how to forward the port using Java or even JNI programmatically by clicking a button? I have seen the telnetd app do it. So I want to do it myself. How do I achieve this? I have tried one function, here it is:

public void openPort()
    {
        try 
        {
            java.lang.Process process = Runtime.getRuntime().exec("setprop service.adb.tcp.port 5555");
            int exitCode = process.waitFor();
            if (exitCode != 0) 
            {
                throw new java.io.IOException("Command exited with " + exitCode);
            }
            Runtime.getRuntime().exec("adb tcpip 5555");
            Toast.makeText(this, "Listening on port "+ port + "...", Toast.LENGTH_LONG).show();
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
            Toast.makeText(this, "An error has occurred: " + ex, Toast.LENGTH_LONG).show();
            port++;
            openPort();
        }
    }

Now it never reaches the exception, it says opened on the port, but when I go to connect via the network it does not work. So how can I do this?

Keep in mind, that the app was moved to system app with lucky patcher, so it is a system app. If that matters.

(Posted solution on behalf of the OP) .

NOTE: Requires Root.

I changed it up a bit. It works now:

public void openPort()
{
    try 
    {
        String cmds[] = {
            "setprop service.adb.tcp.port 2222",
            "stop adbd",
            "start adbd"
        };
        for (int i = 0; i < cmds.length; i++)
        {
            java.lang.Process process = Runtime.getRuntime().exec(cmds[i]);
            int exitCode = process.waitFor();
            if (exitCode != 0) 
            {
                throw new java.io.IOException("Command exited with " + exitCode);
            }
        }
        Toast.makeText(this, "Listening on port 2222...", Toast.LENGTH_LONG).show();
    } 
    catch (Exception ex) 
    {
        ex.printStackTrace();
        Toast.makeText(this, "An error has occurred: " + ex, Toast.LENGTH_LONG).show();
        openPort();
    }
}

I wrote a simple class to execute Shell commands from App

https://gist.github.com/ricardojlrufino/61dbc1e9a8120862791e71287b17fef8

Comands
String return = Shell.execForResult("ls");

Scripts
Shell.execScript(res.openRawResource(R.raw.cpu_script));

Start ADB
Shell.startADB(5555);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM