qq_20258523
qq_20258523
2017-09-22 01:31

android wifi连接记住密码并自动连接

  • 密码
  • wi-fi
  • android

想让链接过一次的WIFI再次点击之后不需要输入密码直接连接,需要用到哪些方法呢?

下面的是连接的代码,哪里需要改动?

public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type) {//
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
//config.SSID = SSID;
config.hiddenSSID = false;
WifiConfiguration tempConfig = isExsits(SSID);
if (tempConfig != null) {
Log.e("connectgid.", "exixtandClear : " + tempConfig.networkId);
wifiManager.removeNetwork(tempConfig.networkId);
}
Log.e("connectgid.", SSID + " " + Password);
if (Type == SECURITY_NONE) {
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}

    if (Type == SECURITY_WEP) {
        config.hiddenSSID = true;
        config.wepKeys[0] = "\"" + Password + "\"";
        config.allowedAuthAlgorithms
                .set(WifiConfiguration.AuthAlgorithm.SHARED);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        config.allowedGroupCiphers
                .set(WifiConfiguration.GroupCipher.WEP104);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.wepTxKeyIndex = 0;

    }

    if (Type == SECURITY_PSK) {
        config.preSharedKey = "\"" + Password + "\"";
        config.hiddenSSID = true;
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        config.status = WifiConfiguration.Status.ENABLED;
        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
        if (Password.length() != 0) {
            if (Password.matches("[0-9A-Fa-f]{8,}")) {
                Log.e("CreateWifiInfo", "password:" + Password);
                config.preSharedKey = Password;
            } else {
                config.preSharedKey = '"' + Password + '"';
            }
        }
    }

    if (Type == SECURITY_EAP) {
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
    }

    return config;
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答