WifiConfiguration在Lollipop中启用网络 [英] WifiConfiguration enable network in Lollipop

查看:122
本文介绍了WifiConfiguration在Lollipop中启用网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当时正在从事Wifi项目,有一个模块可以使用户以编程方式加入wifi.

I was working on Wifi project, there is a module that enable user to join wifi programatically.

在kitkat及以下版本中,它可以成功运行,但在Lollipop中,则不能正常运行.

In kitkat and below it's working successfully, but in Lollipop it's not working.

这是代码:

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上的错误有什么问题吗?

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.

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

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

这篇关于WifiConfiguration在Lollipop中启用网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆