WifiManager.getScanResults() - clarifications (aut

2019-01-27 07:47发布

问题:

I would like some clarifications on the behavior of WifiManager.getScanResults(), namely :

When wireless is enabled

  1. 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
  2. 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 for startScan()?

When wireless is disabled

  1. getScanResults() would return the last scan results ? Or an empty List ? Or null (it does return null at times and this is not handled by 90% of the snippets posted in SO) ?
  2. 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

  1. getScanResults() will return null...
  2. ...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.