Why does ping works on some devices and not others?
Asked Answered
L

2

13

I have the following code in my app:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
return process.waitFor();

I see that it works (i.e. returns 0) on some devices (e.g. Motorola G) but doesn't work (i.e. returns 2) on other devices (e.g. Galaxy S3). I've checked the Galaxy S3 device and it definitely has a "/system/bin/ping" file. I've also made sure that it is connected to the internet.

Does anyone have any ideas why the command might not work on some devices and what I can do to get it to work?

Liquescent answered 23/9, 2015 at 21:5 Comment(4)
Did you test it on rooted devices?Draft
No. I'm looking for a way to get this to work on non-rooted devices.Liquescent
any solution you get ?Maccabean
Punit, see here: adilatwork.blogspot.co.uk/2015/09/…Liquescent
G
27

The way ping is implemented for every OEM (Original Equipment Manufacturer) may be different. This is not only limited to ping. For example, try running the linux du command on old Samsung devices (S3) and newer devices (Motorola G) on adb shell.

The S3 does support du, but it does not take some parameters such as -m (display in megabytes), while newer devices such as Moto G or even Samsung devices (past Galaxy S5) does support it. Try running ping -c 1 8.8.8.8 on adb shell within S3, and you'll note that it's not your Java code, it's the OEM.

Again, even commands such as dumpsys have differently formatted output depending on Android Version and device. Because of this, it would be best if you can use a completely different method to try pinging the desired IP using APIs provided in Android instead of using a process with parameters that might be differently supported per OEM.

It can also be that the permissions are different per device.

When you run a process within your app, you only are granting the app's limited permissions to the process. So if ping requires root level permission (on the specific device), that may also be the reason why it does not work. It is ultimately up to the OEM of how they customized Android.

Source: I work at an OEM.

Garin answered 26/9, 2015 at 9:49 Comment(4)
Hey Can you explain me how to run "ping -c 1 8.8.8.8 on adb shell" as i am facing the same problem. ping on working on 4.2.2 device (if works take more time then 25000ms) how can i solve this ??Maccabean
Punit, the ping command cannot be reliably called on all Android devices.Liquescent
then how to resolve this issue as i am using this line "runtime.exec("ping -c 1 " + url)" but its not working for 4.2.2 devices and same working quite good for other devicesMaccabean
Hello Sir, Is there any way to execute this ping ( " ping -c 1 -s 1024 hostAddress" ) command in all devices right now ? "Sheath
C
-3

Solved: need to make the phone aware is on a lan network. Install a whatever network management app "Ip Tools" and click on "Lan scanner" you will see all the other devices connected in the network, and now check again ping will works!

Cedilla answered 28/4, 2021 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.