I have an app that has a web server that the user will access from their home LAN. Currently I have two ways of getting the device IP and I don't know which is more likely to be the right one.
My normal way:
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
for (InetAddress addr : addrs) {
if (!addr.isLoopbackAddress()) {
String sAddr = addr.getHostAddress().toUpperCase();
My alternate way:
WifiInfo connectionInfo = wifiMgr.getConnectionInfo();
int ip = connectionInfo.getIpAddress();
This I think has been working OK but I do have some statistics on it and it seems like the two ips are different only about 4% of the time.
Also I just tried my app on a Chromebook and the alternate way is actually the correct one, not the normal way.
So how can I know which way is better or is there a better alternative?