I'm trying to use the /system/bin/ping command on Android to ping the devices on my network. For some reason, it returns exit code 2 for every ping I try. I've tried:www.google.com, 192.168.2.1(router local ip), and currently every possible ip for the devices on the subnet... but it always returns 2. Here's my code.
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String subnet = "192.168.2";
for (int i = 0; i <= 255; i++) {
String node = subnet + "." + i;
System.out.println(pingHost(node));
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
pingHost()
public static int pingHost(String host) throws IOException, InterruptedException {
String cmd = "/system/bin/ping -c 1 -W 1000 " + host;
Process proc = Runtime.getRuntime().exec(cmd);
proc.waitFor();
return proc.exitValue();
}
I've tried with and without a timeout... still the only exit code that prints is 2.
Any ideas?
I printed out the error stream and it told me I needed INTERNET permissions to use ping. I added the INTERNET permissions to my manifest and voila!
Thank you Codebender for the suggestion!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With