1

What is the best way to check if IPv6 is available on the currient android phone?

My currient idea is to use NetworkInterface and to enumerate via NetworkInterface.getNetworkInterfaces() but this seems to be too complicated.

Is there a simpler way?

rekire
  • 47,260
  • 30
  • 167
  • 264

2 Answers2

2

I don't know of a simpler way than using NetworkInterface if you need to check all of the interfaces, but it shouldn't be that bad:

for(NetworkInterface netInt: NetworkInterface.getNetworkInterfaces()){
    for(InterfaceAddress address: netInt.getInterfaceAddresses()){
        if(address.getAddress() instanceof Inet6Address){
            // found IPv6 address
            // do any other validation of address you may need here
        }
    }
}

if you know the address you want to check you can skip using NetworkInterface and check the specific InetAddress by calling one of InetAddress's static getBy...() methods and check whether that is an instance of Inet6Address.

Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
sethro
  • 2,127
  • 1
  • 14
  • 30
  • This will only work if the device is *currently* connected to the ipv6 network right? – joaonlima Nov 18 '15 at 11:12
  • This will only tell you that you have an IPv6 adapter. If you're on a NAT64 network that's downstream from an IPv4 only network, this will assume that you have IPv6 connectivity but when you try to make an IPv6 connection it will fail. – Nathan F. Aug 08 '17 at 22:33
0
boolean isIPV6 = false;
Enumeration<NetworkInterface> networkInterfaces =
    NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
    NetworkInterface ni = networkInterfaces.nextElement();
    for (InterfaceAddress addr : ni.getInterfaceAddresses()) {
        if (addr.getAddress() instanceof Inet6Address) {
            isIPV6 = true;
        }
    }
}
astryk
  • 1,256
  • 13
  • 20
  • Why should be this solution better then the accepted? You forgot to write any documentation in your answer. – rekire Apr 19 '18 at 13:32
  • `NetworkInterface.getNetworkInterfaces()` returns an enumeration so you cannot foreach it, you need to use it like you would an `Iterator`. Besides that, it is essentially the same answer. Mine just compiles. :-P – astryk Apr 21 '18 at 02:15