0

I want to obtain the local IPv4 address of a user on their phone running my application when connected to a wifi network. Using the following code:

  WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
                    hostname = ip;

I am able to get something close to the IPv4 address but when compared to the IPv4 address in the command line, it is not exactly the same. Is there a better way to go about this? I know that formatIpAddress is deprecated but until I find a way to get the IPv4 address I'm not too worried about that for now.

EDIT:

I have discovered that the ip address in the phone's wifi settings is what I am getting when using solutions to get the ip address like the suggested solutions. Is there any way to get the ip address in the ip config client side?

  • 4
    *I am able to get something close to the IPv4 address but it is not exact.* What does that even mean? – shmosel Aug 17 '17 at 18:06
  • check this question: https://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device-from-code – Felipe Silveira Aug 17 '17 at 18:06
  • [This link](https://stackoverflow.com/questions/40912417/java-getting-ipv4-address) can maybe help you to get IPv4 address. Hope it helps. – Greenkiller Aug 17 '17 at 18:08
  • Doesn't getIpAddress() method return the IP of the Wireless AP? That's why it seems "similar but not exact" to you. Because the IP of the client and AP are almost same except for the last few digits. – Debanik Dawn Aug 17 '17 at 18:11
  • Possible duplicate of [Get my wifi ip address Android](https://stackoverflow.com/questions/16730711/get-my-wifi-ip-address-android) – AnthonyK Aug 17 '17 at 18:16
  • Debanik that could be it. I have tried all the solutions that are linked to above and I keep getting xx.xxx to be the same as the IPv4 address in command line but anything past that is different. – Bisquick Quick Aug 17 '17 at 18:22
  • I have discovered that the ip address in the phone's wifi settings is what I am getting when using solutions to get the ip address like the links above. Is there any way to get the ip address in the ip config client side? – Bisquick Quick Aug 17 '17 at 19:55

2 Answers2

2

The code print the local IPv4 address of the device in which ur either Android or java application is running.

 try {
        Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces();  // gets All networkInterfaces of your device
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface inet = (NetworkInterface) networkInterfaces.nextElement();
            Enumeration address = inet.getInetAddresses();
            while (address.hasMoreElements()) {
                InetAddress inetAddress = (InetAddress) address.nextElement();
                if (inetAddress.isSiteLocalAddress()) {
                    System.out.println("Your ip: " + inetAddress.getHostAddress());  /// gives ip address of your device
                }
            }
        }
    } catch (Exception e) {
        // Handle Exception
    }
0

The following code traverse all the interface it has and then print the IPv4, IPv6 and mac address of the interface. For LAN IP address u can use a function isSiteLocal() which returns true if IP address is a local address.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
public class App{

public static void main(String[] args)throws Exception {
    // getting the list of interfaces in the local machine
    Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
    while( n.hasMoreElements()){ //for each interface
        System.out.println("----------------------------------------------------");
        NetworkInterface e = n.nextElement();
    //name of the interface
        System.out.println("Interface Name: " + e.getName());
    /* A interface may be binded to many IP addresses like IPv4 and IPv6
        hence getting the Enumeration of list of IP addresses  */
        Enumeration<InetAddress> a = e.getInetAddresses();
        while( a.hasMoreElements()){
            InetAddress addr = a.nextElement();
            String add = addr.getHostAddress().toString();
            if( add.length() < 17 )
                System.out.println("IPv4 Address: " + add);
            else
                System.out.println("IPv6 Address: " + add);
        }
        if(e.getHardwareAddress() != null){
                    // getting the mac address of the particular network interface
            byte[] mac = e.getHardwareAddress();
                    // properly formatting the mac address
            StringBuilder macAddress = new StringBuilder();
            for(int i =0; i < mac.length; i++){
                macAddress.append(String.format("%03X%s", mac[i],(i < mac.length -1) ? "-":""));
            }
            System.out.println("Hardware adrress: " + macAddress.toString());
        }
        System.out.println("----------------------------------------------------");
    }
}

}

The output of the code in kali linux 2.0 is:
----------------------------------------------------
Interface Name: wlan0
IPv6 Address: fe80:0:0:0:1992:d9bc:7d8c:d85b%wlan0
IPv4 Address: 192.168.1.137
Hardware adrress: 078-0E4-000-0E7-0B0-046
----------------------------------------------------
----------------------------------------------------
Interface Name: lo
IPv6 Address: 0:0:0:0:0:0:0:1%lo
IPv4 Address: 127.0.0.1
----------------------------------------------------