diff --git a/android/src/main/java/com/reactnativecommunity/netinfo/ConnectivityReceiver.java b/android/src/main/java/com/reactnativecommunity/netinfo/ConnectivityReceiver.java index 2597f0bb..a3e07a63 100644 --- a/android/src/main/java/com/reactnativecommunity/netinfo/ConnectivityReceiver.java +++ b/android/src/main/java/com/reactnativecommunity/netinfo/ConnectivityReceiver.java @@ -21,8 +21,11 @@ import com.reactnativecommunity.netinfo.types.ConnectionType; import java.math.BigInteger; +import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; import java.util.Locale; import javax.annotation.Nonnull; @@ -43,6 +46,24 @@ public abstract class ConnectivityReceiver { private boolean mIsInternetReachable = false; private Boolean mIsInternetReachableOverride; + private static String getSubnet(InetAddress inetAddress) throws SocketException { + NetworkInterface netAddress = NetworkInterface.getByInetAddress(inetAddress); + int mask = + 0xffffffff + << (32 + - netAddress + .getInterfaceAddresses() + .get(1) + .getNetworkPrefixLength()); + return String.format( + Locale.US, + "%d.%d.%d.%d", + (mask >> 24 & 0xff), + (mask >> 16 & 0xff), + (mask >> 8 & 0xff), + (mask & 0xff)); + } + ConnectivityReceiver(ReactApplicationContext reactContext) { mReactContext = reactContext; mConnectivityManager = @@ -160,6 +181,25 @@ private WritableMap createDetailsMap(@Nonnull String detailsInterface) { details.putString("carrier", carrier); } break; + case "ethernet": + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { + NetworkInterface netInterface = en.nextElement(); + + for (Enumeration ea = netInterface.getInetAddresses(); ea.hasMoreElements(); ) { + InetAddress inetAddress = ea.nextElement(); + if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { + String ipAddress = inetAddress.getHostAddress(); + details.putString("ipAddress", ipAddress); + details.putString("subnet", getSubnet(inetAddress)); + return details; + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + break; case "wifi": if (NetInfoUtils.isAccessWifiStatePermissionGranted(getReactContext())) { WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); @@ -224,24 +264,7 @@ private WritableMap createDetailsMap(@Nonnull String detailsInterface) { BigInteger.valueOf(wifiInfo.getIpAddress()).toByteArray(); NetInfoUtils.reverseByteArray(ipAddressByteArray); InetAddress inetAddress = InetAddress.getByAddress(ipAddressByteArray); - NetworkInterface netAddress = - NetworkInterface.getByInetAddress(inetAddress); - int mask = - 0xffffffff - << (32 - - netAddress - .getInterfaceAddresses() - .get(1) - .getNetworkPrefixLength()); - String subnet = - String.format( - Locale.US, - "%d.%d.%d.%d", - (mask >> 24 & 0xff), - (mask >> 16 & 0xff), - (mask >> 8 & 0xff), - (mask & 0xff)); - details.putString("subnet", subnet); + details.putString("subnet", getSubnet(inetAddress)); } catch (Exception e) { // Ignore errors }