类android.net.wifi.WifiConfiguration.KeyMgmt源码实例Demo

下面列出了怎么用android.net.wifi.WifiConfiguration.KeyMgmt的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: wifi-password-android   文件: Patch.java
private String getWiFiPassword(Context context, int networkId) {
    final WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    @SuppressWarnings("unchecked")
    final List<WifiConfiguration> list = (List<WifiConfiguration>) XposedHelpers.callMethod(wifiManager, "getPrivilegedConfiguredNetworks");

    String pwd;
    for (WifiConfiguration config : list) {
        if (config.networkId == networkId) {
            if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
                pwd = config.preSharedKey;
            } else {
                pwd = config.wepKeys[config.wepTxKeyIndex];
            }

            if (pwd != null) {
                return pwd.replaceAll("^\"|\"$", "");
            }
        }
    }

    return null;
}
 
源代码2 项目: DevUtils   文件: WifiUtils.java
/**
 * 获取 Wifi 加密类型
 * @param wifiConfig Wifi 配置信息
 * @return Wifi 加密类型
 */
public static int getSecurity(final WifiConfiguration wifiConfig) {
    if (wifiConfig == null) return SECURITY_NONE;
    if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
        return SECURITY_PSK;
    }
    if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_EAP)
            || wifiConfig.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
        return SECURITY_EAP;
    }
    return (wifiConfig.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
}
 
源代码3 项目: YiZhi   文件: NetworkConnectionUtils.java
/**
 * 连接指定
 *
 * @param manager
 * @param wifiSSID
 * @return
 */
public static boolean connectToSocketWifi(WifiManager manager, String wifiSSID) {
    LogUtils.i("要连接的socket wifi====>" + wifiSSID);
    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    wifiConfiguration.SSID = "\"" + wifiSSID + "\"";
    wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
    wifiConfiguration.wepKeys[0] = "\"" + "\""; //小米手机MIUI7/华为EMUI4.1 需要webKey

    int networkId = manager.addNetwork(wifiConfiguration);

    if (networkId != -1) {
        manager.enableNetwork(networkId, true);
        e("连接设备成功");
        return true;
    } else {
        e("第一次连接失败,尝试第二次。");
        WifiConfiguration wifiConfiguration2 = new WifiConfiguration();
        wifiConfiguration2.SSID = "\"" + wifiSSID + "\"";
        //wifiConfiguration.wepKeys[0] = "\"" + "\"";//去掉webKey  //小米手机MIUI8不能有webKey
        wifiConfiguration2.allowedKeyManagement.set(KeyMgmt.NONE);
        networkId = manager.addNetwork(wifiConfiguration2);
        if (networkId != -1) {
            manager.enableNetwork(networkId, true);
            e("连接设备成功");
            return true;
        }
        e("连接设备失败");
    }
    return false;
}
 
源代码4 项目: YiZhi   文件: NetworkConnectionUtils.java
/**
 * 获取要连接的wifi节点各个配置选项的加密类型
 *
 * @param ssid
 * @return wifiConfiguration
 */
public static WifiConfiguration getWifiConfiguration(WifiManager manager, String ssid, String
        password) {
    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    wifiConfiguration.SSID = "\"" + ssid + "\"";

    List<ScanResult> list = manager.getScanResults();
    for (ScanResult scResult : list) {
        if (ssid.equals(scResult.SSID)) {
            String capabilities = scResult.capabilities;
            LogUtils.i("capabilities=" + capabilities);
            if (capabilities.contains("WEP") || capabilities.contains("wep")) {
                wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
                wifiConfiguration.preSharedKey = "\"" + password + "\"";
                LogUtils.i("wep");
            } else if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
                wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
                wifiConfiguration.preSharedKey = "\"" + password + "\"";
                LogUtils.i("wpa");
            } else {
                wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
                LogUtils.i("none");
            }
        }
    }
    return wifiConfiguration;
}
 
源代码5 项目: WifiConnecter   文件: WiFi.java
/**
 * @return The security of a given {@link WifiConfiguration}.
 */
static public String getWifiConfigurationSecurity(WifiConfiguration wifiConfig) {

    if (wifiConfig.allowedKeyManagement.get(KeyMgmt.NONE)) {
        // If we never set group ciphers, wpa_supplicant puts all of them.
        // For open, we don't set group ciphers.
        // For WEP, we specifically only set WEP40 and WEP104, so CCMP
        // and TKIP should not be there.
        if (!wifiConfig.allowedGroupCiphers.get(GroupCipher.CCMP)
                && (wifiConfig.allowedGroupCiphers.get(GroupCipher.WEP40)
                        || wifiConfig.allowedGroupCiphers.get(GroupCipher.WEP104))) {
            return WEP;
        } else {
            return OPEN;
        }
    } else if (wifiConfig.allowedProtocols.get(Protocol.RSN)) {
        return WPA2;
    } else if (wifiConfig.allowedKeyManagement.get(KeyMgmt.WPA_EAP)) {
        return WPA_EAP;
    } else if (wifiConfig.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
        return IEEE8021X;
    } else if (wifiConfig.allowedProtocols.get(Protocol.WPA)) {
        return WPA;
    } else {
        Log.w(TAG, "Unknown security type from WifiConfiguration, falling back on open.");
        return OPEN;
    }
}
 
源代码6 项目: DevUtils   文件: WifiUtils.java
/**
     * 创建 Wifi 配置信息
     * @param ssid      Wifi ssid
     * @param pwd       Wifi 密码
     * @param type      Wifi 加密类型
     * @param isHandler 是否处理双引号
     * @return {@link WifiConfiguration}
     */
    public static WifiConfiguration createWifiConfig(final String ssid, final String pwd, final int type, final boolean isHandler) {
        try {
            // 创建一个新的网络配置
            WifiConfiguration wifiConfig = new WifiConfiguration();
            wifiConfig.allowedAuthAlgorithms.clear();
            wifiConfig.allowedGroupCiphers.clear();
            wifiConfig.allowedKeyManagement.clear();
            wifiConfig.allowedPairwiseCiphers.clear();
            wifiConfig.allowedProtocols.clear();
            wifiConfig.priority = 0;
            // 设置连接的 SSID
            if (isHandler) {
                wifiConfig.SSID = formatSSID(ssid, true);
            } else {
                wifiConfig.SSID = ssid;
            }
            switch (type) {
                case WifiUtils.NOPWD: // 不存在密码
                    wifiConfig.hiddenSSID = true;
                    wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
                    break;
                case WifiUtils.WEP: // WEP 加密方式
                    wifiConfig.hiddenSSID = true;
                    if (isHandler) {
                        if (isHexWepKey(pwd)) {
                            wifiConfig.wepKeys[0] = pwd;
                        } else {
                            wifiConfig.wepKeys[0] = "\"" + pwd + "\"";
                        }
                    } else {
                        wifiConfig.wepKeys[0] = pwd;
                    }
                    wifiConfig.allowedKeyManagement.set(KeyMgmt.NONE);
                    wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                    // 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);
                    wifiConfig.wepTxKeyIndex = 0;
                    break;
                case WifiUtils.WPA: // WPA 加密方式
                    if (isHandler) {
                        wifiConfig.preSharedKey = "\"" + pwd + "\"";
                    } else {
                        wifiConfig.preSharedKey = pwd;
                    }
                    wifiConfig.hiddenSSID = true;
                    wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                    wifiConfig.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
//					wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                    wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                    wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                    wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                    wifiConfig.status = WifiConfiguration.Status.ENABLED;
                    break;
            }
            return wifiConfig;
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "createWifiConfig");
        }
        return null;
    }
 
private WifiConfiguration createWifiInfo(String SSID, String Password,
		WifiCipherType 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;
	// nopass
	if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
		// config.wepKeys[0] = "";
		config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
		// config.wepTxKeyIndex = 0;
	}
	// wep
	if (Type == WifiCipherType.WIFICIPHER_WEP) {
		if (!TextUtils.isEmpty(Password)) {
			if (isHexWepKey(Password)) {
				config.wepKeys[0] = Password;
			} else {
				config.wepKeys[0] = "\"" + Password + "\"";
			}
		}
		config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
		config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
		config.allowedKeyManagement.set(KeyMgmt.NONE);
		config.wepTxKeyIndex = 0;
	}
	// wpa
	if (Type == WifiCipherType.WIFICIPHER_WPA) {
		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.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
		config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
		config.allowedPairwiseCiphers
				.set(WifiConfiguration.PairwiseCipher.CCMP);
		config.status = WifiConfiguration.Status.ENABLED;

	}
	return config;
}
 
private WifiConfiguration createWifiInfo(String SSID, String Password, WifiCipherType Type) {
	WifiConfiguration config = new WifiConfiguration();
	config.allowedAuthAlgorithms.clear();
	config.allowedGroupCiphers.clear();
	config.allowedKeyManagement.clear();
	config.allowedPairwiseCiphers.clear();
	config.allowedProtocols.clear();
	config.SSID = "\"" + SSID + "\"";
	// nopass
	if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
		// config.wepKeys[0] = "";
		config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
		// config.wepTxKeyIndex = 0;
	}
	// wep
	if (Type == WifiCipherType.WIFICIPHER_WEP) {
		if (!TextUtils.isEmpty(Password)) {
			if (isHexWepKey(Password)) {
				config.wepKeys[0] = Password;
			} else {
				config.wepKeys[0] = "\"" + Password + "\"";
			}
		}
		config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
		config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
		config.allowedKeyManagement.set(KeyMgmt.NONE);
		config.wepTxKeyIndex = 0;
	}
	// wpa
	if (Type == WifiCipherType.WIFICIPHER_WPA) {
		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.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
		config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
		config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
		config.status = WifiConfiguration.Status.ENABLED;

	}
	return config;
}
 
源代码9 项目: gokit-android   文件: WifiAutoConnectManager.java
private WifiConfiguration createWifiInfo(String SSID, String Password,
		WifiCipherType 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;
	// nopass
	if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
		// config.wepKeys[0] = "";
		config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
		// config.wepTxKeyIndex = 0;
	}
	// wep
	if (Type == WifiCipherType.WIFICIPHER_WEP) {
		if (!TextUtils.isEmpty(Password)) {
			if (isHexWepKey(Password)) {
				config.wepKeys[0] = Password;
			} else {
				config.wepKeys[0] = "\"" + Password + "\"";
			}
		}
		config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
		config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
		config.allowedKeyManagement.set(KeyMgmt.NONE);
		config.wepTxKeyIndex = 0;
	}
	// wpa
	if (Type == WifiCipherType.WIFICIPHER_WPA) {
		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.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
		config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
		config.allowedPairwiseCiphers
				.set(WifiConfiguration.PairwiseCipher.CCMP);
		config.status = WifiConfiguration.Status.ENABLED;

	}
	return config;
}
 
源代码10 项目: WifiConnecter   文件: WiFi.java
/**
    * Fill in the security fields of WifiConfiguration config.
    * @param config The object to fill.
    * @param security If is OPEN, password is ignored.
    * @param password Password of the network if security is not OPEN.
    */
static private void setupSecurity(WifiConfiguration config, String security, final String password) {
       config.allowedAuthAlgorithms.clear();
       config.allowedGroupCiphers.clear();
       config.allowedKeyManagement.clear();
       config.allowedPairwiseCiphers.clear();
       config.allowedProtocols.clear();
       
       if (TextUtils.isEmpty(security)) {
           security = OPEN;
           Log.w(TAG, "Empty security, assuming open");
       }
       
       if (security.equals(WEP)) {
       	 int wepPasswordType = WEP_PASSWORD_AUTO;
           // If password is empty, it should be left untouched
           if (!TextUtils.isEmpty(password)) {
			if (wepPasswordType == WEP_PASSWORD_AUTO) {
                   if (isHexWepKey(password)) {
                       config.wepKeys[0] = password;
                   } else {
                       config.wepKeys[0] = StringUtils.convertToQuotedString(password);
                   }
               } else {
                   config.wepKeys[0] = wepPasswordType == WEP_PASSWORD_ASCII
                           ? StringUtils.convertToQuotedString(password)
                           : password;
               }
           }
           
           config.wepTxKeyIndex = 0;
           
           config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
           config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);

           config.allowedKeyManagement.set(KeyMgmt.NONE);
           
           config.allowedGroupCiphers.set(GroupCipher.WEP40);
           config.allowedGroupCiphers.set(GroupCipher.WEP104);
           
       } else if (security.equals(WPA) || security.equals(WPA2)){
           config.allowedGroupCiphers.set(GroupCipher.TKIP);
           config.allowedGroupCiphers.set(GroupCipher.CCMP);
           
           config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
           
           config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
           config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);

           config.allowedProtocols.set(security.equals(WPA2) ? Protocol.RSN : Protocol.WPA);
           
           // If password is empty, it should be left untouched
           if (!TextUtils.isEmpty(password)) {
               if (password.length() == 64 && isHex(password)) {
                   // Goes unquoted as hex
                   config.preSharedKey = password;
               } else {
                   // Goes quoted as ASCII
                   config.preSharedKey = StringUtils.convertToQuotedString(password);
               }
           }
           
       } else if (security.equals(OPEN)) {
           config.allowedKeyManagement.set(KeyMgmt.NONE);
       } else if (security.equals(WPA_EAP) || security.equals(IEEE8021X)) {
           config.allowedGroupCiphers.set(GroupCipher.TKIP);
           config.allowedGroupCiphers.set(GroupCipher.CCMP);
           if (security.equals(WPA_EAP)) {
               config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
           } else {
               config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
           }
           if (!TextUtils.isEmpty(password)) {
               config.preSharedKey = StringUtils.convertToQuotedString(password);
           }
       }
   }
 
 类所在包
 同包方法