Accessing Wifi settings from android (IPV4)

Asked

Viewed 269 times

2

I’m accessing the Wifi settings of Android, and is charging as if I’m searching for a standard IP IPV6. I wonder if there is any Wificonfiguration property to set to IPV4

1 answer

1


To receive a IP patterned IPV4, use the following code:

public static String getIPV4 (){ 
    try {
        for (Enumeration enumeration = NetworkInterface.getNetworkInterfaces(); enumeration.hasMoreElements();){
            NetworkInterface nInterface = enumeration.nextElement();

            for (Enumeration IPenumeration = nInterface.getInetAddresses(); IPenumeration.hasMoreElements();){
            InetAddress netAdress = IPenumeration.nextElement();

            if (!netAdress.isLoopbackAddress()  &&  netAdress instanceof Inet4Address){ 
                    String IP4 = netAdress.getHostAddress().toString();
                    Toast.MakeText(getApplicationContext(), "IP: " + IP4, Toast.LENGTH_SHORT).show();
                    return IP4;
                }
            }
        }
    } catch (SocketException socketException) {
        Log.e("IP4 Erro", socketException.toString());
    }
    return null; 
}

To summarize all this, see:

  • if(..., netAdress instanceof Inet4Address)

The above condition checks whether the item received in the netAdress is the type Inet4Adress. That was what you need. If you already used a code similar to this, just add the operator instanceof in its condition to compare the received elements to the standard IPV4.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.