其他分享
首页 > 其他分享> > android – 如何连接到Wifi ScanResult项目

android – 如何连接到Wifi ScanResult项目

作者:互联网

我正在尝试创建自定义WifiSettings.

我创建了一个活动:

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项时,它需要连接到那个wifi

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?

解决方法:

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

标签:android,wifimanager
来源: https://codeday.me/bug/20190709/1410332.html