Reputation: 703
I'm working on an Android application that will run on Android TV Box, my device can connect to network through Wifi
or Ethernet
cable. I need to get device IPv4
address, now I'm able to get the address when device is connected through Wifi
but failed to get the address when it is connected through Ethernet
cable.
My minimum SDK version is 24, and currently as I control the devices which my application will run, my target Android versions are 7.1 and 8.0.
I use below code to get device IPv4
address when it is connected to Wifi
, but I cannot find a similar code for Ethernet
cable
WifiManager manager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
int ipInt = wifiInfo.getIpAddress();
String ip = InetAddress.getByAddress(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()).getHostAddress();
I need a way to detect if my device is connected through Wifi
or Ethernet
and get IPv4
properly.
Upvotes: 1
Views: 1373
Reputation: 585
public static String getIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
Edit : Add Internet permission in manifest for this to work :
<uses-permission android:name="android.permission.INTERNET" />
This should help in all cases.
Upvotes: 2
Reputation: 139
You should try something as shown below.
Check if device is connected to Wi-Fi or not. If yes, the one that you've done works perfectly fine but if not, try the else part from the code below.
Step - 1:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step - 2:
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
String ip = null;
if (mWifi.isConnected()) {
WifiManager manager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = manager.getConnectionInfo();
int ipInt = wifiInfo.getIpAddress();
ip = InetAddress.getByAddress(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()).getHostAddress();
}
else{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ip = inetAddress.getHostAddress();
Log.i(TAG, "***** IP="+ ip);
}
}
}
} catch (SocketException ex) {
Log.e(TAG, ex.toString());
}
}
return ip;
Upvotes: 0