connect to Wifi - Android android.net.wifi

Android examples for android.net.wifi:Wifi Connection

Description

connect to Wifi

Demo Code

import java.util.List;

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

public class Main {

  public static boolean connectWifi(WifiConfiguration mWifiConfiguration, WifiManager mWifiManager) {
    WifiConfiguration tempConfig = IsExists(mWifiConfiguration.SSID, mWifiManager);

    if (tempConfig != null) {
      mWifiManager.removeNetwork(tempConfig.networkId);
    }//from ww w .j  a v a 2 s . c  om
    int netId = mWifiManager.addNetwork(mWifiConfiguration);
    boolean b = mWifiManager.enableNetwork(netId, true);

    return b;
  }

  public static WifiConfiguration IsExists(String SSID, WifiManager wifiManager) {
    List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
    for (WifiConfiguration existingConfig : existingConfigs) {
      String ssid = existingConfig.SSID;
      if (existingConfig.SSID.equals(SSID)) {
        return existingConfig;
      }
    }
    return null;
  }

}

Related Tutorials