Search code examples
javaandroid-studioprocesspinglan

How to get ping (in LAN 1 to 255) in Android Studio


I want to get ping from the LAN to see what devices are on my LAN.

This is my code:

TextView info = (TextView) findViewById(R.id.info);
    String alive = "";
    for (int i = 0; i < 256; i++) {
        Process p = Runtime.getRuntime().exec("/system/bin/ping -c 1 192.168.1." + i);
        info.setText("");
        int status = p.waitFor();
        if (status == 0) {
            alive = alive + " - " + i;
            Log.d("Hey", i+"");
        }

This code works correctly but this process is very time consuming.


Solution

  • You're waiting for each ping process to finish before you start the next one.

    Instead, you should make a List<Process> with every ping at once, then wait for them to finish in a separate loop after starting all of them.