我在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,现在我无法以编程方式添加网络。
答案 0 :(得分:3)
我发现了问题,我需要将代码更改为
if (Build.VERSION.SDK_INT >= 21)
wc.SSID = "" + scanResult.SSID.replace("\"", "") + "";
else
wc.SSID = "\"" + scanResult.SSID.replace("\"", "") + "\"";