【发布时间】:2021-04-18 10:36:56
【问题描述】:
我已经从我的运行时提供了数据
ssid = "Some SSID";
password = "myPassword";
我想用它们连接到 WIFI。 到目前为止我的代码:
try {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.preSharedKey = "\"" + password + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
int netId = wifiManager.addNetwork(conf);
wifiManager.enableNetwork(netId, true);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
// ignore the returns
问题是,我的 addNetork() 返回 -1,因此我的 enableNetowrk() 失败。我愿意接受任何建议甚至重构。我也知道它适用于所有情况下的 WPA,所以不需要不同...
【问题讨论】: