Skip to content

Commit 84290e3

Browse files
authored
Merge pull request #24 from pusherman/feature/update_java_get_ip
Update java's getIPAddress method
2 parents a595e50 + 33bebe8 commit 84290e3

File tree

2 files changed

+22
-38
lines changed

2 files changed

+22
-38
lines changed

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.1"
5+
buildToolsVersion '25.0.2'
66

77
defaultConfig {
88
minSdkVersion 16

android/src/main/java/com/pusherman/networkinfo/RNNetworkInfo.java

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
import android.util.Log;
77

88
import com.facebook.react.bridge.Callback;
9-
import com.facebook.react.bridge.NativeModule;
109
import com.facebook.react.bridge.ReactApplicationContext;
11-
import com.facebook.react.bridge.ReactContext;
1210
import com.facebook.react.bridge.ReactContextBaseJavaModule;
1311
import com.facebook.react.bridge.ReactMethod;
14-
15-
import java.math.BigInteger;
1612
import java.net.InetAddress;
17-
import java.net.UnknownHostException;
18-
import java.nio.ByteOrder;
19-
import java.util.Map;
13+
14+
import java.net.NetworkInterface;
15+
import java.util.Enumeration;
2016

2117
public class RNNetworkInfo extends ReactContextBaseJavaModule {
2218
WifiManager wifi;
@@ -26,7 +22,8 @@ public class RNNetworkInfo extends ReactContextBaseJavaModule {
2622
public RNNetworkInfo(ReactApplicationContext reactContext) {
2723
super(reactContext);
2824

29-
wifi = (WifiManager)reactContext.getSystemService(Context.WIFI_SERVICE);
25+
wifi = (WifiManager)reactContext.getApplicationContext()
26+
.getSystemService(Context.WIFI_SERVICE);
3027
}
3128

3229
@Override
@@ -37,47 +34,34 @@ public String getName() {
3734
@ReactMethod
3835
public void getSSID(final Callback callback) {
3936
WifiInfo info = wifi.getConnectionInfo();
40-
37+
4138
// This value should be wrapped in double quotes, so we need to unwrap it.
4239
String ssid = info.getSSID();
4340
if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
4441
ssid = ssid.substring(1, ssid.length() - 1);
4542
}
46-
43+
4744
callback.invoke(ssid);
4845
}
4946

5047
@ReactMethod
5148
public void getIPAddress(final Callback callback) {
52-
WifiInfo info = wifi.getConnectionInfo();
53-
54-
// The following is courtesy of Digital Rounin at
55-
// http://stackoverflow.com/a/18638588 .
49+
String ipAddress = null;
5650

57-
// The endian-ness of `ip` is potentially varying, but we need it to be big-
58-
// endian.
59-
int ip = info.getIpAddress();
60-
61-
// Convert little-endian to big-endian if needed.
62-
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
63-
ip = Integer.reverseBytes(ip);
64-
}
65-
66-
// Now that the value is guaranteed to be big-endian, we can convert it to
67-
// an array whose first element is the high byte.
68-
byte[] ipByteArray = BigInteger.valueOf(ip).toByteArray();
69-
70-
String ipAddressString;
7151
try {
72-
// `getByAddress()` wants network byte-order, aka big-endian.
73-
// Good thing we planned ahead!
74-
ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
75-
} catch (UnknownHostException ex) {
76-
Log.e(TAG, "Unable to determine IP address.");
77-
ipAddressString = null;
52+
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
53+
NetworkInterface intf = en.nextElement();
54+
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
55+
InetAddress inetAddress = enumIpAddr.nextElement();
56+
if (!inetAddress.isLoopbackAddress()) {
57+
ipAddress = inetAddress.getHostAddress();
58+
}
59+
}
60+
}
61+
} catch (Exception ex) {
62+
Log.e(TAG, ex.toString());
7863
}
79-
80-
callback.invoke(ipAddressString);
81-
}
8264

65+
callback.invoke(ipAddress);
66+
}
8367
}

0 commit comments

Comments
 (0)