问题
I would like some clarifications on the behavior of WifiManager.getScanResults()
, namely :
When wireless is enabled
- Does android scan for access points on a fixed interval ? Can one query/change the interval ? Can one query the time of the last scan ? For a discussion see this answer
- What happens when the wireless radio is turned off (sleeps) - while wifi is still enabled - will
getScanResults()
go on returning the last scan results ? How would one know if it's time forstartScan()
?
When wireless is disabled
getScanResults()
would return the last scan results ? Or an empty List ? Ornull
(it does return null at times and this is not handled by 90% of the snippets posted in SO) ?- The answer to 1 would be true even after a reboot (the phone booting with wifi off) ?
The reason I ask is I need to get the list of the available access points periodically and I'd rather call getScanResults()
at once, than WifiManager.startScan()
and then getScanResults()
if I really do not have to - as this involves acquiring wifi locks and the like and is rather subtle. I am not also sure how much do the API level and phone hardware come into play.
For a good discussion of difficulties see this answer
回答1:
When wireless is disabled
getScanResults()
will returnnull
...- ...even after a reboot (that's to be expected - this would be disputed if it did not return null in the first place)
When wireless is enabled
2.Surprisingly enough getScanResults()
will return null
after some time (on a Nexus One, Android 2.3.7, Cyanogen mod - but I suspect this is not really relevant). So yes, apparently one has to initiate a scan irrespective of the wireless status
I guess this is true also if the state is WIFI_STATE_DISABLING
I am still interested in the answer in the case wireless is enabled - in particular question 2, if the device is asleep. Google groups won't let me post (hint)
回答2:
public List getScanResults ()
Added in API LEVEL 1
Return the results of the latest access point scan.
Returns
the list of access points found in the most recent scan. An app must hold ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
permission in order to get valid results.
来源:https://stackoverflow.com/questions/16137268/wifimanager-getscanresults-clarifications-automatic-scans-sleep-etc