如何在扫描WiFi时在listView中添加BSSID?

时间:2013-08-08 19:11:25

标签: java android

我有这个代码,它工作正常,但我只得到一个wifi网络名称,而不是BSSID。我需要Item中的网络名称和ListView的subItem中的BSSID。这是我的代码:

public class Wifi_list extends Activity implements OnItemClickListener
 {      
    WifiManager wifi;       
    ListView lv;
    int size = 0;
    List<ScanResult> results;
    private ProgressDialog progressBar;
    private Handler progressBarHandler = new Handler();
    private int progressBarStatus = 0;
    private long fileSize = 0;
    private BroadcastReceiver myReceiver;

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

        requestWindowFeature(Window.FEATURE_NO_TITLE);   //full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.wifi_list);
        lv = (ListView)findViewById(R.id.listView1);

        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        this.adapter = new SimpleAdapter(Wifi_list.this, arraylist, android.R.layout.simple_list_item_1, new String[] { ITEM_KEY }, new int[] { android.R.id.text1 });

        arraylist.clear();          
        wifi.startScan();
        Toast.makeText(this, "Scanning....", Toast.LENGTH_SHORT).show();

        lv.setAdapter(this.adapter);
        lv.setClickable(true);

        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                //scanWifi();
            }
        });

        myReceiver = 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();
            }
        };
        registerReceiver(myReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(myReceiver);
        super.onDestroy();
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

    }

}

1 个答案:

答案 0 :(得分:0)

在这部分

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

您的商品地图中只有一个条目,即results.get(i).SSID的值。 ScanResult也有public String BSSID(接入点的地址)。将其添加到循环内的地图中。

for(int i=size-1;i>=0;i--){
    HashMap<String, String> item = new HashMap<String, String>();                       
    item.put(ITEM_KEY, results.get(i).SSID);
    item.put("BSSID", results.get(i).BSSID);
    arraylist.add(item);
}

您可能还想更改ITEM_KEY以反映键/值对的实际名称(“SSID”)。现在只需“key