0

I am new to android development and I am doing an application which sends the IP address of an android device to another one by sms. I need to get the IP in decimal like this 192.168.0.4 not in hexadecimal which I got from the below code. any idea how to do that and thanks for the help.

    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(TAG, ex.toString());
         }

         return null;
    } 

4 Answers 4

8
public static String getLocalIpv4Address(){
    try {
        String ipv4;
        List<NetworkInterface>  nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        if(nilist.size() > 0){
            for (NetworkInterface ni: nilist){
                List<InetAddress>  ialist = Collections.list(ni.getInetAddresses());
                if(ialist.size()>0){
                    for (InetAddress address: ialist){
                        if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress())){ 
                            return ipv4;
                        }
                    }
                }

            }
        }

    } catch (SocketException ex) {

    }
    return "";
}

Should this be ok ? This function will return ipv4 (in xxx.xxx.xxx.xxx pattern) only if available.

Please note that those hexadecimal value you mentioned should be an ipv6 address.

Sign up to request clarification or add additional context in comments.

Comments

3

This post explains how to get the IP of the device.

This bit of code (taken from the aforementioned post) should get you the IP address the correct way:

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;
}

3 Comments

I am getting the IP in weird form hexadecimal or something and I need to have it like this 192.168.0.0 or anything like that. please help.
I don't know how to do that exactly, but if you follow the link I shared, which seems to be a fairly short tutorial, it explains how to get the IP of the device the right way so you get it in the form you're asking for.
I know, it's a bit late to reply but this might help others. You may use the following condition: if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) to filter out your results. This should give you nicely formatted IPv4 address only.
0

While hungr's answer is correct, I found that if I loop through the ip_addresses for a specific device "wlan0" for example, the first address is ipv6 and the second is ipv4. I was only returning the first value, which is why I was only getting a hex string.

for (InetAddress inetAddress : Collections.list(inetAddresses)) {
                String ip_address = inetAddress.getHostAddress();
                Log.d(APP_NAME, "IP: " + ip_address);
                //return if_name + ": " + ip_address;
}

Notice I commented out the "return"

Comments

-1

This info is available from the shell, using getprop command.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.