Skip to content

Commit b99e1d6

Browse files
authored
feat(android): Add BSSID WIFI type (#361 by @organom)
1 parent 216a2e5 commit b99e1d6

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ The `details` value depends on the `type` value.
265265
| ----------------------- | --------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------------------- |
266266
| `isConnectionExpensive` | Android, iOS, macOS, Windows, Web | `boolean` | If the network connection is considered "expensive". This could be in either energy or monetary terms. |
267267
| `ssid` | Android, iOS (not tvOS) | `string` | The SSID of the network. May not be present, `null`, or an empty string if it cannot be determined. **On iOS, make sure your app meets at least one of the [following requirements](https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo?language=objc#discussion). On Android, you need to have the `ACCESS_FINE_LOCATION` permission in your `AndroidManifest.xml` and accepted by the user**. |
268+
| `bssid` | Android, iOS (not tvOS) | `string` | The BSSID of the network. May not be present, `null`, or an empty string if it cannot be determined. **On iOS, make sure your app meets at least one of the [following requirements](https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo?language=objc#discussion). On Android, you need to have the `ACCESS_FINE_LOCATION` permission in your `AndroidManifest.xml` and accepted by the user**. |
268269
| `strength` | Android | `number` | An integer number from `0` to `5` for the signal strength. May not be present if the signal strength cannot be determined. |
269270
| `ipAddress` | Android, iOS, macOS | `string` | The external IP address. Can be in IPv4 or IPv6 format. May not be present if it cannot be determined. |
270271
| `subnet` | Android, iOS, macOS | `string` | The subnet mask in IPv4 format. May not be present if it cannot be determined. |
@@ -415,6 +416,7 @@ You can optionally send an `interface` string so the `Promise` resolves to a [`N
415416
```javascript
416417
NetInfo.fetch("wifi").then(state => {
417418
console.log("SSID", state.details.ssid);
419+
console.log("BSSID", state.details.bssid);
418420
console.log("Is connected?", state.isConnected);
419421
});
420422
```

android/src/main/java/com/reactnativecommunity/netinfo/ConnectivityReceiver.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,17 @@ private WritableMap createDetailsMap(@Nonnull String detailsInterface) {
177177
// Ignore errors
178178
}
179179

180+
// Get the BSSID
181+
try {
182+
String bssid = wifiInfo.getBSSID();
183+
if (bssid != null) {
184+
details.putString("bssid", bssid);
185+
}
186+
} catch (Exception e) {
187+
// Ignore errors
188+
}
189+
190+
180191
// Get/parse the wifi signal strength
181192
try {
182193
int signalStrength =

ios/RNCNetInfo.m

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ - (NSDictionary *)currentDictionaryFromUpdateState:(RNCConnectionState *)state w
106106
if (connected) {
107107
details[@"isConnectionExpensive"] = @(state.expensive);
108108
}
109-
109+
110110
return @{
111111
@"type": selectedInterface,
112112
@"isConnected": @(connected),
@@ -125,6 +125,7 @@ - (NSMutableDictionary *)detailsFromInterface:(nonnull NSString *)requestedInter
125125
details[@"subnet"] = [self subnet] ?: NSNull.null;
126126
#if !TARGET_OS_TV && !TARGET_OS_OSX
127127
details[@"ssid"] = [self ssid] ?: NSNull.null;
128+
details[@"bssid"] = [self bssid] ?: NSNull.null;
128129
#endif
129130
}
130131
return details;
@@ -166,7 +167,7 @@ - (NSString *)ipAddress
166167
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
167168
}
168169
}
169-
170+
170171
temp_addr = temp_addr->ifa_next;
171172
}
172173
}
@@ -200,7 +201,7 @@ - (NSString *)subnet
200201
subnet = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_netmask)->sin_addr)];
201202
}
202203
}
203-
204+
204205
temp_addr = temp_addr->ifa_next;
205206
}
206207
}
@@ -227,6 +228,22 @@ - (NSString *)ssid
227228
}
228229
return SSID;
229230
}
231+
232+
- (NSString *)bssid
233+
{
234+
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
235+
NSDictionary *networkDetails;
236+
NSString *BSSID = NULL;
237+
for (NSString *interfaceName in interfaceNames) {
238+
networkDetails = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
239+
if (networkDetails.count > 0)
240+
{
241+
BSSID = networkDetails[(NSString *) kCNNetworkInfoKeyBSSID];
242+
break;
243+
}
244+
}
245+
return BSSID;
246+
}
230247
#endif
231248

232249
@end

src/internal/defaultConfiguration.web.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ export default {
33
reachabilityTest: (response: Response): Promise<boolean> =>
44
Promise.resolve(response.status === 200),
55
reachabilityShortTimeout: 5 * 1000, // 5s
6-
reachabilityLongTimeout: 60 * 1000, // 60s
6+
reachabilityLongTimeout: 60 * 1000, // 60s
77
reachabilityRequestTimeout: 15 * 1000, // 15s
88
};

0 commit comments

Comments
 (0)