Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Wi-Fi adapter enabled state check on Android #39

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 18 additions & 9 deletions android/src/main/java/com/ly/wifi/WifiDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -193,16 +203,11 @@ private void launchWifiList() {
level = 0;
}
HashMap<String, Object> 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);
}
}
}
}
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/com/ly/wifi/WifiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion ios/Classes/WifiPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 12 additions & 5 deletions lib/wifi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,29 @@ class Wifi {
return await _channel.invokeMethod('ip');
}

static Future<bool> get wifiEnabled async {
return await _channel.invokeMethod('wifiEnabled');
}

static Future<List<WifiResult>> list(String key) async {
final Map<String, dynamic> params = {
'key': key,
};
var results = await _channel.invokeMethod('list', params);
List<WifiResult> 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<WifiState> connection(String ssid, String password) async {
static Future<WifiState> connection(String ssid, [String password]) async {
final Map<String, dynamic> 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:
Expand All @@ -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);
}