问题
Does Android provide a notification of being in vicinity of a new Wifi Network? Whether the device is configured to connect to that wifi network depends on whether the device has the wifi configuration set for that particular wifi network, but is it possible to get notification whenever entering any new wifi network?
I saw the WifiManager class but the states inside the class do not seem to achieve what I am trying to do. Any ideas?
回答1:
Use a BroadcastReceiver
registered to receive intents with action: WifiManager.NETWORK_STATE_CHANGED_ACTION
.
In this BroadcastReceiver, you can extract a NetworkInfo object from the intent:
NetworkInfo ni = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
Then process ni.getState()
to check connections/disconnections from wifi networks.
Is this what you were looking for?
Edit after answer
So if you want to know which wifi networks are available, use WifiManager.getScanResults() This gives you the list of nearby access points in Scanresult
objects. Those contain the SSID and BSSID of the access points, which are respectively their network name and mac address.
You can get this information asynchronously by using a BroadcastReceiver
registered to receive intents with action WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
. Then you will be notified each time the system performs a wifi scan, and you can check if a new SSID (i.e. network name) has appeared since the last scan.
And finally if you wish to scan more often than the system does by default, you can trigger wifi scans yourself using WifiManager.startScan()
.
来源:https://stackoverflow.com/questions/4253249/android-get-notified-when-a-new-access-point-is-detected