2013-04-12 79 views
4

我正在嘗試創建自定義WifiSettings。如何連接到Wifi ScanResult項目

我創建活動:

package manual;  
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.WifiManager;  
import android.os.Bundle;  
import android.util.Log; 
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.AdapterView;  
import android.widget.Button;  
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.ImageButton; 
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
import android.widget.Switch; 
import android.widget.TextView;  
import android.widget.Toast; 

public class WiFiDemo extends Activity implements OnClickListener 
{  
    WifiManager wifi;  
    ListView lv; 
    TextView textStatus; 
    Button buttonScan; 
    Switch enable; 
    int size = 0; 
    List<ScanResult> results; 

    String ITEM_KEY = "key"; 
    ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>(); 
    SimpleAdapter adapter; 

    /* Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.wifi); 
     buttonScan = (Button) findViewById(R.id.updateWifi); 
     buttonScan.setOnClickListener(this); 
     enable  = (Switch) findViewById(R.id.switch1); 
     lv = (ListView)findViewById(R.id.listView1); 

     wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     enable.setChecked(wifi.isWifiEnabled()); 
     enable.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundbutton, boolean flag) { 
       wifi.setWifiEnabled(flag); 
       if(flag){ 
        arraylist.clear(); 
        lv.setVisibility(View.VISIBLE); 
        buttonScan.setEnabled(true); 
        wifi.startScan(); 
       }else{ 
        lv.setVisibility(View.INVISIBLE); 
        buttonScan.setEnabled(false); 
       } 
      } 
     }); 
     this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.wifi_item, new String[] { ITEM_KEY }, new int[] { R.id.list_value }); 
     lv.setAdapter(this.adapter); 
     lv.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       int a = lv.getSelectedItemPosition(); 

      } 
     }); 

     registerReceiver(new BroadcastReceiver() 
     { 
      @Override 
      public void onReceive(Context c, Intent intent) 
      { 
       results = wifi.getScanResults(); 
       size = results.size(); 
       for(int i=size-1;i>=0;i--){ 
        HashMap<String, String> item = new HashMap<String, String>();      
        item.put(ITEM_KEY, results.get(i).SSID); 
        arraylist.add(item); 
       } 
       if(size>0) 
        adapter.notifyDataSetChanged(); 
      } 
     }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));      
    } 

    public void onClick(View view) 
    { 
     arraylist.clear();   
     wifi.startScan(); 
     Toast.makeText(this, "Scanning....", Toast.LENGTH_SHORT).show(); 

    }  
} 

當用戶點擊的ListView的項目,它需要連接到無線網絡,以便

lv.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     int a = lv.getSelectedItemPosition(); 
     ScanResult wifiChoosed = results.get(a);    
     //WHAT I NEED TO DO HERE? 
    } 
}); 

我需要ScanResult wifiChoosed = results.get(a);做的連接到wifi?

回答

0
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); 
+1

NetId總是-1 – JackTurky