connect To Network - Android android.net.wifi

Android examples for android.net.wifi:Wifi Connection

Description

connect To Network

Demo Code

import java.util.List;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class Main {

  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();//from   www .j a va  2s.  c om
        wifiManager.enableNetwork(conf.networkId, true);
        wifiManager.reconnect();
        return true;
      }
    }
    return false;
  }

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

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

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

}

Related Tutorials