Connect with particular wifi programmatically after fetching available wifi connections

Sourav Bansal picture Sourav Bansal · Dec 23, 2013 · Viewed 9.8k times · Source

I have got the list of available wifi network list now i want to connect with a particular network.So please guide me what i have to do after fetching the network list in a list view.

**package com.example.wifilist     
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List;
import android.app.Activity; 
import android.content.BroadcastReceiver;
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt; 
import android.net.wifi.WifiManager; 
import android.os.Bundle; 
import android.view.View; import android.view.View.OnClickListener; 
import android.widget.AdapterView; import android.widget.ArrayAdapter;
import android.widget.Button; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 
public class MainActivity extends Activity implements OnClickListener
{
  String networkSSID = "test"; 
  String networkPass = "pass"; 
  WifiManager wifi;
  ListView lv;
  TextView textstatus;
  Button buttonscan,buttonConnect;
  String ITEM_KEY = "key"; 
  ArrayList<String> arralist11=new ArrayList<String>(); 
  ArrayAdapter<String> arrayadapter;
  List<ScanResult> scanresult; 
  int size=0; 
  protected void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonConnect=(Button)findViewById(R.id.btn_connect);
  lv=(ListView)findViewById(R.id.list);
  wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
  if(wifi.isWifiEnabled()==false) 
  {
    Toast.makeText(getApplicationContext(), "Wifi Is Not                          Enabled",Toast.LENGTH_SHORT).show(); }
  else
  {
    Toast.makeText(getApplicationContext(), "Wifi Is   Enabled",Toast.LENGTH_SHORT).show(); 
  } 
    the networbuttonscan=(Button)findViewById(R.id.btn);
    buttonscan.setOnClickListener(this);
    arrayadapter=new ArrayAdapter<String>   (MainActivity.this,R.layout.adapter,R.id.listtext,arralist11);
  lv.setAdapter(arrayadapter); 
  private void ScanningWifiNetwork() 
  {
  registerReceiver(new BroadcastReceiver() 
{
   public void
  onReceive(Context context, Intent intent) 
  { 
    scan result=wifi.getScanResults();
    size=scanresult.size(); 
  }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); 
}
@Override public void onClick(View v)
{ 
  // TODO Auto-generated method
    stub arralist11.clear(); wifi.startScan();
    Toast.makeText(getApplicationContext(),     "Scanning...."+size,Toast.LENGTH_SHORT).show();
try 
{ 
    size=size-1; 
    while(size>=0) 
  {
      HashMap<String, String> item=new HashMap<String, String>();
      item.put(ITEM_KEY, scanresult.get(size).SSID+"\n"             +""+scanresult.get(size).capabilities);
    arralist11.add(scanresult.get(size).SSID+"\n"+scanresult.get(size).capabilities); 
      size--;
      arrayadapter.notifyDataSetChanged(); 
  } 
}
catch(Exception e)
  {
      System.out.println("error in on click view block"+'\n');
      e.printStackTrace(); 
  } 
}

Answer

user3257401 picture user3257401 · Mar 24, 2014

Try This

  WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 // setup a wifi configuration
  WifiConfiguration wc = new WifiConfiguration();
  wc.SSID = "\"YOUR_SSID\"";
  wc.preSharedKey = "\"YOUR_PASSWORD\"";
  wc.status = WifiConfiguration.Status.ENABLED;
  wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
  wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
  wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
  wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
// connect to and enable the connection
 int netId = wifiManager.addNetwork(wc);
 wifiManager.enableNetwork(netId, true);
 wifiManager.setWifiEnabled(true);