Example usage for android.net.wifi WifiConfiguration WifiConfiguration

List of usage examples for android.net.wifi WifiConfiguration WifiConfiguration

Introduction

In this page you can find the example usage for android.net.wifi WifiConfiguration WifiConfiguration.

Prototype

public WifiConfiguration() 

Source Link

Usage

From source file:Main.java

public static WifiConfiguration createWifiCfg(String ssid) {
    WifiConfiguration wifiCfg = new WifiConfiguration();
    wifiCfg.SSID = ssid;//  w  w  w.j  a  v a  2 s.  c o  m
    //        wifiCfg.SSID = "\"" + ssid + "\"";
    wifiCfg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
    //        wifiCfg.preSharedKey = "abcdefgh";
    return wifiCfg;
}

From source file:Main.java

public static boolean connectToNetwork(Context context, String username, String password) {
    boolean status = false;
    try {//from  w  w w  .j  a va2  s.  c o  m
        WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

        if (!wifi.isWifiEnabled()) {
            wifi.setWifiEnabled(true);
            Thread.sleep(3000);
        }

        WifiConfiguration netConfig = new WifiConfiguration();
        netConfig.SSID = "\"" + username + "\"";
        netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        netConfig.preSharedKey = "\"" + password + "\"";
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

        int netId = wifi.addNetwork(netConfig);
        status = wifi.enableNetwork(netId, true);
    } catch (Exception e) {
        status = false;
        e.printStackTrace();
    }

    return status;
}

From source file:Main.java

private static WifiConfiguration getConfig(int networkId, String ssid, String password,
        int mAccessPointSecurity) {

    WifiConfiguration config = new WifiConfiguration();

    if (networkId == -1) {
        config.SSID = convertToQuotedString(ssid);
    } else {/*from w  ww .j  a v  a  2 s .c o  m*/
        config.networkId = networkId;
    }

    switch (mAccessPointSecurity) {
    case SECURITY_NONE:
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        break;

    case SECURITY_WEP:
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
        if (password.length() != 0) {
            int length = password.length();
            if ((length == 10 || length == 26 || length == 58) && password.matches("[0-9A-Fa-f]*")) {
                config.wepKeys[0] = password;
            } else {
                config.wepKeys[0] = '"' + password + '"';
            }
        }
        break;

    case SECURITY_PSK:
        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
        if (password.length() != 0) {

            if (password.matches("[0-9A-Fa-f]{64}")) {
                config.preSharedKey = password;
            } else {
                config.preSharedKey = '"' + password + '"';
            }
        }
        break;

    case SECURITY_EAP:
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
        if (password.length() != 0) {
            if (password.matches("[0-9A-Fa-f]{64}")) {
                config.preSharedKey = password;
            } else {
                config.preSharedKey = '"' + password + '"';
            }
        }
        break;

    default:
        return null;
    }

    return config;
}

From source file:Main.java

public static boolean disableAP(Context context, String ntId, String password) throws Exception {
    boolean apstatus = false;

    WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

    Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class     
    for (Method method : wmMethods) {
        if (method.getName().equals("setWifiApEnabled")) {

            WifiConfiguration netConfig = new WifiConfiguration();
            netConfig.SSID = ntId;//from  w  w  w .  j  a v a2 s  .c o m
            netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
            netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            netConfig.preSharedKey = password;
            netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
            netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
            netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

            apstatus = (Boolean) method.invoke(wifiManager, netConfig, false);
        }
    }

    return apstatus;
}

From source file:Main.java

public static void createWifiAccessPoint(Context context, String ntId, String password) {
    try {/*from w ww . ja v  a 2s .c  o  m*/
        WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);

        if (wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(false);
        }

        Method[] wmMethods = wifiManager.getClass().getDeclaredMethods(); //Get all declared methods in WifiManager class     
        for (Method method : wmMethods) {
            if (method.getName().equals("setWifiApEnabled")) {
                WifiConfiguration netConfig = new WifiConfiguration();
                netConfig.SSID = ntId;
                netConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
                netConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
                netConfig.preSharedKey = password;
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                netConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                netConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                try {
                    boolean apstatus = (Boolean) method.invoke(wifiManager, netConfig, true);

                    for (Method isWifiApEnabledmethod : wmMethods) {
                        if (isWifiApEnabledmethod.getName().equals("isWifiApEnabled")) {
                            while (!(Boolean) isWifiApEnabledmethod.invoke(wifiManager)) {
                            }
                            ;
                            for (Method method1 : wmMethods) {
                                if (method1.getName().equals("getWifiApState")) {
                                    int apstate;
                                    apstate = (Integer) method1.invoke(wifiManager);
                                }
                            }
                        }
                    }
                    if (apstatus) {
                        Log.d("better", "Access Point Created");
                    } else {
                        Log.d("better", "Failed to create Access Point!");
                    }

                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.google.android.apps.iosched.util.WiFiUtils.java

public static void installConferenceWiFi(final Context context) {
    // Create config
    WifiConfiguration config = new WifiConfiguration();
    // Must be in double quotes to tell system this is an ASCII SSID and passphrase.
    config.SSID = String.format("\"%s\"", Config.WIFI_SSID);
    config.preSharedKey = String.format("\"%s\"", Config.WIFI_PASSPHRASE);

    // Store config
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int netId = wifiManager.addNetwork(config);
    if (netId != -1) {
        wifiManager.enableNetwork(netId, false);
        boolean result = wifiManager.saveConfiguration();
        if (!result) {
            Log.e(TAG, "Unknown error while calling WiFiManager.saveConfiguration()");
            Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                    Toast.LENGTH_SHORT).show();
        }//from  w w  w.  j  a v  a  2s . c  o m
    } else {
        Log.e(TAG, "Unknown error while calling WiFiManager.addNetwork()");
        Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                Toast.LENGTH_SHORT).show();
    }
}

From source file:com.conferenceengineer.android.iosched.util.WiFiUtils.java

public static void installConferenceWiFi(final Context context) {
    // Create config
    WifiConfiguration config = new WifiConfiguration();
    // Must be in double quotes to tell system this is an ASCII SSID and passphrase.
    config.SSID = String.format("\"%s\"", Config.WIFI_SSID);
    config.preSharedKey = String.format("\"%s\"", Config.WIFI_PASSPHRASE);

    // Store config
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    int netId = wifiManager.addNetwork(config);
    if (netId != -1) {
        wifiManager.enableNetwork(netId, false);
        boolean result = wifiManager.saveConfiguration();
        if (!result) {
            Log.e(TAG, "Unknown error while calling WiFiManager.saveConfiguration()");
            Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                    Toast.LENGTH_SHORT).show();
        } else {//from   www . j  a  v a2 s  . co m
            Toast.makeText(context, context.getResources().getString(R.string.wifi_install_success_message),
                    Toast.LENGTH_SHORT).show();
        }
    } else {
        Log.e(TAG, "Unknown error while calling WiFiManager.addNetwork()");
        Toast.makeText(context, context.getResources().getString(R.string.wifi_install_error_message),
                Toast.LENGTH_SHORT).show();
    }
}

From source file:org.wahtod.wififixer.ui.ConnectFragment.java

private WifiConfiguration getKeyAppropriateConfig(String password) {
    WifiConfiguration wf = new WifiConfiguration();
    if (wf.toString().contains(BUGGED)) {
        /*//from  w w w.  jav a2  s. c  o  m
         * Add hidden fields on bugged Android 3.1+ configs
        */
        wf = addHiddenFields(wf);
    }
    wf.SSID = StringUtil.addQuotes(mNetwork.SSID);
    if (mNetwork.capabilities.length() == 0) {
        wf.BSSID = mNetwork.BSSID;
        wf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wf.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
        return wf;
    }

    wf.status = WifiConfiguration.Status.ENABLED;
    wf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
    if (mNetwork.capabilities.contains(WPA)) {
        wf.preSharedKey = StringUtil.addQuotes(password);
        wf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
    } else if (mNetwork.capabilities.contains(WEP)) {
        wf.wepKeys[0] = StringUtil.addQuotes(password);
        wf.wepTxKeyIndex = 0;
        wf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        wf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
    }

    return wf;
}

From source file:com.google.android.gms.samples.vision.face.facetracker.MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override//from ww  w .  j a  va2 s  .  c o  m
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity);

    //        ---Ambil username dari sharedpreference agar user tetap dapat melihat schedule jika keluar dari apps------
    sharedPreferences = getSharedPreferences("PHAROS", MODE_PRIVATE);
    username = sharedPreferences.getString("Username", "");
    //        ---------------------------------------------------------------------------------------------------------

    //        ---Deklarasi fragment fragment yang akan di gunakan---
    Fragment CameraFragment = new CameraFragment();
    Fragment LoginFramgent = new LoginFragment();
    Fragment ScheduleFragment = new ScheduleFragment();
    //        -----------------------------------------------------

    //        Meminta permisssion untuk GPS jika menggunakan android Marshmallow keatas
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
            android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION }, 1);
    }
    //        -------------------------------------------------------------------------

    //       ------- Instantiasi intent filter untuk broadcast recevier------
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

    IntentFilter connectionIntentFilter = new IntentFilter();
    connectionIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    //        ---------------------------------------------------------------------

    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiConfiguration = new WifiConfiguration();

    //        Registrasikan broadcast receiver
    registerReceiver(new wifiEnabled(wifiManager), intentFilter);
    registerReceiver(new wifiConnecting(wifiManager), connectionIntentFilter);
    //        ---------------------------------------

    //       -------- Deklarasi view pager---------
    viewPager = (ViewPager) findViewById(R.id.fragmentFrame);
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), CameraFragment, LoginFramgent,
            ScheduleFragment);
    viewPager.setAdapter(pagerAdapter);
    //        Mengarahkan halaman awal saat membuka aplikasi ke fragment index 1
    viewPager.setCurrentItem(1);
    //        -------------------------------------

}

From source file:foam.starwisp.NetworkManager.java

void Connect() {
    Log.i("starwisp", "Attemping connect to " + SSID);

    List<WifiConfiguration> list = wifi.getConfiguredNetworks();

    Boolean found = false;/*  w ww .  j  a v  a2 s.co m*/

    for (WifiConfiguration i : list) {
        if (i.SSID != null && i.SSID.equals("\"" + SSID + "\"")) {
            found = true;
            Log.i("starwisp", "Connecting (state=connected)");
            state = State.CONNECTED;
            wifi.disconnect();
            wifi.enableNetwork(i.networkId, true);
            wifi.reconnect();
            Log.i("starwisp", "Connected");
            try {
                Thread.sleep(2000);
                Log.i("starwisp", "trying post-connection callback");
                m_Builder.DialogCallback(m_Context, m_Context.m_Name, m_CallbackName, "\"Connected\"");
            } catch (Exception e) {
                Log.i("starwisp", e.toString());
                e.printStackTrace();
            }

            break;
        }
    }

    if (!found) {
        Log.i("starwisp", "adding wifi config");
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + SSID + "\"";

        //conf.wepKeys[0] = "\"" + networkPass + "\"";
        //conf.wepTxKeyIndex = 0;
        //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        //conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        wifi.addNetwork(conf);
    }

}