Alternative for Formatter.formatIpAddress(int);

Viewed 20875

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?

10 Answers

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.

val ip = (InetAddress.getByAddress(ByteArray(4, { i ->
        (connectionInfo.ipAddress
            .shr(i * 8).and(255)).toByte()
    })) as Inet4Address).hostAddress

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) {
}

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()

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

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