1I used this and it workd !
2WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
3String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
4Below permissions in the manifest file.
5<uses-permission android:name="android.permission.INTERNET" />
6<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
8
1You can use this method to get IP address of the device pass true for IPv4 and false for IPv6
2public static String getIPAddress(boolean useIPv4) {
3try {
4List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
5for (NetworkInterface intf : interfaces) {
6List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
7for (InetAddress addr : addrs) {
8if (!addr.isLoopbackAddress()) {
9String sAddr = addr.getHostAddress();
10//boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
11boolean isIPv4 = sAddr.indexOf(':')<0;
12if (useIPv4) {
13if (isIPv4)
14return sAddr;
15} else {
16if (!isIPv4) {
17int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
18return delim<0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
19}
20}
21}
22}
23}
24} catch (Exception ex) { } // for now eat exceptions
25return "";
26}
27Thanks to this ans How to get IP address of the device?