22

I'd used this code in my application, but the warning says its : "The method formatIpAddress(int) from the type Formatter is deprecated"

android.text.format.Formatter.formatIpAddress(mWifiManager.getConnectionInfo().getIpAddress());

what's the quick fix for this?

Blackbelt
  • 156,034
  • 29
  • 297
  • 305

10 Answers10

25

The documentation states

Use getHostAddress(), which supports both IPv4 and IPv6 addresses. This method does not support IPv6 addresses.

where getHostAddress() refers to InetAddress.getHostAddress().

However, WifiInfo just has a ipv4 address as an int and AFAIK there's no practical way to convert it to an InetAddress. The deprecation is because the function doesn't support ipv6 but neither does WifiInfo. So I'd say just use formatIpAddress() because it works and add @SuppressWarnings("deprecation") to get rid of the warning.

laalto
  • 150,114
  • 66
  • 286
  • 303
25
WifiInfo wifiinfo = manager.getConnectionInfo();
byte[] myIPAddress = BigInteger.valueOf(wifiinfo.getIpAddress()).toByteArray();
// you must reverse the byte array before conversion. Use Apache's commons library
ArrayUtils.reverse(myIPAddress);
InetAddress myInetIP = InetAddress.getByAddress(myIPAddress);
String myIP = myInetIP.getHostAddress();

So myIP should be what you want.

Muhammad Riyaz
  • 2,832
  • 2
  • 26
  • 30
  • ArrayUtils.reverse is not found? missing something? – Alp Altunel Sep 25 '20 at 09:29
  • @AlpAltunel as mentioned in above code comment, i used ArrayUtils by Apache commons. see here https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ArrayUtils.html – Muhammad Riyaz Dec 04 '20 at 13:06
9

An alternative to Muhammad Riyaz approach:

String ip = InetAddress.getByAddress(
    ByteBuffer
        .allocate(Integer.BYTES)
        .order(ByteOrder.LITTLE_ENDIAN)
        .putInt(manager.getConnectionInfo().getIpAddress())
        .array()
    ).getHostAddress();

This way you don't have to use Apache's commons library.

jpihl
  • 7,941
  • 3
  • 37
  • 50
2

it was deprecated from api level 12 in favour of [getHostAdress();][1]. So I suggest to add the suppresswarning annotation and do the following thing:

String myIpString = null;
if (apilevel < 12) {
    myIpString = formatIpAddress(...);
} else {
    myIpString = getHostAdress();
}

you can get the api level of the device this way:

int apiLevel = Integer.valueOf(android.os.Build.VERSION.SDK);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0
val ip = (InetAddress.getByAddress(ByteArray(4, { i ->
        (connectionInfo.ipAddress
            .shr(i * 8).and(255)).toByte()
    })) as Inet4Address).hostAddress
Ken Zira
  • 1,170
  • 2
  • 10
  • 12
0

Also without any additional dependencies (notice the different Context used):

WifiManager wm = (WifiManager) requireContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int ip = wm.getConnectionInfo().getIpAddress();
byte[] buffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ip).array();

try {
    String dotNotation = InetAddress.getByAddress(buffer).getHostAddress();
} catch (UnknownHostException ignore) {
}
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

You basically have to use InetAddress.getHostAddress() to get it working with both IPv4 and IPv6

// Start by creating an `InetAddress` Object
val inetAddress= InetAddress.getByAddress(wifiManager.connectionInfo.ipAddress.toReversedByteArray())

// this will return a string representation of your ip address
val ipAddress = inetAddress.hostAddress 

// cast an int IpAddress into byte array
fun Int.toReversedByteArray() = toBigInteger().toByteArray().reversedArray()
AouledIssa
  • 2,528
  • 2
  • 22
  • 39
0

Both getConnectionInfo() and getIpAddress() will be deprecated, here I post the latest solution to get the Wifi ip address:

object ConnectivityHelper {
    fun Context.getConnectivityManager() = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    fun getIpAddress(context: Context) = with(context.getConnectivityManager()) {
        getLinkProperties(activeNetwork)!!.linkAddresses[1].address.hostAddress!!
    }
}
Sam Chen
  • 7,597
  • 2
  • 40
  • 73
0

Kotlin:

 private fun getLocalIpAddress(): String? {
        try {
            val en = NetworkInterface.getNetworkInterfaces()
            while (en.hasMoreElements()) {
                val enumIpAddress = en.nextElement().inetAddresses
                while (enumIpAddress.hasMoreElements()) {
                    val inetAddress = enumIpAddress.nextElement()
                    if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
                        return inetAddress.hostAddress
                    }
                }
            }
        } catch (ex: SocketException) {
            ex.printStackTrace()
        }
        return null
    }

Java:

public static 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() && inetAddress instanceof Inet4Address) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        ex.printStackTrace();
    }
    return null;
}
Meet Bhavsar
  • 442
  • 6
  • 12
-4

Get rid of the warning by adding @SuppressWarnings("deprecation")

@SuppressWarnings("deprecation")
public static String getLocalAddress()
{
    String iIPv4 = "";

    WifiManager wm = (WifiManager) theContext.getSystemService(Context.WIFI_SERVICE);

    iIPv4 = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

    return iIPv4;
}
Tim
  • 606
  • 1
  • 8
  • 19