3

When I am connected to WiFi, I can obtain the IP address of the Android phone.

However, when on the mobile network like 3G connection, is it still possible to obtain the IP address of the Android phone?
If yes, kindly post the code for the same.

iammilind
  • 68,093
  • 33
  • 169
  • 336
Shahtaj
  • 51
  • 1
  • 2
  • 4

2 Answers2

8

try something like this

String ipAddress = null;
    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()) {
                    ipAddress = inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {}
Josnidhin
  • 12,469
  • 9
  • 42
  • 61
  • getting ip addres 0.0.0.0 – Umesh Apr 18 '14 at 09:59
  • @Josnidhin because it runs over loop, how do you ensure that you actually are getting correct ip? There is a possibility that genuine ip has been overwritten by the last ip(found during loop iteration) fetched using this approach. – Kaps Sep 15 '17 at 12:59
-1

the mobile device does not have an ip when browsing over 3G connection, You will get the ISP ip on the server side code. I recommend you replace the ip with the unique id, device type and coordinates if possible.

Houssam Hamdan
  • 888
  • 6
  • 15