WifiConfiguration在Lollipop中启用网络

时间:2014-11-18 03:20:04

标签: android android-wifi android-5.0-lollipop

我正在研究Wifi项目,有一个模块可以让用户以编程方式加入wifi。

在kitkat及以下版本中,它成功运作,但在Lollipop中它无效。

以下是代码:

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

我测试的设备是nexus 5内部版本编号LRX21O,我的代码中有什么问题或Lollipop上有错误吗?

5 个答案:

答案 0 :(得分:9)

在棒棒糖上遇到类似的问题。

手动禁用其他网络,然后重新连接的wifi管理器解决了这个问题。

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

答案 1 :(得分:6)

试试这个。不要禁用其他已保存的网络。因为当您选择的网络不可用时,它会阻碍其他网络上的wifi回退。优先玩。

/*
 *  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 + "\"";
}

答案 2 :(得分:0)

尝试添加wifiManager.setWifiEnabled(true);在代码的最后。

答案 3 :(得分:0)

尝试在您尝试播放其他网络的地方设置false。

wifiManager.enableNetwork(netId, false);

还有一件事,如果你的addNetwork成功,那么你可以继续其余的事情。如果它返回-1,则无法调用enableNetwork,因为您使用的netId将为-1。所以你理想的是

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

答案 4 :(得分:0)

我正面临Nexus 7连接到hp打印机的情况,其中方法enablenetwork(id,true)返回true但仍然无法连接。来自@Santosh和@Seraphim的回答帮助了我。禁用每个网络并只启用所需的SSID。