Reputation: 1771
I am trying to display the ip address of connected networks in android. I am using the following code. But it returns 2 IPv6 ip address. How to find the correct ip address from that?
I am using the following code:
List<LinkAddress> linkAddresses = connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getLinkAddresses();
for (LinkAddress linkAddress : linkAddresses) {
Log.i("","LinkAddress getAddress "+linkAddress.getAddress() + "");
Log.i("","Is IPV6 " + (linkAddress.getAddress() instanceof Inet6Address) +"");
Log.i("","Is IPV4 " + (linkAddress.getAddress() instanceof Inet4Address) +"");
Log.i("","Is isLinkLocalAddress " + (linkAddress.getAddress().isLinkLocalAddress()) +"");
Log.i("","Is not isLoopbackAddress " + (!linkAddress.getAddress().isLoopbackAddress()) +"");
}
Now I am getting 4 ip addresses.
LinkAddress getAddress /fe80::2d0:caff:fe00:5ad6
LinkAddress getAddress /2401:4900:2305:14e:2d0:caff:fe00:5ad6
LinkAddress getAddress /2401:4900:2305:14e:28e2:5192:e38f:3e9
LinkAddress getAddress /192.168.43.176
I can identify fe80 is Link Local Ip address and 192. is IPv4 address. But I am confused to identify IPV6 address from this. Please help me to find out the IPv6 ip address.
Upvotes: 0
Views: 2222
Reputation: 9978
Both IPv6 addresses are valid. It's normal to have multiple addresses per interface. Both are in the same subnet (2401:4900:2305:14e::/64). When you look at the interface ID (the second half of the address) you'll see that one has ..ff:fe.. in the middle. That's a sign that that address is probably derived from the MAC address of the interface. The other address is a temporary address that will change over time to protect the privacy of the user.
But in short: both addresses are completely valid and usable.
Upvotes: 2