我无法对wifi列表进行排序,我正在获取重复的wifi SSID,因此我想获得具有更高强度的唯一SSID列表。您可以看到我的以下代码,但没有获得正确的wifi列表。
List<ScanResult> results = mWifiManager.getScanResults();
int size = results.size();
try {
for (int i = 0; i < results.size() - 1; i++) {
for (int k = i + 1; k < results.size(); k++) {
if (results.get(i).SSID.equals(results.get(k).SSID)) {
if (results.get(k).level > results.get(i).level) {
results.remove(i);
} else {
results.remove(k);
}
}
}
}
} catch (Exception e)
{
e.printStackTrace();
}
答案 0 :(得分:0)
使用ScanResults
合并HashMap
,其中密钥为String
SSID。只需添加检查以前添加的ScanResult
是否存在(在迭代中),然后比较两个level
值
编辑:
List<ScanResult> results = mWifiManager.getScanResults();
HashMap<String, ScanResult> filtered = new HashMap<String, ScanResult>();
for(ScanResult sc : results){
ScanResult prevScanResultOrNull = filtered.get(sc.SSID);
// compare lever with previous ScanResult,
// insert bigger value under SSID key, they are same
if(prevScanResultOrNull!=null)
filtered.put(prevScanResultOrNull.SSID, // or sc.SSID, whatever
prevScanResultOrNull.level > sc.level ?
prevScanResultOrNull : sc);
// no SSID in HashMap, so just put as highest value
else
filtered.put(sc.SSID, sc);
}
EDIT2:
如果您仍想使用List
界面,那么:
results = new ArrayList<ScanResult>(filtered.values());