@onekeyfe/react-native-network-info 3.0.7 → 3.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/rnnetworkinfo/NetworkInfoModule.kt +151 -124
- package/ios/NetworkInfo.h +2 -0
- package/ios/NetworkInfo.mm +9 -0
- package/lib/module/NativeNetworkInfo.js.map +1 -1
- package/lib/typescript/src/NativeNetworkInfo.d.ts +1 -0
- package/lib/typescript/src/NativeNetworkInfo.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativeNetworkInfo.ts +1 -0
|
@@ -1,27 +1,38 @@
|
|
|
1
1
|
package com.rnnetworkinfo
|
|
2
2
|
|
|
3
3
|
import android.content.Context
|
|
4
|
-
import android.net.
|
|
5
|
-
import android.net.NetworkCapabilities
|
|
4
|
+
import android.net.wifi.SupplicantState
|
|
6
5
|
import android.net.wifi.WifiInfo
|
|
7
6
|
import android.net.wifi.WifiManager
|
|
8
|
-
import android.os.Build
|
|
9
7
|
import com.facebook.react.bridge.Promise
|
|
10
8
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
11
9
|
import com.facebook.react.module.annotations.ReactModule
|
|
12
10
|
import java.net.Inet4Address
|
|
13
11
|
import java.net.Inet6Address
|
|
14
12
|
import java.net.InetAddress
|
|
13
|
+
import java.net.InterfaceAddress
|
|
15
14
|
import java.net.NetworkInterface
|
|
16
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Ported from upstream react-native-network-info RNNetworkInfo.java.
|
|
18
|
+
* Adapted to extend NativeRNNetworkInfoSpec (TurboModule).
|
|
19
|
+
*/
|
|
17
20
|
@ReactModule(name = NetworkInfoModule.NAME)
|
|
18
21
|
class NetworkInfoModule(reactContext: ReactApplicationContext) :
|
|
19
22
|
NativeRNNetworkInfoSpec(reactContext) {
|
|
20
23
|
|
|
21
24
|
companion object {
|
|
22
25
|
const val NAME = "RNNetworkInfo"
|
|
26
|
+
|
|
27
|
+
val DSLITE_LIST = listOf(
|
|
28
|
+
"192.0.0.0", "192.0.0.1", "192.0.0.2", "192.0.0.3",
|
|
29
|
+
"192.0.0.4", "192.0.0.5", "192.0.0.6", "192.0.0.7"
|
|
30
|
+
)
|
|
23
31
|
}
|
|
24
32
|
|
|
33
|
+
private val wifi: WifiManager =
|
|
34
|
+
reactContext.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
|
35
|
+
|
|
25
36
|
override fun getName(): String = NAME
|
|
26
37
|
|
|
27
38
|
// MARK: - getSSID
|
|
@@ -29,7 +40,16 @@ class NetworkInfoModule(reactContext: ReactApplicationContext) :
|
|
|
29
40
|
override fun getSSID(promise: Promise) {
|
|
30
41
|
Thread {
|
|
31
42
|
try {
|
|
32
|
-
|
|
43
|
+
@Suppress("DEPRECATION")
|
|
44
|
+
val info: WifiInfo = wifi.connectionInfo
|
|
45
|
+
var ssid: String? = null
|
|
46
|
+
if (info.supplicantState == SupplicantState.COMPLETED) {
|
|
47
|
+
@Suppress("DEPRECATION")
|
|
48
|
+
ssid = info.ssid
|
|
49
|
+
if (ssid != null && ssid.startsWith("\"") && ssid.endsWith("\"")) {
|
|
50
|
+
ssid = ssid.substring(1, ssid.length - 1)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
33
53
|
promise.resolve(ssid)
|
|
34
54
|
} catch (e: Exception) {
|
|
35
55
|
promise.resolve(null)
|
|
@@ -37,31 +57,18 @@ class NetworkInfoModule(reactContext: ReactApplicationContext) :
|
|
|
37
57
|
}.start()
|
|
38
58
|
}
|
|
39
59
|
|
|
40
|
-
private fun getWifiSSID(): String? {
|
|
41
|
-
val context = reactApplicationContext
|
|
42
|
-
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as? WifiManager
|
|
43
|
-
?: return null
|
|
44
|
-
@Suppress("DEPRECATION")
|
|
45
|
-
val wifiInfo: WifiInfo = wifiManager.connectionInfo ?: return null
|
|
46
|
-
@Suppress("DEPRECATION")
|
|
47
|
-
val ssid = wifiInfo.ssid
|
|
48
|
-
if (ssid == null || ssid == "<unknown ssid>") return null
|
|
49
|
-
return if (ssid.startsWith("\"") && ssid.endsWith("\"")) {
|
|
50
|
-
ssid.substring(1, ssid.length - 1)
|
|
51
|
-
} else {
|
|
52
|
-
ssid
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
60
|
// MARK: - getBSSID
|
|
57
61
|
|
|
58
62
|
override fun getBSSID(promise: Promise) {
|
|
59
63
|
Thread {
|
|
60
64
|
try {
|
|
61
|
-
val context = reactApplicationContext
|
|
62
|
-
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as? WifiManager
|
|
63
65
|
@Suppress("DEPRECATION")
|
|
64
|
-
val
|
|
66
|
+
val info: WifiInfo = wifi.connectionInfo
|
|
67
|
+
var bssid: String? = null
|
|
68
|
+
if (info.supplicantState == SupplicantState.COMPLETED) {
|
|
69
|
+
@Suppress("DEPRECATION")
|
|
70
|
+
bssid = wifi.connectionInfo.bssid
|
|
71
|
+
}
|
|
65
72
|
promise.resolve(bssid)
|
|
66
73
|
} catch (e: Exception) {
|
|
67
74
|
promise.resolve(null)
|
|
@@ -74,22 +81,16 @@ class NetworkInfoModule(reactContext: ReactApplicationContext) :
|
|
|
74
81
|
override fun getBroadcast(promise: Promise) {
|
|
75
82
|
Thread {
|
|
76
83
|
try {
|
|
77
|
-
var
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
val broadcastAddr = ia.broadcast
|
|
84
|
-
if (broadcastAddr != null) {
|
|
85
|
-
broadcast = broadcastAddr.hostAddress
|
|
86
|
-
break
|
|
87
|
-
}
|
|
84
|
+
var ipAddress: String? = null
|
|
85
|
+
for (address in getInetAddresses()) {
|
|
86
|
+
if (!address.address.isLoopbackAddress) {
|
|
87
|
+
val broadCast: InetAddress? = address.broadcast
|
|
88
|
+
if (broadCast != null) {
|
|
89
|
+
ipAddress = broadCast.toString()
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
|
-
if (broadcast != null) break
|
|
91
92
|
}
|
|
92
|
-
promise.resolve(
|
|
93
|
+
promise.resolve(ipAddress)
|
|
93
94
|
} catch (e: Exception) {
|
|
94
95
|
promise.resolve(null)
|
|
95
96
|
}
|
|
@@ -101,10 +102,41 @@ class NetworkInfoModule(reactContext: ReactApplicationContext) :
|
|
|
101
102
|
override fun getIPAddress(promise: Promise) {
|
|
102
103
|
Thread {
|
|
103
104
|
try {
|
|
104
|
-
|
|
105
|
-
|
|
105
|
+
var ipAddress: String? = null
|
|
106
|
+
var tmp = "0.0.0.0"
|
|
107
|
+
for (address in getInetAddresses()) {
|
|
108
|
+
if (!address.address.isLoopbackAddress) {
|
|
109
|
+
tmp = address.address.hostAddress.toString()
|
|
110
|
+
if (!inDSLITERange(tmp)) {
|
|
111
|
+
ipAddress = tmp
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
promise.resolve(ipAddress)
|
|
106
116
|
} catch (e: Exception) {
|
|
107
|
-
promise.resolve(
|
|
117
|
+
promise.resolve(null)
|
|
118
|
+
}
|
|
119
|
+
}.start()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// MARK: - getIPV4Address
|
|
123
|
+
|
|
124
|
+
override fun getIPV4Address(promise: Promise) {
|
|
125
|
+
Thread {
|
|
126
|
+
try {
|
|
127
|
+
var ipAddress: String? = null
|
|
128
|
+
var tmp = "0.0.0.0"
|
|
129
|
+
for (address in getInetAddresses()) {
|
|
130
|
+
if (!address.address.isLoopbackAddress && address.address is Inet4Address) {
|
|
131
|
+
tmp = address.address.hostAddress.toString()
|
|
132
|
+
if (!inDSLITERange(tmp)) {
|
|
133
|
+
ipAddress = tmp
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
promise.resolve(ipAddress)
|
|
138
|
+
} catch (e: Exception) {
|
|
139
|
+
promise.resolve(null)
|
|
108
140
|
}
|
|
109
141
|
}.start()
|
|
110
142
|
}
|
|
@@ -135,131 +167,126 @@ class NetworkInfoModule(reactContext: ReactApplicationContext) :
|
|
|
135
167
|
}.start()
|
|
136
168
|
}
|
|
137
169
|
|
|
138
|
-
// MARK: -
|
|
170
|
+
// MARK: - getWIFIIPV4Address
|
|
139
171
|
|
|
140
|
-
override fun
|
|
172
|
+
override fun getWIFIIPV4Address(promise: Promise) {
|
|
141
173
|
Thread {
|
|
142
174
|
try {
|
|
143
|
-
val context = reactApplicationContext
|
|
144
|
-
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as? WifiManager
|
|
145
175
|
@Suppress("DEPRECATION")
|
|
146
|
-
val
|
|
147
|
-
val
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
176
|
+
val info: WifiInfo = wifi.connectionInfo
|
|
177
|
+
val ipAddress = info.ipAddress
|
|
178
|
+
val stringIp = String.format(
|
|
179
|
+
"%d.%d.%d.%d",
|
|
180
|
+
ipAddress and 0xff,
|
|
181
|
+
ipAddress shr 8 and 0xff,
|
|
182
|
+
ipAddress shr 16 and 0xff,
|
|
183
|
+
ipAddress shr 24 and 0xff
|
|
184
|
+
)
|
|
185
|
+
promise.resolve(stringIp)
|
|
153
186
|
} catch (e: Exception) {
|
|
154
|
-
promise.resolve(
|
|
187
|
+
promise.resolve(null)
|
|
155
188
|
}
|
|
156
189
|
}.start()
|
|
157
190
|
}
|
|
158
191
|
|
|
159
|
-
// MARK: -
|
|
192
|
+
// MARK: - getSubnet
|
|
160
193
|
|
|
161
|
-
override fun
|
|
194
|
+
override fun getSubnet(promise: Promise) {
|
|
162
195
|
Thread {
|
|
163
196
|
try {
|
|
164
|
-
val
|
|
165
|
-
|
|
197
|
+
val interfaces = NetworkInterface.getNetworkInterfaces()
|
|
198
|
+
while (interfaces.hasMoreElements()) {
|
|
199
|
+
val iface = interfaces.nextElement()
|
|
200
|
+
if (iface.isLoopback || !iface.isUp) continue
|
|
201
|
+
|
|
202
|
+
val addresses = iface.inetAddresses
|
|
203
|
+
for (address in iface.interfaceAddresses) {
|
|
204
|
+
val addr = addresses.nextElement()
|
|
205
|
+
if (addr is Inet6Address) continue
|
|
206
|
+
|
|
207
|
+
promise.resolve(intToIP(address.networkPrefixLength.toInt()))
|
|
208
|
+
return@Thread
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
promise.resolve("0.0.0.0")
|
|
166
212
|
} catch (e: Exception) {
|
|
167
213
|
promise.resolve("0.0.0.0")
|
|
168
214
|
}
|
|
169
215
|
}.start()
|
|
170
216
|
}
|
|
171
217
|
|
|
172
|
-
// MARK: -
|
|
218
|
+
// MARK: - getGatewayIPAddress
|
|
173
219
|
|
|
174
|
-
override fun
|
|
220
|
+
override fun getGatewayIPAddress(promise: Promise) {
|
|
175
221
|
Thread {
|
|
176
222
|
try {
|
|
177
|
-
|
|
178
|
-
val
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
promise.resolve(address ?: "0.0.0.0")
|
|
223
|
+
@Suppress("DEPRECATION")
|
|
224
|
+
val dhcpInfo = wifi.dhcpInfo
|
|
225
|
+
val gatewayIPInt = dhcpInfo.gateway
|
|
226
|
+
val gatewayIP = String.format(
|
|
227
|
+
"%d.%d.%d.%d",
|
|
228
|
+
gatewayIPInt and 0xFF,
|
|
229
|
+
gatewayIPInt shr 8 and 0xFF,
|
|
230
|
+
gatewayIPInt shr 16 and 0xFF,
|
|
231
|
+
gatewayIPInt shr 24 and 0xFF
|
|
232
|
+
)
|
|
233
|
+
promise.resolve(gatewayIP)
|
|
193
234
|
} catch (e: Exception) {
|
|
194
|
-
promise.resolve(
|
|
235
|
+
promise.resolve(null)
|
|
195
236
|
}
|
|
196
237
|
}.start()
|
|
197
238
|
}
|
|
198
239
|
|
|
199
|
-
// MARK: -
|
|
240
|
+
// MARK: - getFrequency
|
|
200
241
|
|
|
201
|
-
override fun
|
|
242
|
+
override fun getFrequency(promise: Promise) {
|
|
202
243
|
Thread {
|
|
203
244
|
try {
|
|
204
|
-
|
|
205
|
-
val
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
if (!iface.isUp || iface.isLoopback) continue
|
|
209
|
-
for (ia in iface.interfaceAddresses) {
|
|
210
|
-
val addr = ia.address
|
|
211
|
-
if (addr is Inet4Address && !addr.isLoopbackAddress) {
|
|
212
|
-
val prefixLen = ia.networkPrefixLength.toInt()
|
|
213
|
-
val mask = prefixLengthToSubnetMask(prefixLen)
|
|
214
|
-
subnet = mask
|
|
215
|
-
break@loop
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
promise.resolve(subnet ?: "0.0.0.0")
|
|
245
|
+
@Suppress("DEPRECATION")
|
|
246
|
+
val info: WifiInfo = wifi.connectionInfo
|
|
247
|
+
val frequency = info.frequency.toDouble()
|
|
248
|
+
promise.resolve(frequency)
|
|
220
249
|
} catch (e: Exception) {
|
|
221
|
-
promise.resolve(
|
|
250
|
+
promise.resolve(null)
|
|
222
251
|
}
|
|
223
252
|
}.start()
|
|
224
253
|
}
|
|
225
254
|
|
|
226
|
-
//
|
|
227
|
-
|
|
228
|
-
private fun
|
|
229
|
-
val
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
return addr.hostAddress
|
|
255
|
+
// --- Private helpers (ported from upstream RNNetworkInfo.java) ---
|
|
256
|
+
|
|
257
|
+
private fun intToIP(ip: Int): String {
|
|
258
|
+
val finl = arrayOf("", "", "", "")
|
|
259
|
+
var k = 1
|
|
260
|
+
for (i in 0 until 4) {
|
|
261
|
+
for (j in 0 until 8) {
|
|
262
|
+
if (k <= ip) {
|
|
263
|
+
finl[i] += "1"
|
|
264
|
+
} else {
|
|
265
|
+
finl[i] += "0"
|
|
238
266
|
}
|
|
267
|
+
k++
|
|
239
268
|
}
|
|
240
269
|
}
|
|
241
|
-
return
|
|
270
|
+
return "${Integer.parseInt(finl[0], 2)}.${Integer.parseInt(finl[1], 2)}.${Integer.parseInt(finl[2], 2)}.${Integer.parseInt(finl[3], 2)}"
|
|
242
271
|
}
|
|
243
272
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
return String.format(
|
|
247
|
-
"%d.%d.%d.%d",
|
|
248
|
-
ip and 0xff,
|
|
249
|
-
ip shr 8 and 0xff,
|
|
250
|
-
ip shr 16 and 0xff,
|
|
251
|
-
ip shr 24 and 0xff
|
|
252
|
-
)
|
|
273
|
+
private fun inDSLITERange(ip: String): Boolean {
|
|
274
|
+
return DSLITE_LIST.contains(ip)
|
|
253
275
|
}
|
|
254
276
|
|
|
255
|
-
private fun
|
|
256
|
-
val
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
277
|
+
private fun getInetAddresses(): List<InterfaceAddress> {
|
|
278
|
+
val addresses = mutableListOf<InterfaceAddress>()
|
|
279
|
+
try {
|
|
280
|
+
val en = NetworkInterface.getNetworkInterfaces()
|
|
281
|
+
while (en.hasMoreElements()) {
|
|
282
|
+
val intf = en.nextElement()
|
|
283
|
+
for (interfaceAddress in intf.interfaceAddresses) {
|
|
284
|
+
addresses.add(interfaceAddress)
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
} catch (ex: Exception) {
|
|
288
|
+
android.util.Log.e(NAME, ex.toString())
|
|
289
|
+
}
|
|
290
|
+
return addresses
|
|
264
291
|
}
|
|
265
292
|
}
|
package/ios/NetworkInfo.h
CHANGED
package/ios/NetworkInfo.mm
CHANGED
|
@@ -305,6 +305,15 @@
|
|
|
305
305
|
});
|
|
306
306
|
}
|
|
307
307
|
|
|
308
|
+
// MARK: - getFrequency
|
|
309
|
+
|
|
310
|
+
- (void)getFrequency:(RCTPromiseResolveBlock)resolve
|
|
311
|
+
reject:(RCTPromiseRejectBlock)reject
|
|
312
|
+
{
|
|
313
|
+
// WiFi frequency is not available on iOS
|
|
314
|
+
resolve(nil);
|
|
315
|
+
}
|
|
316
|
+
|
|
308
317
|
// MARK: - getAllIPAddresses helper
|
|
309
318
|
|
|
310
319
|
- (NSDictionary *)getAllIPAddresses
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeNetworkInfo.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAAQ,cAAc;
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeNetworkInfo.ts"],"mappings":";;AAAA,SAASA,mBAAmB,QAAQ,cAAc;AAgBlD,eAAeA,mBAAmB,CAACC,YAAY,CAAO,eAAe,CAAC","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NativeNetworkInfo.d.ts","sourceRoot":"","sources":["../../../src/NativeNetworkInfo.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,OAAO,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAClC,QAAQ,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACnC,YAAY,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACvC,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACvC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACtC,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"NativeNetworkInfo.d.ts","sourceRoot":"","sources":["../../../src/NativeNetworkInfo.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAEhD,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,OAAO,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAClC,QAAQ,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACnC,YAAY,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACvC,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACvC,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAClC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IACtC,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7B,YAAY,IAAI,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CACxC;;AAED,wBAAuE"}
|
package/package.json
CHANGED
package/src/NativeNetworkInfo.ts
CHANGED
|
@@ -11,6 +11,7 @@ export interface Spec extends TurboModule {
|
|
|
11
11
|
getIPV4Address(): Promise<string>;
|
|
12
12
|
getWIFIIPV4Address(): Promise<string>;
|
|
13
13
|
getSubnet(): Promise<string>;
|
|
14
|
+
getFrequency(): Promise<number | null>;
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export default TurboModuleRegistry.getEnforcing<Spec>('RNNetworkInfo');
|