Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[connectivity] Enable fetching current Wi-Fi network's BSSID #1331

Merged
merged 15 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Sarthak Verma <sarthak@artiosys.com>
Mike Diarmid <mike@invertase.io>
Invertase <oss@invertase.io>
Elliot Hesp <elliot@invertase.io>
Vince Varga <vince.varga@smaho.com>
Aawaz Gyawali <awazgyawali@gmail.com>
EUI Limited <ian.evans3@admiralgroup.co.uk>
Katarina Sheremet <katarina@sheremet.ch>
Expand Down
4 changes: 4 additions & 0 deletions packages/connectivity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.3

* Add getWifiBSSID to obtain current wifi network's BSSID.

## 0.4.2+2

* Add integration test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public void onMethodCall(MethodCall call, Result result) {
case "wifiName":
handleWifiName(call, result);
break;
case "wifiBSSID":
handleBSSID(call, result);
break;
case "wifiIPAddress":
handleWifiIPAddress(call, result);
break;
Expand All @@ -99,21 +102,27 @@ private void handleCheck(MethodCall call, final Result result) {
}
}

private void handleWifiName(MethodCall call, final Result result) {
private WifiInfo getWifiInfo() {
WifiManager wifiManager =
(WifiManager) registrar.context().getSystemService(Context.WIFI_SERVICE);
return wifiManager == null ? null : wifiManager.getConnectionInfo();
}

WifiInfo wifiInfo = null;
if (wifiManager != null) wifiInfo = wifiManager.getConnectionInfo();

private void handleWifiName(MethodCall call, final Result result) {
WifiInfo wifiInfo = getWifiInfo();
String ssid = null;
if (wifiInfo != null) ssid = wifiInfo.getSSID();

if (ssid != null) ssid = ssid.replaceAll("\"", ""); // Android returns "SSID"

result.success(ssid);
}

private void handleBSSID(MethodCall call, MethodChannel.Result result) {
WifiInfo wifiInfo = getWifiInfo();
String bssid = null;
if (wifiInfo != null) bssid = wifiInfo.getBSSID();
result.success(bssid);
}

private void handleWifiIPAddress(MethodCall call, final Result result) {
WifiManager wifiManager =
(WifiManager) registrar.context().getSystemService(Context.WIFI_SERVICE);
Expand Down
16 changes: 11 additions & 5 deletions packages/connectivity/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,33 @@ class _MyHomePageState extends State<MyHomePage> {
Future<void> _updateConnectionStatus(ConnectivityResult result) async {
switch (result) {
case ConnectivityResult.wifi:
String wifiName, wifiIP;
String wifiName, wifiBSSID, wifiIP;

try {
wifiName = (await _connectivity.getWifiName()).toString();
wifiName = await _connectivity.getWifiName();
} on PlatformException catch (e) {
print(e.toString());

wifiName = "Failed to get Wifi Name";
}

try {
wifiIP = (await _connectivity.getWifiIP()).toString();
wifiBSSID = await _connectivity.getWifiBSSID();
} on PlatformException catch (e) {
print(e.toString());
wifiBSSID = "Failed to get Wifi BSSID";
}

wifiName = "Failed to get Wifi IP";
try {
wifiIP = await _connectivity.getWifiIP();
} on PlatformException catch (e) {
print(e.toString());
wifiIP = "Failed to get Wifi IP";
}

setState(() {
_connectionStatus = '$result\n'
'Wifi Name: $wifiName\n'
'Wifi BSSID: $wifiBSSID\n'
'Wifi IP: $wifiIP\n';
});
break;
Expand Down
2 changes: 2 additions & 0 deletions packages/connectivity/example/test_driver/connectivity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ void main() {
case ConnectivityResult.wifi:
expect((_connectivity.getWifiName()), completes);
expect((await _connectivity.getWifiIP()), isNotNull);
// Returns null on emulators so we just check that it doesn't throw
await _connectivity.getWifiBSSID();
collinjackson marked this conversation as resolved.
Show resolved Hide resolved
break;
default:
break;
Expand Down
27 changes: 18 additions & 9 deletions packages/connectivity/ios/Classes/ConnectivityPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
[streamChannel setStreamHandler:instance];
}

- (NSString*)getWifiName {
NSString* wifiName = nil;
NSArray* interFaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();

for (NSString* name in interFaceNames) {
NSDictionary* info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)name);
if (info[@"SSID"]) {
wifiName = info[@"SSID"];
- (NSString*)findNetworkInfo:(NSString*)key {
NSString* info = nil;
NSArray* interfaceNames = (__bridge_transfer id)CNCopySupportedInterfaces();
for (NSString* interfaceName in interfaceNames) {
NSDictionary* networkInfo =
(__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName);
if (networkInfo[key]) {
info = networkInfo[key];
}
}
return info;
}

- (NSString*)getWifiName {
return [self findNetworkInfo:@"SSID"];
}

return wifiName;
- (NSString*)getBSSID {
return [self findNetworkInfo:@"BSSID"];
}

- (NSString*)getWifiIP {
Expand Down Expand Up @@ -100,6 +107,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
result([self statusFromReachability:[Reachability reachabilityForInternetConnection]]);
} else if ([call.method isEqualToString:@"wifiName"]) {
result([self getWifiName]);
} else if ([call.method isEqualToString:@"wifiBSSID"]) {
result([self getBSSID]);
} else if ([call.method isEqualToString:@"wifiIPAddress"]) {
result([self getWifiIP]);
} else {
Expand Down
10 changes: 10 additions & 0 deletions packages/connectivity/lib/connectivity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ class Connectivity {
return wifiName;
}

/// Obtains the wifi BSSID of the connected network.
///
/// Please note that it DOESN'T WORK on emulators (returns null).
///
/// From Android 8.0 onwards the GPS must be ON (high accuracy)
/// in order to be able to obtain the BSSID.
Future<String> getWifiBSSID() async {
return await methodChannel.invokeMethod('wifiBSSID');
}

/// Obtains the IP address of the connected wifi network
Future<String> getWifiIP() async {
return await methodChannel.invokeMethod<String>('wifiIPAddress');
Expand Down
2 changes: 1 addition & 1 deletion packages/connectivity/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for discovering the state of the network (WiFi &
mobile/cellular) connectivity on Android and iOS.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity
version: 0.4.2+2
version: 0.4.3

flutter:
plugin:
Expand Down
16 changes: 16 additions & 0 deletions packages/connectivity/test/connectivity_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ void main() {
return 'wifi';
case 'wifiName':
return '1337wifi';
case 'wifiBSSID':
return 'c0:ff:33:c0:d3:55';
case 'wifiIPAddress':
return '127.0.0.1';
default:
Expand Down Expand Up @@ -59,6 +61,20 @@ void main() {
);
});

test('getWifiBSSID', () async {
final String result = await Connectivity().getWifiBSSID();
expect(result, 'c0:ff:33:c0:d3:55');
expect(
log,
<Matcher>[
isMethodCall(
'wifiBSSID',
arguments: null,
),
],
);
});

test('getWifiIP', () async {
final String result = await Connectivity().getWifiIP();
expect(result, '127.0.0.1');
Expand Down