Skip to content

Commit

Permalink
feat(android): add ethernet information (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Mar 18, 2022
1 parent 07e9345 commit 2b3a8e2
Showing 1 changed file with 41 additions and 18 deletions.
Expand Up @@ -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;
Expand All @@ -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 =
Expand Down Expand Up @@ -160,6 +181,25 @@ private WritableMap createDetailsMap(@Nonnull String detailsInterface) {
details.putString("carrier", carrier);
}
break;
case "ethernet":
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface netInterface = en.nextElement();

for (Enumeration<InetAddress> 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();
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2b3a8e2

Please sign in to comment.