Android examples for android.net.wifi:WifiManager
Create a configuration file for the specifies WiFi
import android.net.wifi.WifiConfiguration; import android.util.Log; public class Main { public static final int SECURITY_WEP = 1; public static final int SECURITY_PSK = 2; public static final int SECURITY_NONE = 0; public static final int SECURITY_EAP = 3; private static final String TAG = "WifiUtils"; /**//w w w . j a v a 2s .c om * Create a configuration file for the specifies WiFi. * * @param networkSSID * The SSID of the network * @param networkPass * The password of the network * @param typeOfEncryption * The encryption of the network * @return The desired configuration in order to connect to the wifi */ public static WifiConfiguration createConfigFile(String networkSSID, String networkPass, String typeOfEncryption) { WifiConfiguration wifiConfiguration = new WifiConfiguration(); wifiConfiguration.SSID = "\"" + networkSSID + "\""; // Please note the quotes. String should contain ssid in quotes // Case of WPA switch (Integer.valueOf(typeOfEncryption)) { case SECURITY_NONE: Log.d(TAG, "SECURITY_NONE"); wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); break; case SECURITY_WEP: Log.d(TAG, "SECURITY_WEP"); wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); if (networkPass.length() != 0) { int length = networkPass.length(); // String password = networkPass.getText().toString(); // WEP-40, WEP-104, and 256-bit WEP (WEP-232?) if ((length == 10 || length == 26 || length == 58) && networkPass.matches("[0-9A-Fa-f]*")) { wifiConfiguration.wepKeys[0] = networkPass; } else { wifiConfiguration.wepKeys[0] = '"' + networkPass + '"'; } } break; case SECURITY_PSK: Log.d(TAG, "SECURITY_PSK"); wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); if (networkPass.length() != 0) { // String password = mPasswordView.getText().toString(); if (networkPass.matches("[0-9A-Fa-f]{64}")) { wifiConfiguration.preSharedKey = networkPass; } else { wifiConfiguration.preSharedKey = '"' + networkPass + '"'; } } break; case SECURITY_EAP: wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); break; default: break; } wifiConfiguration.status = WifiConfiguration.Status.ENABLED; return wifiConfiguration; } }