2012-08-10 21 views
1

我想显示可用的无线设备列表。这是我的代码,我不明白这里有什么错误?可用的无线设备列表

wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
if (wifi.isWifiEnabled() == false) 
    { 
Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled",Toast.LENGTH_LONG).show(); 
wifi.setWifiEnabled(true); 
    } 
String[] str1 = null; 
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,str1); 
lv.setAdapter(adapter); 
WifiInfo info = wifi.getConnectionInfo(); 
textStatus.append("\n\nWiFi Status: " + info.toString()); 
boolean b=wifi.isWifiEnabled(); 
if(b){ 
wifi.setWifiEnabled(false); 
Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_SHORT).show(); 
enter code here 

这是我的代码,我想获得无线上网的编程功能的设备性能特别是在Android系统。我怎么能得到这个。任何人都可以帮我在这里吗?

public void onReceive(Context c, Intent intent) 
     { 
results = wifi.getScanResults(); 
size = results.size(); 
int i = 0; 
str1 = new String[size]; 
for (ScanResult result : results){ 
str1[i] = result.SSID + " " + result.level; 
i++; 
    } 

回答

5

您可以通过

List<ScanResult> mScanResults = mWifiManager.getScanResults(); 

然后再遍历mScanResults和使用results.SSID获取SSID获取可用的WIFI扫描结果。此外,如果您有兴趣获得最佳网络,则可以使用WifiManager.compareSignalLevel(int rssiA, int rssiB)来比较两个网络。

ScanResult bestResult = null; 
for(ScanResult results : mScanResults){ 
    Log.d("result",results.SSID); 
    if(bestResult == null || WifiManager.compareSignalLevel(bestResult.level, 
                 results.level) < 0){ 
     bestResult = results; 
    } 
} 
String message = String.format("%s networks found. %s is the strongest.", 
              mScanResults.size(), bestResult.SSID); 
Log.d("best network",message); 

你可以从我的repository下载完整的演示。

+0

我想如何把它放在字符串数组中然后使用listview设置适配器? – 2012-08-10 11:05:15

+0

这里,我应该如何使用列表中的SSID,但我已经写了这样的代码,为(ScanResult result:results){str1 [i] = result.SSID +“”+ result.level; i ++; } – 2012-08-17 05:47:16

1

试试看看这个代码。

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.ListView;  
import android.widget.SimpleAdapter;  
import android.widget.TextView;  
import android.widget.Toast; 

public class WiFiDemo extends Activity implements OnClickListener 
{  
    WifiManager wifi;  
    ListView lv; 
    TextView textStatus; 
    Button buttonScan; 
    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.main); 

     textStatus = (TextView) findViewById(R.id.textStatus); 
     buttonScan = (Button) findViewById(R.id.buttonScan); 
     buttonScan.setOnClickListener(this); 
     lv = (ListView)findViewById(R.id.list); 

     wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
     if (wifi.isWifiEnabled() == false) 
     { 
      Toast.makeText(getApplicationContext(), "wifi is disabled..making it enabled", Toast.LENGTH_LONG).show(); 
      wifi.setWifiEnabled(true); 
     } 
     this.adapter = new SimpleAdapter(WiFiDemo.this, arraylist, R.layout.row, new String[] { ITEM_KEY }, new int[] { R.id.list_value }); 
     lv.setAdapter(this.adapter); 

     registerReceiver(new BroadcastReceiver() 
     { 
      @Override 
      public void onReceive(Context c, Intent intent) 
      { 
       results = wifi.getScanResults(); 
       size = results.size(); 
      } 
     }, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));      
    } 

    public void onClick(View view) 
    { 
     arraylist.clear();   
     wifi.startScan(); 

     Toast.makeText(this, "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, results.get(size).SSID + " " + results.get(size).capabilities); 

       arraylist.add(item); 
       size--; 
       adapter.notifyDataSetChanged();     
      } 
     } 
     catch (Exception e) 
     { }   
    }  
} 
+0

如果不使用Toast,我需要使用Array Adapter在Listview中显示Wifi设备的所有列表。我该怎么做?我对这个Android很新,我是初学者,请有谁能帮我一下吗?抱歉,如果我错了! – 2012-08-13 04:46:01

+0

在这个例子中,我列出了listview中所有可用的wifi。请在main.xml文件中使用一个textview,button和listview,并按照示例给它一个id。 – 2012-08-13 04:47:32

+0

好的,谢谢你的帮助! – 2012-08-13 05:12:52