1

We are developing chat application and we want to fetch the IP address of the emulator which is connected to private network using the code.We are developing code in eclipse.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Tejaswini
  • 357
  • 4
  • 15

2 Answers2

2

Try out this code -

public String getLocalIpAddress() {
try {
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
        NetworkInterface intf = en.nextElement();
        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
            InetAddress inetAddress = enumIpAddr.nextElement();
            if (!inetAddress.isLoopbackAddress()) {
                return inetAddress.getHostAddress().toString();
            }
        }
    }
} catch (SocketException ex) {
    Log.e(LOG_TAG, ex.toString());
}
return null;
}

Check out this link

Community
  • 1
  • 1
Galaxy S2
  • 438
  • 1
  • 4
  • 10
  • We tried this.But we are getting 10.0.2.15.This is not the one we set in the emulator.We have set 192.168.xx.xx.Please help.Thanks in advance – Tejaswini Feb 16 '12 at 07:21
2
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
ipString = String.format( 
    "%d.%d.%d.%d", 
    (ip & 0xff), 
    (ip >> 8 & 0xff),
    (ip >> 16 & 0xff),
    (ip >> 24 & 0xff)
);

From this thread. Don't forget to have INTERNET permission in your AndroidManifest!

Hope this helps!

Community
  • 1
  • 1
Nick Badal
  • 681
  • 1
  • 8
  • 26