7

How can you find the IP address of the router (gateway address) from code?

WifiInfo.getIpAddress() - returns IP address of device.

In a shell command "ipconfig" does not return any value.

Here is my solution, but please let me know if there is a better way to do this:

WifiManager manager = (WifiManager)getSystemService(WIFI_SERVICE);
DhcpInfo info = manager.getDhcpInfo();
info.gateway;
jww
  • 97,681
  • 90
  • 411
  • 885
HotIceCream
  • 2,271
  • 2
  • 19
  • 27
  • possible duplicate of [How to get gateway and subnet mask details in Android? programmatically ](http://stackoverflow.com/questions/5387036/how-to-get-gateway-and-subnet-mask-details-in-android-programmatically) – slayton Jan 27 '12 at 17:06
  • 3
    `ipconfig` is a windows command. The linux command is `ifconfig` with an F. Android doesn't seem to this either and uses `netcfg` – slayton Jan 27 '12 at 17:07
  • Of course, I mean `ifconfig`. `netcfg` return ip of device:( – HotIceCream Jan 29 '12 at 15:19
  • Normally, the IP address of "the router" is not something you know. It's also not very well-defined, do you mean your default gateway? – unwind Jan 27 '12 at 15:35
  • yes, i want gateway. In ubuntu i find it: route -n – HotIceCream Jan 27 '12 at 15:45

4 Answers4

15

Hey this might help you: DHCPInfo

final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);

Add following rows to AndroidManifest.xml in order to access wifi functionalities:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

As the formatIpAddress is deprecated now you can use below code

byte[] myIPAddress = BigInteger.valueOf(manager.getIpAddress()).toByteArray();
ArrayUtils.reverse(myIPAddress);
InetAddress myInetIP = InetAddress.getByAddress(myIPAddress);
String myIP = myInetIP.getHostAddress();
Sandeep
  • 1,814
  • 21
  • 25
  • What if I have given Static IP & disabled DHCP, in that case I am getting 0.0.0.0 for (dhcp.serverAddress). – ravz Dec 08 '14 at 06:14
  • Formatter.formatIpAddress() [has been deprecated](https://developer.android.com/reference/android/text/format/Formatter.html#formatIpAddress(int)). – Jk Jensen Jun 01 '17 at 02:48
1

I think the way you're doing it is the best (AFAIK), here's some example code from a Cordova plugin that does it the same way:

public class GetRouterIPAddress extends CordovaPlugin {

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        try {
            String ip = getRouterIPAddress();
            if (ip.equals("0.0.0.0")) {
                callbackContext.error("No valid IP address");
                return false;
            }
            callbackContext.success(ip);
            return true;
        } catch(Exception e) {
            callbackContext.error("Error while retrieving the IP address. " + e.getMessage());
            return false;
        }
    }

    private String formatIP(int ip) {
        return String.format(
            "%d.%d.%d.%d",
            (ip & 0xff),
            (ip >> 8 & 0xff),
            (ip >> 16 & 0xff),
            (ip >> 24 & 0xff)
        );
    }

    private String getRouterIPAddress() {
        WifiManager wifiManager = (WifiManager) cordova.getActivity().getSystemService(Context.WIFI_SERVICE);
        DhcpInfo dhcp = wifiManager.getDhcpInfo();
        int ip = dhcp.gateway;
        return formatIP(ip);
    }
}

https://github.com/vallieres/cordova-plugin-get-router-ip-address/blob/master/src/android/GetRouterIPAddress.java

sMyles
  • 2,418
  • 1
  • 30
  • 44
-3

Try this:

$ busybox ip route show

It worked fine in my tablet with Terminal Emulator!

plsgogame
  • 1,334
  • 15
  • 28
Alex
  • 5
-4

To get the IP address, try getInetAddress();

Tom Tanner
  • 9,244
  • 3
  • 33
  • 61