I was working on Wifi project, there is a module that enable user to join wifi programatically.
In kitkat and below it's working successfully, but in Lollipop it's not working.
Here is the code :
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.SSID = "\"testSSID\"";
wifiConfiguration.hiddenSSID = true;
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
wifiConfiguration.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.NONE);
netId = wifiManager.addNetwork(wifiConfiguration);
Log.d("WifiPreference", "add Network returned " + netId);
boolean checkEnableWifi = wifiManager.enableNetwork(netId, true);
Log.d("WifiPreference", "enableNetwork returned " + checkEnableWifi);
my tested device is nexus 5 build number LRX21O, is ther something wrong in my code or bug on Lollipop?
Faced similar issue on lollipop.
Disabling other networks manually, an then reconnecting wifi manager solved the issue.
boolean enableNework(String ssid, Context cxt) {
boolean state = false;
WifiManager wm = (WifiManager) cxt.getSystemService(Context.WIFI_SERVICE);
if (wm.setWifiEnabled(true)) {
List<WifiConfiguration> networks = wm.getConfiguredNetworks();
Iterator<WifiConfiguration> iterator = networks.iterator();
while (iterator.hasNext()) {
WifiConfiguration wifiConfig = iterator.next();
if (wifiConfig.SSID.equals(ssid))
state = wm.enableNetwork(wifiConfig.networkId, true);
else
wm.disableNetwork(wifiConfig.networkId);
}
wm.reconnect();
}
return state;
}
Try this. Do not disable other saved networks. As it will hinder the fallback of wifi on other networks when your selected network is not available. Play with the priority.
/*
* Max priority of network to be associated.
*/
private static final int MAX_PRIORITY = 999999;
/**
* Allow a previously configured network to be associated with.
*/
public boolean enableNetwork(String ssid) {
boolean state = false;
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
if(list != null && list.size() > 0) {
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals(convertToQuotedString(ssid))) {
wifiManager.disconnect();
int newPri = getMaxPriority() + 1;
if(newPri >= MAX_PRIORITY) {
// We have reached a rare situation.
newPri = shiftPriorityAndSave();
}
i.priority = newPri;
wifiManager.updateNetwork(i);
wifiManager.saveConfiguration();
state = wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
return state;
}
private int getMaxPriority() {
final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
int pri = 0;
for (final WifiConfiguration config : configurations) {
if (config.priority > pri) {
pri = config.priority;
}
}
return pri;
}
private void sortByPriority(final List<WifiConfiguration> configurations) {
Collections.sort(configurations,
new Comparator<WifiConfiguration>() {
@Override
public int compare(WifiConfiguration object1, WifiConfiguration object2) {
return object1.priority - object2.priority;
}
});
}
private int shiftPriorityAndSave() {
final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
sortByPriority(configurations);
final int size = configurations.size();
for (int i = 0; i < size; i++) {
final WifiConfiguration config = configurations.get(i);
config.priority = i;
wifiManager.updateNetwork(config);
}
wifiManager.saveConfiguration();
return size;
}
/**
* Add quotes to string if not already present.
*
* @param string
* @return
*/
public static String convertToQuotedString(String string) {
if (TextUtils.isEmpty(string)) {
return "";
}
final int lastPos = string.length() - 1;
if (lastPos > 0
&& (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
return string;
}
return "\"" + string + "\"";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With