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

Update to Functionality of Android Library #28

Open
wants to merge 5 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ pubspec.lock
# Directory created by dartdoc
# If you don't generate documentation locally you can remove this line.
doc/api/
android/.classpath
android/.project
android/.settings/org.eclipse.buildship.core.prefs
87 changes: 67 additions & 20 deletions android/src/main/java/com/ly/wifi/WifiDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,11 @@ public void connection(MethodCall methodCall, MethodChannel.Result result) {
private void connection() {
String ssid = methodCall.argument("ssid");
String password = methodCall.argument("password");
WifiConfiguration wifiConfig = createWifiConfig(ssid, password);
String security = methodCall.argument("security");
boolean hidden = methodCall.argument("hidden");
WifiConfiguration wifiConfig = createWifiConfig(ssid, password, security, hidden);
if (wifiConfig == null) {
Log.d("connection", "wifi config is null");
finishWithError("unavailable", "wifi config is null!");
return;
}
Expand All @@ -238,6 +241,7 @@ private void connection() {
// support Android O
// https://stackoverflow.com/questions/50462987/android-o-wifimanager-enablenetwork-cannot-work
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
result.success(1);
Expand All @@ -248,26 +252,69 @@ private void connection() {
}
}

private WifiConfiguration createWifiConfig(String ssid, String Password) {
WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"" + ssid + "\"";
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
WifiConfiguration tempConfig = isExist(wifiManager, ssid);
if (tempConfig != null) {
wifiManager.removeNetwork(tempConfig.networkId);
private WifiConfiguration createWifiConfig(String ssid, String password, String security, boolean hidden) {
WifiConfiguration prevConfig = isExist(wifiManager, ssid);
if (prevConfig != null) {
Log.d("createWifiConfig", "removing previous config!");
Log.d("createWifiConfig", "ssid: " + prevConfig.SSID);
Log.d("createWifiConfig", "networkId: " + prevConfig.networkId);
if (wifiManager.removeNetwork(prevConfig.networkId)) {
Log.d("createWifiConfig", "removed previous config!");
} else {
Log.d("createWifiConfig", "failed to remove previous config!");
return null;
}

} else {
Log.d("createWifiConfig", "no previous config!");
}

WifiConfiguration config = new WifiConfiguration();
config.SSID = "\"".concat(ssid).concat("\"");
config.status = WifiConfiguration.Status.DISABLED;
config.priority = 40;

config.hiddenSSID = hidden;
switch (security) {
case "WEP":
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.wepKeys[0] = "\"".concat(password).concat("\"");
config.wepTxKeyIndex = 0;
break;
case "WPA":
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

config.preSharedKey = "\"".concat(password).concat("\"");
break;
default:
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
config.allowedAuthAlgorithms.clear();
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
break;
}
config.preSharedKey = "\"" + Password + "\"";
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);
config.status = WifiConfiguration.Status.ENABLED;
return config;
}
Expand Down
8 changes: 7 additions & 1 deletion lib/wifi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ class Wifi {
return resultList;
}

static Future<WifiState> connection(String ssid, String password) async {
static Future<WifiState> connection(
String ssid,
String password,
{String security: "WPA", bool hidden: false}
) async {
final Map<String, dynamic> params = {
'ssid': ssid,
'password': password,
'security': security,
'hidden': hidden
};
int state = await _channel.invokeMethod('connection', params);
switch (state) {
Expand Down