how to obtain the ip address of the connected wifi router in android programmatically?

akshay1728 picture akshay1728 · Aug 17, 2012 · Viewed 13.4k times · Source

I want to obtain the ip address of the the wifi router to which my android phone is connected? I know that we can get the mac/BSSId and SSID by using the android APIS but I don't find the way to find the way to find the ip address of it?

I found the code for obtaining the ip address of phone owns wifi router

WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int ipAddress = myWifiInfo.getIpAddress();
System.out.println("WiFi address is " + android.text.format.Formatter.formatIpAddress(ipAddress))

but failed to get what I want

Answer

obataku picture obataku · Aug 17, 2012

What you want is DhcpInfo....

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

This will determine the formatted gateway IP address, which should be what you're looking for.