1. 程式人生 > >Android 獲取wifi的加密方式

Android 獲取wifi的加密方式

Android 獲取當前wifi的加密方式

由於有需求是搜尋附近的網路,通過賬號和密碼連線wifi,但是連線的時候需要知道連線的wifi加密方式

1.一般WIFI加密有幾種方式

  • WPA-PSK/WPA2-PSK(目前最安全家用加密)
  • WPA/WPA2(較不安全)
  • WEP(安全較差)
  • AP(迄今最安全的)

    jiami

2.獲取WiFi加密方式一(不推薦)

 public void getCipherType(Context context, String ssid) {
        WifiManager wifiManager = (WifiManager) context.
getSystemService(Context.WIFI_SERVICE); List<ScanResult> list = wifiManager.getScanResults(); for (ScanResult scResult : list) { if (!TextUtils.isEmpty(scResult.SSID) && scResult.SSID.equals(ssid)) { String capabilities = scResult.capabilities; Log
.i("river","capabilities=" + capabilities); if (!TextUtils.isEmpty(capabilities)) { if (capabilities.contains("WPA") || capabilities.contains("wpa")) { Log.i("river", "wpa"); } else if (capabilities.contains("WEP") ||
capabilities.contains("wep")) { Log.i("river", "wep"); } else { Log.i("river", "no"); } } } } }

注意:此方法獲取不到隱藏的網路加密方式

3.獲取WiFi加密方式二(推薦)

於是檢視系統設定原始碼中檢視,系統是如何獲取到隱藏網路的加密方式

    /** 
     * These values are matched in string arrays -- changes must be kept in sync 
     */  
    static final int SECURITY_NONE = 0;  
    static final int SECURITY_WEP = 1;  
    static final int SECURITY_PSK = 2;  
    static final int SECURITY_EAP = 3;  

    static int getSecurity(WifiConfiguration config) {  
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {  
            return SECURITY_PSK;  
        }  
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {  
            return SECURITY_EAP;  
        }  
        return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;  
    }  

測試程式碼

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = mWifiManager.getConnectionInfo();


        // 得到配置好的網路連線
        List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();

        for (WifiConfiguration wifiConfiguration : wifiConfigList) {
            //配置過的SSID
            String configSSid = wifiConfiguration.SSID;
            configSSid = configSSid.replace("\"", "");

            //當前連線SSID
            String currentSSid =info.getSSID();
            currentSSid = currentSSid.replace("\"", "");

            //比較networkId,防止配置網路儲存相同的SSID
            if (currentSSid.equals(configSSid)&&info.getNetworkId()==wifiConfiguration.networkId) {
                Log.e("river", "當前網路安全性:" + getSecurity(wifiConfiguration));
            }
        }
    }

    /**
     * These values are matched in string arrays -- changes must be kept in sync
     */
    static final int SECURITY_NONE = 0;
    static final int SECURITY_WEP = 1;
    static final int SECURITY_PSK = 2;
    static final int SECURITY_EAP = 3;

    static int getSecurity(WifiConfiguration config) {
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
            return SECURITY_PSK;
        }
        if (config.allowedKeyManagement.get(KeyMgmt.WPA_EAP) || config.allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
            return SECURITY_EAP;
        }
        return (config.wepKeys[0] != null) ? SECURITY_WEP : SECURITY_NONE;
    }