Android WifiConfiguration显示-1表示ID。如何修复它才能识别SSID?

时间:2014-02-21 15:03:46

标签: java android wireless wificonfiguration

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();

这是我用SSID和密码连接到无线WEP的代码,但是当我调试代码时,它显示WifiConfiguration的ID为-1。它导致netID的值为-1,并且没有与无线连接。请帮忙!谢谢。

1 个答案:

答案 0 :(得分:0)

我很确定配置不正确或只是尝试设置BSSID

wc.BSSID = yourBSSID;

因为你有一个隐藏的网络。您可以从SSID没有值的网络扫描中获取它。