6
6
import android .util .Log ;
7
7
8
8
import com .facebook .react .bridge .Callback ;
9
- import com .facebook .react .bridge .NativeModule ;
10
9
import com .facebook .react .bridge .ReactApplicationContext ;
11
- import com .facebook .react .bridge .ReactContext ;
12
10
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
13
11
import com .facebook .react .bridge .ReactMethod ;
14
-
15
- import java .math .BigInteger ;
16
12
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 ;
20
16
21
17
public class RNNetworkInfo extends ReactContextBaseJavaModule {
22
18
WifiManager wifi ;
@@ -26,7 +22,8 @@ public class RNNetworkInfo extends ReactContextBaseJavaModule {
26
22
public RNNetworkInfo (ReactApplicationContext reactContext ) {
27
23
super (reactContext );
28
24
29
- wifi = (WifiManager )reactContext .getSystemService (Context .WIFI_SERVICE );
25
+ wifi = (WifiManager )reactContext .getApplicationContext ()
26
+ .getSystemService (Context .WIFI_SERVICE );
30
27
}
31
28
32
29
@ Override
@@ -37,47 +34,34 @@ public String getName() {
37
34
@ ReactMethod
38
35
public void getSSID (final Callback callback ) {
39
36
WifiInfo info = wifi .getConnectionInfo ();
40
-
37
+
41
38
// This value should be wrapped in double quotes, so we need to unwrap it.
42
39
String ssid = info .getSSID ();
43
40
if (ssid .startsWith ("\" " ) && ssid .endsWith ("\" " )) {
44
41
ssid = ssid .substring (1 , ssid .length () - 1 );
45
42
}
46
-
43
+
47
44
callback .invoke (ssid );
48
45
}
49
46
50
47
@ ReactMethod
51
48
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 ;
56
50
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 ;
71
51
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 ());
78
63
}
79
-
80
- callback .invoke (ipAddressString );
81
- }
82
64
65
+ callback .invoke (ipAddress );
66
+ }
83
67
}
0 commit comments