Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know ip address of the router from code in android?

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;
like image 960
HotIceCream Avatar asked Jan 27 '12 15:01

HotIceCream


People also ask

How do I get the IP address of my router?

On the Command Prompt window, enter “ipconfig” and press the [Enter]. The numbers indicated on the Default Gateway section is your router's IP Address. If you want to access the router's web-based setup page, enter the default gateway number in the URL or Address bar of your web browser.

How do I find my 192.168 IP address?

Select Command Prompt from the search results. In the black screen that comes up type in ipconfig/all and then Enter. You'll see a screen like this. In this example, the IPv4 address is 192.168.

How can I see all IP addresses on my network Android?

To check IP address of the local network on the Android device: Go to Settings → Network & internet on the tablet and select Wi-Fi. Tap the name of active network and expand the Advanced section. Find the Network details field with the local IP address.


2 Answers

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();
like image 161
Sandeep Avatar answered Sep 20 '22 07:09

Sandeep


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

like image 35
sMyles Avatar answered Sep 22 '22 07:09

sMyles