无法在Android 5.0上添加新的WiFi网络

时间:2014-11-17 21:24:43

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

我在Android< 5.0中使用此代码时没有任何问题:

// //////////// save network
WifiConfiguration wc = new WifiConfiguration();
ConfigurationSecuritiesV8 conf = new ConfigurationSecuritiesV8();
conf.setupSecurity(wc, conf.getScanResultSecurity(scanResult), password);
wc.BSSID = scanResult.BSSID;
wc.SSID = "\"" + scanResult.SSID.replace("\"", "") + "\"";

int id = wifiManager.addNetwork(wc);
if (id == -1)
    return RESULT_CANT_CREATE_NETWORK;

我只是在我的Nexus 5上安装了新的Android 5.0,现在我无法以编程方式添加网络。

1 个答案:

答案 0 :(得分:3)

我发现了问题,我需要将代码更改为

if (Build.VERSION.SDK_INT >= 21)
    wc.SSID = "" + scanResult.SSID.replace("\"", "") + "";
else
    wc.SSID = "\"" + scanResult.SSID.replace("\"", "") + "\"";