在获取可用的wifi连接后,以编程方式连接特定的wifi

时间:2013-12-23 05:52:30

标签: android android-wifi wifimanager

我已经获得了可用的wifi网络列表列表,现在我想与特定网络连接。所以请在列表视图中获取网络列表后指导我要做的事情。 < / p>

**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(); 
  } 
}

1 个答案:

答案 0 :(得分:12)

试试这个

  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);