问题描述
|
我正在尝试通过我的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。
谁能帮我吗?
解决方法
究竟您如何尝试每秒更新一次扫描结果?
在我看来,因为您仅在接收到广播时才更新列表,并且startscan代码出现在列表填充之后。
相反,您可以使用计时器任务来更新运行单独的方法,该方法每秒更新一次Scanresult列表,然后再次运行循环。
就像是:
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;
}
}
也许?