Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): add ethernet information #584

Merged
merged 1 commit into from
Mar 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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