本文介绍了如何连接到一个无线ScanResult项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个自定义WifiSettings。

我创建活动:

 包手册;
进口的java.util.ArrayList;
进口的java.util.HashMap;
进口的java.util.List;
进口android.app.Activity;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.content.IntentFilter;
进口android.net.wifi.ScanResult;
进口android.net.wifi.WifiConfiguration;
进口android.net.wifi.WifiManager;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.AdapterView;
进口android.widget.Button;
进口android.widget.CompoundButton;
进口android.widget.CompoundButton.OnCheckedChangeListener;
进口android.widget.ImageButton;
进口android.widget.ListView;
进口android.widget.SimpleAdapter;
进口android.widget.Switch;
进口android.widget.TextView;
进口android.widget.Toast;公共类WiFiDemo扩展活动实现OnClickListener
 {
    WifiManager无线网络;
    LV的ListView;
    TextView的textStatus;
    按钮buttonScan;
    切换使能;
    INT大小= 0;
    清单< ScanResult>结果;    字符串ITEM_KEY =钥匙;
    ArrayList的<&HashMap的LT;字符串,字符串>> ArrayList的=新的ArrayList<&HashMap的LT;字符串,字符串>>();
    SimpleAdapter适配器;    / *当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.wifi);
        buttonScan =(按钮)findViewById(R.id.updateWifi);
        buttonScan.setOnClickListener(本);
        使=(开关)findViewById(R.id.switch1);
        LV =(ListView控件)findViewById(R.id.listView1);        无线=(WifiManager)getSystemService(Context.WIFI_SERVICE);
        enable.setChecked(wifi.isWifiEnabled());
        enable.setOnCheckedChangeListener(新OnCheckedChangeListener(){
            @覆盖
            公共无效onCheckedChanged(CompoundButton compoundbutton,布尔标志){
                wifi.setWifiEnabled(标志);
                如果(标志){
                    arraylist.clear();
                    lv.setVisibility(View.VISIBLE);
                    buttonScan.setEnabled(真);
                    wifi.startScan();
                }其他{
                    lv.setVisibility(View.INVISIBLE);
                    buttonScan.setEnabled(假);
                }
            }
        });
        this.adapter =新SimpleAdapter(WiFiDemo.this,ArrayList中,R.layout.wifi_item,新的String [] {} ITEM_KEY,新的INT [] {R.id.list_value});
        lv.setAdapter(this.adapter);
        lv.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(查看视图){
                INT A = lv.getSelectedItemPosition();            }
        });        registerReceiver(新广播接收器()
        {
            @覆盖
            公共无效的onReceive(上下文C,意图意图)
            {
               结果= wifi.getScanResults();
               大小= results.size();
               的for(int i =大小-1; I> = 0; I - ){
                   HashMap的<字符串,字符串>项目=新的HashMap<字符串,字符串>();
                   item.put(ITEM_KEY,results.get(ⅰ).SSID);
                   arraylist.add(项目);
               }
               如果(大小大于0)
                   adapter.notifyDataSetChanged();
            }
        },新的IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }    公共无效的onClick(查看视图)
    {
        arraylist.clear();
        wifi.startScan();
        Toast.makeText(这一点,扫描......,Toast.LENGTH_SHORT).show();    }
}

当用户点击的ListView的一个项目,它需要连接到无线网络,以便

  lv.setOnClickListener(新View.OnClickListener(){
    @覆盖
    公共无效的onClick(查看视图){
        INT A = lv.getSelectedItemPosition();
        ScanResult wifiChoosed = results.get(一);
        //我需要在这里做?
    }
});

我需要 ScanResult wifiChoosed = results.get做什么(一); 来连接到WLAN


解决方案

  WifiManager wifiManager =(WifiManager)getSystemService(Context.WIFI_SERVICE);
    //设置一个无线网络配置
    WifiConfiguration WC =新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);
    //连接到并启用连接
    INT NETID = wifiManager.addNetwork(WC);
    wifiManager.enableNetwork(NETID,真);
    wifiManager.setWifiEnabled(真);

i'm trying to create a custom WifiSettings.

I create an activity:

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

    }    
}

When an user clicks on an item of ListView, it needs to connect to that wifi so

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

What i need to do with ScanResult wifiChoosed = results.get(a); to connect to 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);

这篇关于如何连接到一个无线ScanResult项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 17:17