Ahmed
Ahmed

Reputation: 15069

How to find LAN ip address of android device?

If wifi of device is connected, I assume the device has an LAN IP address assigned presumably by a dhcp running on a router.

How can find it what's the LAN ip address (not the external ip) on the wifi interface ?

Thanks,

Upvotes: 9

Views: 7630

Answers (1)

ariefbayu
ariefbayu

Reputation: 21979

NetworkInterface will help you:

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) {}

Upvotes: 12

Related Questions