0

I want to get the IP used by WIFI connection(not 3G). Does anybody know how to do it? I used:

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;

}

but it returns the 3G ip on ICS.

Thanks,

Alex

Alexandru Circus
  • 5,478
  • 7
  • 52
  • 89

2 Answers2

0
WifiInfo winfo = ((WifiManager)this.getSystemService(Context.WIFI_SERVICE)).getConnectionInfo();
winfo.getIpAddress();

You may want to check if you are connected to Wifi prior to doing the above

Orlymee
  • 2,349
  • 1
  • 22
  • 24
0

You cant detect connection type based on IP address because your mobile network and home WiFi network, both can assign private IP address.

What you need to do is to first detect either you have mobile network or WiFi connection, and then based on that info get the IP address of that connection.

See this thread in SO it's the same issue as your on ICS

Community
  • 1
  • 1
K_Anas
  • 31,226
  • 9
  • 68
  • 81