diff --git a/AUTHORS b/AUTHORS index f497b6c6ad5d..5ab83295ae9e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -27,6 +27,7 @@ Sarthak Verma Mike Diarmid Invertase Elliot Hesp +Vince Varga Aawaz Gyawali EUI Limited Katarina Sheremet diff --git a/packages/connectivity/CHANGELOG.md b/packages/connectivity/CHANGELOG.md index 65270ebfa150..bbd924fb29c7 100644 --- a/packages/connectivity/CHANGELOG.md +++ b/packages/connectivity/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.3 + +* Add getWifiBSSID to obtain current wifi network's BSSID. + ## 0.4.2+2 * Add integration test. diff --git a/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java index 5694df6006ca..05b4608723f9 100644 --- a/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java +++ b/packages/connectivity/android/src/main/java/io/flutter/plugins/connectivity/ConnectivityPlugin.java @@ -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; @@ -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); diff --git a/packages/connectivity/example/lib/main.dart b/packages/connectivity/example/lib/main.dart index 243412e1edd1..3784a22fc241 100644 --- a/packages/connectivity/example/lib/main.dart +++ b/packages/connectivity/example/lib/main.dart @@ -87,27 +87,33 @@ class _MyHomePageState extends State { Future _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; diff --git a/packages/connectivity/example/test_driver/connectivity.dart b/packages/connectivity/example/test_driver/connectivity.dart index 634a00985d8a..7efba5e4ab50 100644 --- a/packages/connectivity/example/test_driver/connectivity.dart +++ b/packages/connectivity/example/test_driver/connectivity.dart @@ -20,7 +20,8 @@ void main() { expect(result, isNotNull); switch (result) { case ConnectivityResult.wifi: - expect((_connectivity.getWifiName()), completes); + expect(_connectivity.getWifiName(), completes); + expect(_connectivity.getWifiBSSID(), completes); expect((await _connectivity.getWifiIP()), isNotNull); break; default: diff --git a/packages/connectivity/ios/Classes/ConnectivityPlugin.m b/packages/connectivity/ios/Classes/ConnectivityPlugin.m index 722b797cff3a..e92fff220a2e 100644 --- a/packages/connectivity/ios/Classes/ConnectivityPlugin.m +++ b/packages/connectivity/ios/Classes/ConnectivityPlugin.m @@ -33,18 +33,25 @@ + (void)registerWithRegistrar:(NSObject*)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 { @@ -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 { diff --git a/packages/connectivity/lib/connectivity.dart b/packages/connectivity/lib/connectivity.dart index 8726278426ac..808e7dcc0e52 100644 --- a/packages/connectivity/lib/connectivity.dart +++ b/packages/connectivity/lib/connectivity.dart @@ -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 getWifiBSSID() async { + return await methodChannel.invokeMethod('wifiBSSID'); + } + /// Obtains the IP address of the connected wifi network Future getWifiIP() async { return await methodChannel.invokeMethod('wifiIPAddress'); diff --git a/packages/connectivity/pubspec.yaml b/packages/connectivity/pubspec.yaml index 8eff3e6e2bff..3ed7aae0c109 100644 --- a/packages/connectivity/pubspec.yaml +++ b/packages/connectivity/pubspec.yaml @@ -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 homepage: https://github.com/flutter/plugins/tree/master/packages/connectivity -version: 0.4.2+2 +version: 0.4.3 flutter: plugin: diff --git a/packages/connectivity/test/connectivity_test.dart b/packages/connectivity/test/connectivity_test.dart index 5dc2cbb0fa94..8e1b435c019e 100644 --- a/packages/connectivity/test/connectivity_test.dart +++ b/packages/connectivity/test/connectivity_test.dart @@ -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: @@ -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, + [ + isMethodCall( + 'wifiBSSID', + arguments: null, + ), + ], + ); + }); + test('getWifiIP', () async { final String result = await Connectivity().getWifiIP(); expect(result, '127.0.0.1');