I'm creating android app and want to get, in the backend, authenticated user's device OS version and the ip address? after some researches, I didn't found the helpful information and can someone help me? :)
I'm creating android app and want to get, in the backend, authenticated user's device OS version and the ip address? after some researches, I didn't found the helpful information and can someone help me? :)
Here's in java For OsVesion:
String osVersion = Build.VERSION.RELEASE + " " + Build.VERSION_CODES.class.getFields()[android.os.Build.VERSION.SDK_INT].getName();
For IP: if it's mobileData
public static String getMobileIPAddress() {
try {
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()) {
return addr.getHostAddress();
}
}
}
} catch (Exception ex) {
} // for now eat exceptions
return "";
}
For IP: if it's WiFi
public String getWifiIPAddress() {
WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
return Formatter.formatIpAddress(ip);
}
to check if it's mobile data or WiFi:
if (getWifiIPAddress().equals("0.0.0.0")) {
ipAddress = getMobileIPAddress();
} else {
ipAddress = getWifiIPAddress();
}
you can get os version with:
val deviceOS = android.os.Build.VERSION;
for ip address:
fun getLocalIpAddress(): String? {
try {
val en: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces()
while (en.hasMoreElements()) {
val networkInterface: NetworkInterface = en.nextElement()
val enumerationIpAddress: Enumeration<InetAddress> = networkInterface.inetAddresses
while (enumerationIpAddress.hasMoreElements()) {
val inetAddress: InetAddress = enumerationIpAddress.nextElement()
if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) {
return inetAddress.getHostAddress()
}
}
}
} catch (ex: SocketException) {
ex.printStackTrace()
}
return null
}