Android WifiManager:如何存储多次扫描的wifi扫描结果

时间:2015-06-04 14:21:54

标签: android arraylist wifimanager

我需要使用WifiManager进行多次扫描。我有以下代码只扫描一次并将结果存储在access_points中,任何人都可以建议一个有效的代码来存储多次扫描的结果吗?

 wifi = (WifiManager) this.getSystemService(WIFI_SERVICE);
 List<ScanResult> access_points = wifi.getScanResults();

1 个答案:

答案 0 :(得分:2)

这是获得结果的错误方法。如果您只是$xml = simplexml_load_string($xmlstring); $json = json_encode($xml); ,它会将旧结果返回给您。

要获得wifi扫描结果,您只需像第一行中那样获得WifiManager的实例然后

List<ScanResult> access_points = wifi.getScanResults();

要多次获得结果或进行多次扫描,您需要多次调用WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.getScanResults(); // The above is an async call and will results are available System will broadcast SCAN_RESULTS_AVAILABLE intent and you need to set a `BroadCastReceiver` for it. // And when you catch the intent, get the results using List<ScanResult> results = wifiManager.getScanResults(); ,并在捕获意图wifiManager.startScan()时使用List<ScanResult> results = wifiManager.getScanResults();获取新结果。您可以使用计时器或使用处理程序的post方法。