Android WifiConfiguration shows -1 for ID. How can

2019-08-06 02:14发布

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条回答
成全新的幸福
2楼-- · 2019-08-06 02:44

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.

查看更多
登录 后发表回答