Skip to content Skip to sidebar Skip to footer

Wificonfiguration Enable Network In Lollipop

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. Her

Solution 1:

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;
}

Solution 2:

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.
 */privatestaticfinalintMAX_PRIORITY=999999;

/**
 * Allow a previously configured network to be associated with.
 */publicbooleanenableNetwork(String ssid) {
    booleanstate=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();

                intnewPri= 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;
}

privateintgetMaxPriority() {
    final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
    intpri=0;
    for (final WifiConfiguration config : configurations) {
        if (config.priority > pri) {
            pri = config.priority;
        }
    }
    return pri;
}

privatevoidsortByPriority(final List<WifiConfiguration> configurations) {
    Collections.sort(configurations,
        newComparator<WifiConfiguration>() {
            @Overridepublicintcompare(WifiConfiguration object1, WifiConfiguration object2) {
                return object1.priority - object2.priority;
            }
        });
}

privateintshiftPriorityAndSave() {
    final List<WifiConfiguration> configurations = wifiManager.getConfiguredNetworks();
    sortByPriority(configurations);
    finalintsize= configurations.size();
    for (inti=0; i < size; i++) {
        finalWifiConfigurationconfig= configurations.get(i);
        config.priority = i;
        wifiManager.updateNetwork(config);
    }
    wifiManager.saveConfiguration();
    return size;
}

/**
 * Add quotes to string if not already present.
 *
 * @param string
 * @return
 */publicstatic String convertToQuotedString(String string) {
    if (TextUtils.isEmpty(string)) {
        return"";
    }

    finalintlastPos= string.length() - 1;
    if (lastPos > 0
            && (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
        return string;
    }

    return"\"" + string + "\"";
}

Solution 3:

Try adding wifiManager.setWifiEnabled(true); at the end of your code.

Solution 4:

try setting false where you are trying to diable other networks.

wifiManager.enableNetwork(netId, false);

and one more thing if your addNetwork succeeds then you can proceed with the rest. if it returns a -1 you cannot call enableNetwork as the netId you are using would be -1. so what you could ideally do is

netId = wifiManager.addNetwork(wifiConfiguration);
if(netId >= 0){
Log.d("WifiPreference", "add Network returned " + netId);
booleancheckEnableWifi= wifiManager.enableNetwork(netId, true);
Log.d("WifiPreference", "enableNetwork returned " + checkEnableWifi);
}

Solution 5:

I was facing a situation on Nexus 7 connecting to hp printer in which the method enablenetwork(id,true) return true but still does not connect. Answer from @Santosh and @Seraphim helped me..Disabling each network and enabling only the required SSID worked.

Post a Comment for "Wificonfiguration Enable Network In Lollipop"