Accessing android Wifi settings (IPV4)

2

I'm accessing the Wifi settings of Android, and it's accusing like I'm looking for an IPv6 default IP. I would like to know if there is any WifiConfiguration property to define as IPV4

    
asked by anonymous 17.03.2015 / 19:15

1 answer

1

To receive an IP with IPV4 pattern, 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 to see if the element received in netAdress is of type Inet4Adress . That's what you need. If you already used code similar to this, just add the instanceof operator in your condition to compare the elements received with the IPV4 pattern.

    
18.03.2015 / 00:23