Which one is the best practice to connect Wifi und

2019-07-23 15:20发布

问题:

This is about network Wifi how to connect it under android. But the question is which one is the best practice between the Code shown at here.

And... this below? Because two of the source code given have a similarity but they're different in configuring the content. Please give a comment / clear answer.

Thanks

private void getConnected(){

        // for Wifi Connectivity
        obWifiManager = (WifiManager) getApplicationContext().getSystemService(
                Context.WIFI_SERVICE);

        WifiConfiguration config = new WifiConfiguration();
        config.SSID = "\""+objOpp.getName()+"\"";
        config.BSSID = objOpp.getID();
        config.priority = 1;
        config.preSharedKey = "\""+ "PASSWORD"+"\"";
        config.status = WifiConfiguration.Status.DISABLED;
        config.status = WifiConfiguration.Status.CURRENT; config.status = WifiConfiguration.Status.ENABLED;
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.NONE);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP);
        config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);

        obWifiManager.addNetwork(config);

        // start connection 
        obWifiManager.enableNetwork(config.networkId, true);
        obWifiManager.saveConfiguration();

    }