diff --git a/android/src/main/java/com/ly/wifi/WifiDelegate.java b/android/src/main/java/com/ly/wifi/WifiDelegate.java index c3b6f6f..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()) { @@ -193,16 +203,11 @@ 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); + maps.put("bssid", scanResult.BSSID); list.add(maps); - } else { - if (scanResult.SSID.contains(key)) { - maps.put("ssid", scanResult.SSID); - maps.put("level", level); - list.add(maps); - } } } } @@ -260,11 +265,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/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/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..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, @@ -26,16 +30,18 @@ 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; } - 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: @@ -53,6 +59,7 @@ class Wifi { class WifiResult { String ssid; int level; + String bssid; - WifiResult(this.ssid, this.level); + WifiResult(this.ssid, this.level, this.bssid); }