From e8a8175d10a66eb47903d24ccfb7de9d9cd1f8a8 Mon Sep 17 00:00:00 2001 From: Mattia Aracne Date: Wed, 6 Nov 2019 11:47:37 +0100 Subject: [PATCH 1/4] Add support for open Wi-Fi networks You just need to leave out the password field when calling the "connection" method. Also makes password an optional positional parameter to ensure compatibility with previous code. --- android/src/main/java/com/ly/wifi/WifiDelegate.java | 8 ++++++-- ios/Classes/WifiPlugin.m | 7 ++++++- lib/wifi.dart | 8 +++++--- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/ly/wifi/WifiDelegate.java b/android/src/main/java/com/ly/wifi/WifiDelegate.java index c3b6f6f..7552f38 100644 --- a/android/src/main/java/com/ly/wifi/WifiDelegate.java +++ b/android/src/main/java/com/ly/wifi/WifiDelegate.java @@ -260,11 +260,15 @@ private WifiConfiguration createWifiConfig(String ssid, String Password) { if (tempConfig != null) { wifiManager.removeNetwork(tempConfig.networkId); } - config.preSharedKey = "\"" + Password + "\""; + if (Password != null && !Password.isEmpty()) { + config.preSharedKey = "\"" + Password + "\""; + config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); + } else { + config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); + } config.hiddenSSID = true; config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); - config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); diff --git a/ios/Classes/WifiPlugin.m b/ios/Classes/WifiPlugin.m index 2bbe9e2..e068874 100644 --- a/ios/Classes/WifiPlugin.m +++ b/ios/Classes/WifiPlugin.m @@ -40,7 +40,12 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { NSDictionary* argsMap = call.arguments; NSString *ssid = argsMap[@"ssid"]; NSString *password = argsMap[@"password"]; - NEHotspotConfiguration * hotspotConfig = [[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:password isWEP:NO]; + NEHotspotConfiguration *hotspotConfig; + if ([password length] == 0) { + hotspotConfig = [[NEHotspotConfiguration alloc] initWithSSID:ssid]; + } else { + hotspotConfig = [[NEHotspotConfiguration alloc] initWithSSID:ssid passphrase:password isWEP:NO]; + } [[NEHotspotConfigurationManager sharedManager] applyConfiguration:hotspotConfig completionHandler:^(NSError * _Nullable error) { if(error == nil){ result(@1); diff --git a/lib/wifi.dart b/lib/wifi.dart index 835ae51..bd75ce5 100644 --- a/lib/wifi.dart +++ b/lib/wifi.dart @@ -31,11 +31,13 @@ class Wifi { return resultList; } - static Future connection(String ssid, String password) async { + static Future connection(String ssid, [String password]) async { final Map params = { - 'ssid': ssid, - 'password': password, + 'ssid': ssid }; + if (password != null && password.isNotEmpty) { + params.addEntries([MapEntry('password', password)]); + } int state = await _channel.invokeMethod('connection', params); switch (state) { case 0: From f0e029875d23abef30e375eb3ea21688cd689d06 Mon Sep 17 00:00:00 2001 From: Mattia Aracne Date: Mon, 4 Nov 2019 15:36:47 +0100 Subject: [PATCH 2/4] Simplify SSID filtering and remove code duplication --- android/src/main/java/com/ly/wifi/WifiDelegate.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/android/src/main/java/com/ly/wifi/WifiDelegate.java b/android/src/main/java/com/ly/wifi/WifiDelegate.java index 7552f38..e9081e3 100644 --- a/android/src/main/java/com/ly/wifi/WifiDelegate.java +++ b/android/src/main/java/com/ly/wifi/WifiDelegate.java @@ -193,16 +193,10 @@ private void launchWifiList() { level = 0; } HashMap maps = new HashMap<>(); - if (key.isEmpty()) { + if (key.isEmpty() || scanResult.SSID.contains(key)) { maps.put("ssid", scanResult.SSID); maps.put("level", level); list.add(maps); - } else { - if (scanResult.SSID.contains(key)) { - maps.put("ssid", scanResult.SSID); - maps.put("level", level); - list.add(maps); - } } } } From 65a7684da3de8e952f77256b1b4b5d0718bfbe2b Mon Sep 17 00:00:00 2001 From: Mattia Aracne Date: Mon, 4 Nov 2019 15:40:29 +0100 Subject: [PATCH 3/4] Add BSSID to WifiResult --- android/src/main/java/com/ly/wifi/WifiDelegate.java | 1 + lib/wifi.dart | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/ly/wifi/WifiDelegate.java b/android/src/main/java/com/ly/wifi/WifiDelegate.java index e9081e3..c8c0629 100644 --- a/android/src/main/java/com/ly/wifi/WifiDelegate.java +++ b/android/src/main/java/com/ly/wifi/WifiDelegate.java @@ -196,6 +196,7 @@ private void launchWifiList() { if (key.isEmpty() || scanResult.SSID.contains(key)) { maps.put("ssid", scanResult.SSID); maps.put("level", level); + maps.put("bssid", scanResult.BSSID); list.add(maps); } } diff --git a/lib/wifi.dart b/lib/wifi.dart index bd75ce5..27e9d73 100644 --- a/lib/wifi.dart +++ b/lib/wifi.dart @@ -26,7 +26,7 @@ class Wifi { var results = await _channel.invokeMethod('list', params); List resultList = []; for (int i = 0; i < results.length; i++) { - resultList.add(WifiResult(results[i]['ssid'], results[i]['level'])); + resultList.add(WifiResult(results[i]['ssid'], results[i]['level'], results[i]['bssid'])); } return resultList; } @@ -55,6 +55,7 @@ class Wifi { class WifiResult { String ssid; int level; + String bssid; - WifiResult(this.ssid, this.level); + WifiResult(this.ssid, this.level, this.bssid); } From 604e67091e06cfce72197fc28e8f66e0ae219328 Mon Sep 17 00:00:00 2001 From: Mattia Aracne Date: Sat, 6 Jun 2020 15:05:48 +0200 Subject: [PATCH 4/4] Add support for Wi-Fi adapter enabled state check on Android --- android/src/main/java/com/ly/wifi/WifiDelegate.java | 10 ++++++++++ android/src/main/java/com/ly/wifi/WifiPlugin.java | 3 +++ lib/wifi.dart | 4 ++++ 3 files changed, 17 insertions(+) diff --git a/android/src/main/java/com/ly/wifi/WifiDelegate.java b/android/src/main/java/com/ly/wifi/WifiDelegate.java index c8c0629..deb4ce4 100644 --- a/android/src/main/java/com/ly/wifi/WifiDelegate.java +++ b/android/src/main/java/com/ly/wifi/WifiDelegate.java @@ -128,6 +128,16 @@ public void getIP(MethodCall methodCall, MethodChannel.Result result) { launchIP(); } + public void isWifiEnabled(MethodCall methodCall, MethodChannel.Result result) { + WifiManager wifiManager = (WifiManager) activity.getApplicationContext() + .getSystemService(Context.WIFI_SERVICE); + if (wifiManager != null) { + result.success(wifiManager.isWifiEnabled()); + } else { + result.error("unavailable", "wifi service not available.", null); + } + } + private void launchIP() { NetworkInfo info = ((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info != null && info.isConnected()) { diff --git a/android/src/main/java/com/ly/wifi/WifiPlugin.java b/android/src/main/java/com/ly/wifi/WifiPlugin.java index 68945cb..877cd44 100644 --- a/android/src/main/java/com/ly/wifi/WifiPlugin.java +++ b/android/src/main/java/com/ly/wifi/WifiPlugin.java @@ -54,6 +54,9 @@ public void onMethodCall(MethodCall call, Result result) { case "ip": delegate.getIP(call, result); break; + case "wifiEnabled": + delegate.isWifiEnabled(call, result); + break; case "list": delegate.getWifiList(call, result); break; diff --git a/lib/wifi.dart b/lib/wifi.dart index 27e9d73..b8b05f7 100644 --- a/lib/wifi.dart +++ b/lib/wifi.dart @@ -19,6 +19,10 @@ class Wifi { return await _channel.invokeMethod('ip'); } + static Future get wifiEnabled async { + return await _channel.invokeMethod('wifiEnabled'); + } + static Future> list(String key) async { final Map params = { 'key': key,