create Wifi Configuration - Android android.net.wifi

Android examples for android.net.wifi:WifiManager

Description

create Wifi Configuration

Demo Code

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

public class Main {

  public static WifiConfiguration createWifiConfiguration(WifiManager wifiManager, String ssid, String password,
      int type) {
    Log.d("", "test createWifiInfo ssid" + ssid + " password:" + password + " type:" + type);
    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    wifiConfiguration.allowedAuthAlgorithms.clear();
    wifiConfiguration.allowedGroupCiphers.clear();
    wifiConfiguration.allowedKeyManagement.clear();
    wifiConfiguration.allowedPairwiseCiphers.clear();
    wifiConfiguration.allowedProtocols.clear();
    {/*  w ww. ja  v  a2  s.co m*/
      wifiConfiguration.SSID = ssid;
      wifiConfiguration.allowedAuthAlgorithms.set(1);
      wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
      wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
      wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
      wifiConfiguration.allowedKeyManagement.set(0);
      wifiConfiguration.wepTxKeyIndex = 0;
      if (type == 1) {
        wifiConfiguration.wepKeys[0] = "";
        wifiConfiguration.allowedKeyManagement.set(0);
        wifiConfiguration.wepTxKeyIndex = 0;
      } else if (type == 2) {
        wifiConfiguration.hiddenSSID = true;
        wifiConfiguration.wepKeys[0] = password;
      } else if (type == 3) {
        wifiConfiguration.preSharedKey = password;
        wifiConfiguration.allowedAuthAlgorithms.set(0);
        wifiConfiguration.allowedProtocols.set(1);
        wifiConfiguration.allowedProtocols.set(0);
        wifiConfiguration.allowedKeyManagement.set(1);
        wifiConfiguration.allowedPairwiseCiphers.set(2);
        wifiConfiguration.allowedPairwiseCiphers.set(1);
      }
    }
    return wifiConfiguration;
  }

}

Related Tutorials