connect Access Point via SSID, password and cipher Type - Android android.net.wifi

Android examples for android.net.wifi:Wifi SSID

Description

connect Access Point via SSID, password and cipher Type

Demo Code

import java.util.List;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

public class Main {

  public static final int WIFICIPHER_WEP = 0;
  public static final int WIFICIPHER_NOPASS = 2;
  public static final int WIFICIPHER_WPA = 1;
  private final static String TAG = "WifiUtils";

  public static boolean connectAccessPoint(WifiManager wifiManager, String ssid, String pwd, int cipherType) {
    if (wifiManager == null || ssid == null) {
      return false;
    }/*from  ww  w  .  j a  v a2  s.c o m*/
    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + ssid + "\"";
    WifiConfiguration tmpConfig = isExists(wifiManager, ssid);
    if (tmpConfig != null) {
      boolean flag = wifiManager.disableNetwork(tmpConfig.networkId);
      Log.d(TAG, "removeNetwork result=" + flag + " tmp config ssid:" + tmpConfig.SSID);
    }
    if (cipherType == WIFICIPHER_NOPASS) {
      config.wepKeys[0] = "";
      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
      config.wepTxKeyIndex = 0;
    } else if (cipherType == WIFICIPHER_WEP) {
      config.wepKeys[0] = "\"" + pwd + "\"";
      config.hiddenSSID = true;
      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;
    } else if (cipherType == WIFICIPHER_WPA) {
      config.preSharedKey = "\"" + pwd + "\"";
      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;
    } else {
      return false;
    }
    int networkId = wifiManager.addNetwork(config);
    boolean result = wifiManager.enableNetwork(networkId, true);
    Log.d(TAG, "connectToHostAP networkId==" + networkId + " result=" + result);
    return result;
  }

  private static WifiConfiguration isExists(WifiManager wifiManager, String ssid) {
    List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
    if (configs != null) {
      for (WifiConfiguration config : configs) {
        if (config.SSID.equals("\"" + ssid + "\"")) {
          return config;
        }
      }
    }
    return null;
  }

}

Related Tutorials