Android - Get Notified when a new access point is detected?

一个人想着一个人 提交于 2019-12-22 10:51:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!