Android WiFi无法从ScanResult获取SSID和BSSID

时间:2015-04-20 08:01:10

标签: android wifi android-wifi signal-strength

我得到了这个代码,我想扫描网络,然后将其全部写入listview。但问题是ssid和bssid没有表现出来。其他一切都显示但不是ssid。

另外,每秒更新列表视图的最佳方法是什么,以便您可以看到信号强化实际信号?

import java.util.List;

import android.annotation.SuppressLint;
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.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.wifi.R;

public class MainActivity extends Activity {

    WifiManager wifiManager;
    WifiScanReceiver wifiReciever;
    ListView list;
    String wifis[];
    WifiInfo wifiInfo;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.text);
        wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        wifiReciever = new WifiScanReceiver();
        wifiInfo = wifiManager.getConnectionInfo();
        wifiManager.startScan();
    }



    protected void onPause() {
        unregisterReceiver(wifiReciever);
        super.onPause();
    }

    protected void onResume() {
        registerReceiver(wifiReciever, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }

    class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            wifis = new String[wifiScanList.size()];
            for (int i = 0; i < wifiScanList.size(); i++) {
                wifis[i] = ((wifiScanList.get(i)).toString());
            }

            list.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_list_item_1, wifis));
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用SSIDBSSIDScanResult对象获取wifiScanList.get(i).SSIDwifiScanList.get(i).BSSID。然后将其附加到从toString()返回的其他数据。

请参阅Documentation Here

首先,将您的ArrayAdapter声明为实例变量,并在setAdapter()中致电onCreate()

ArrayAdapter adapter;
ListView list;
ArrayList<String> wifis;
WifiInfo wifiInfo;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list = (ListView) findViewById(R.id.text);
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiReciever = new WifiScanReceiver();
    wifiInfo = wifiManager.getConnectionInfo();

    wifis = new ArrayList<String>(); //initialize wifis
    wifis.add("loading...");
    adapter = new ArrayAdapter<String>(getApplicationContext(),
                        android.R.layout.simple_list_item_1, wifis)
    list.setAdapter(adapter);

    wifiManager.startScan(); //make sure this is the last call
}

修改后的BroadcastReceiver

class WifiScanReceiver extends BroadcastReceiver {
        @SuppressLint("UseValueOf")
        public void onReceive(Context c, Intent intent) {
            List<ScanResult> wifiScanList = wifiManager.getScanResults();
            //wifis = new String[wifiScanList.size()]; //remove this
            wifis.clear(); //add this
            for (int i = 0; i < wifiScanList.size(); i++) {
                String ssid = wifiScanList.get(i).SSID; //Get the SSID
                String bssid =  wifiScanList.get(i).BSSID //Get the BSSID
                //use add here:
                wifis.add( ssid + " " + bssid + " " +((wifiScanList.get(i)).toString()) ); //append to the other data
            }

            adapter.notifyDataSetChanged(); //add this
            wifiManager.startScan(); //start a new scan to update values faster

            //ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),
             //       android.R.layout.simple_list_item_1, wifis)
            //list.setAdapter(adapter);
        }
    }

这仍然只会更新每个扫描结果。我不认为建议每秒更新ListView,您可能需要重新考虑显示RSSI级别的方法。您可以为每个SSID单击一次,并具有一个TextView的详细信息视图,您可以每秒更新当前SSID的RSSI。