2

我正在尝试通过我的 Android 设备扫描可用的 wifi 网络。这是代码块 -

WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);

                registerReceiver(new BroadcastReceiver() {
                    @Override 
                    public void onReceive(Context context, Intent intent) {
                        WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
                        List<ScanResult> results = wifiManager.getScanResults(); 

                        //int newRSSI= intent.getIntExtra(wifiManager.EXTRA_NEW_RSSI, 0);
                        //WifiDistance wifi_dis = new WifiDistance();

                        for (ScanResult result : results) {

                            textView.append("\nSSID="+result.SSID + ", " + "Strength(dBm)=" + result.level + ", AP: "+ result.BSSID);
                            wifi+="\n"+result.SSID + ", " + result.level + ", "+ result.BSSID;


                            //WifiDistance.getWifiDistance().distanceCalc(result.BSSID, result.level);
                    }, 
                    new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

                    // Initiate a scan.
                wifiManager.startScan();

                }

我把它放在一个动作处理程序下,例如如果按下一个按钮,它就会开始扫描。现在我的问题是,当我的手机连接到特定网络时,它不会自动更新 wifi 扫描结果。它只保留较旧的数据。我想按下按钮开始扫描并每秒更新一次扫描结果。

我在 HTC Magic 中使用 OS 2.1。

任何人都可以帮助我吗?

4

1 回答 1

2

您究竟如何尝试每秒更新扫描结果?

在我看来,您只在收到广播时才更新列表,并且 startscan 代码出现在列表填充之后。

相反,您可以使用计时器任务来更新运行一个单独的方法,该方法每秒更新 Scanresult 列表,然后再次运行 For-Loop。

就像是:

Public void RunEverySecond(){
List<ScanResult> results = wifiManager.getScanResults();

for (ScanResult result : results) {

                        textView.append("\nSSID="+result.SSID + ", " + "Strength(dBm)=" + result.level + ", AP: "+ result.BSSID);
                        wifi+="\n"+result.SSID + ", " + result.level + ", "+ result.BSSID;

                }
}

也许?

于 2011-06-14T07:02:46.267 回答