Thursday, 25 November 2021

Android get route info programmatically

I am sending UDP packets to a remote, and I would like to know the IP of the local interface that will be used for that. In a shell I would run:

ip route get 192.168.1.54

and it would answer something like:

192.168.1.12 dev wlan0  table local_network  src 192.168.1.1

Where I'm interested in getting the src: 192.168.1.1.

I can do it by calling exec like this:

Runtime.getRuntime().exec("ip route get 192.168.1.54")

but that does not seem very elegant and nothing tells me it will work on all Android devices.

Is there a way to do that with the Android SDK? I looked at the ConnectivityManager but could not find a way...



from Android get route info programmatically

No comments:

Post a Comment