Android WifiConfiguration shows -1 for ID. How can I fix it for SSID to be recognized?

北慕城南 提交于 2020-01-05 16:40:06

问题


String networkSSID = "networkName";
String networkPassword = "networkPassword";

WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

if (!wm.isWifiEnabled()) {

    if (wm.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {

        wm.setWifiEnabled(true);
    }
}

WifiConfiguration wifiConfig = new WifiConfiguration();

wifiConfig.allowedAuthAlgorithms.clear();
wifiConfig.allowedGroupCiphers.clear();
wifiConfig.allowedKeyManagement.clear();
wifiConfig.allowedPairwiseCiphers.clear();
wifiConfig.allowedProtocols.clear();

wifiConfig.SSID = "\"" + networkSSID + "\"";
wifiConfig.priority = 40;
wifiConfig.hiddenSSID = true;

wifiConfig.status = WifiConfiguration.Status.ENABLED;

wifiConfig.wepKeys[0] = "\"" + networkPassword + "\"";
wifiConfig.wepTxKeyIndex = 0;
wifiConfig.preSharedKey = "\"" + networkPassword + "\"";

wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);

int netId = wm.addNetwork(wifiConfig);

wm.disconnect();
wm.enableNetwork(netId, false);
wm.reconnect();

This is the code that I write to connect to wireless for WEP with SSID and password, but when I debug the code, it shows ID for WifiConfiguration is -1. It leads to netID having the value of -1, and there is no connection to wireless. Please help! Thanks.


回答1:


I'm pretty sure the configuration is not correct or simply try to set also the BSSID:

wc.BSSID = yourBSSID;

cause you've an hidden network. You can obtain it from a network scan where the SSID has no value.



来源:https://stackoverflow.com/questions/21937887/android-wificonfiguration-shows-1-for-id-how-can-i-fix-it-for-ssid-to-be-recog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!