0

In an android application, I want to get my dynamic local ip address that appears when I type "ipconfig" in cmd. Using this code I found somewhere here on stackoverflow, it is returning an ip address but it is not the same as the one from ipconfig. Why is that? How do I get the exact IP address that appeared using ipconfig?

public String getLocalIpAddress() {
       WifiManager wifiManager = (WifiManager)      
       getSystemService(WIFI_SERVICE);
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();
       int ip = wifiInfo.getIpAddress();

       String ipString = String.format(
       "%d.%d.%d.%d",
       (ip & 0xff),
       (ip >> 8 & 0xff),
       (ip >> 16 & 0xff),
       (ip >> 24 & 0xff));

       return ipString;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Jaquline
  • 3
  • 4
  • Did you take a look [here](http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device)? – Antrromet Feb 15 '15 at 22:04
  • 1
    I dont understand, what do you mean `ipconfig` is returning different ip address? When you run `ipconfig` in your terminal, it returns the ip address of your system. But the ip address that is returned with the above code will be the ip address of your phone. Both the ip addresses will obviously be different for different devices. – Antrromet Feb 15 '15 at 22:40
  • 1
    Why would you? Your android application runs on your android device and can only determine the ip of that device. Not of some PC in the neighbourhood. – greenapps Feb 15 '15 at 22:46

1 Answers1

0

Ok so based on the above commands I think I know the problem. A few basics first. Each device connected to a network will have a different ip address.

An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication

If you are at home with 2 mobiles and 1 laptop and all are connected to your home wifi, you will have 3 unique different ip addresses for each of your phones and laptop.

When you run ipconfig in your terminal, it returns the ip address of your system(or laptop). But when you run any of the code given here, then it returns the ip address of your mobile. This will obviously be different from what you see on your terminal.

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • I understand your point, but thats not possible in Android. You cannot get some other devices ip address. Your best bet is to change your url whenever the ip address changes. Another way is to create a hotspot from your phone and connect your laptop to it. In that way you can get your laptops ip address as explained [here](http://stackoverflow.com/questions/8324215/ip-address-of-device-using-phone-as-access-point?rq=1). – Antrromet Feb 15 '15 at 22:56