1

Creating WifiManager

 WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

Getting ipAddress

 String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());

And displaying in textview

 textview.setText("Your Device IP Address: " + ipAddress);
Santanu Sur
  • 10,997
  • 7
  • 33
  • 52

4 Answers4

1

You can use this method to get IP address of the device pass true for IPv4 and false for IPv6

public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress();
                    //boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    boolean isIPv4 = sAddr.indexOf(':')<0;

                    if (useIPv4) {
                        if (isIPv4) 
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
                            return delim<0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
                        }
                    }
                }
            }
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}
Kalpesh Rupani
  • 991
  • 4
  • 12
0

You have to define <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> in AndroidManifest.xml file, It will give you the ip address.

0

If you use a simulator or real device, you need to check this device has use wifi or network connection. If you don't know which device is using the network you need get all network and check IP, it has, so returns IP

public String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress();
                    //boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    boolean isIPv4 = sAddr.indexOf(':')<0;

                    if (useIPv4) {
                        if (isIPv4) 
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
                            return delim<0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
                        }
                    }
                }
            }
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
 }

In AndroidManifest, add permission:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Cuong Nguyen
  • 970
  • 6
  • 17
0

Add this permission in Manifest

      <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

Make sure that Formatter library should be this

  import android.text.format.Formatter

Nothing wrong with your code. Just make sure this thing

Rahul sharma
  • 1,492
  • 12
  • 26