connect To WIFI Network by ssid - Android Wifi

Android examples for Wifi:Wifi SSID

Description

connect To WIFI Network by ssid

Demo Code


//package com.java2s;
import android.content.Context;

import android.net.wifi.WifiConfiguration;

import android.net.wifi.WifiManager;
import java.util.List;

public class Main {
    /**/*from   w ww .ja v a 2  s. c o m*/
     *
     * @param c
     * @param ssid
     * @return Connect to a network if preferences are saved.
     */
    public static boolean connectToNetwork(Context c, String ssid) {
        WifiManager wifiManager = getWifiManger(c);
        List<WifiConfiguration> confs = getConfiguredNetworks(wifiManager);
        for (WifiConfiguration conf : confs) {
            if (conf.SSID.equals(quote(ssid))) {
                wifiManager.disconnect();
                wifiManager.enableNetwork(conf.networkId, true);
                wifiManager.reconnect();
                return true;
            }
        }
        return false;
    }

    private static WifiManager getWifiManger(Context c) {
        return (WifiManager) c.getSystemService(c.WIFI_SERVICE);
    }

    private static List<WifiConfiguration> getConfiguredNetworks(
            WifiManager wifiManager) {
        return wifiManager.getConfiguredNetworks();
    }

    private static String quote(String string) {
        return String.format("\"%s\"", string);
    }
}

Related Tutorials