With android (java), you can do this:
public static String getLocalIpAddress() {
try {
// gera uma lista enumerada de todas as interfaces de endereços de rede
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement(); // pega o elemento
// gera uma nova lista com todos os endereços de IP da interface
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement(); // pega o endereço IP
// verifica se este endereço não é um "localhost" e verifica se este é um IP de protocolo IPv4
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
// retorna o valor em string
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
This function returns the Ipv4 protocol IP
If you want Ipv6, you should use in your if of this foma:
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address)
Or so, to catch either one:
if (!inetAddress.isLoopbackAddress() && (inetAddress instanceof Inet6Address || inetAddress instanceof Inet4Address))
And then you send this information to the site. Better?
Sources:
Documentation of Android
Stackoverflow
Why not use java? Your application is hybrid?
– Andrei Coelho
The application is developed in android studio and if you are going to use Java how could do this?
– IMM Telecom