开发者

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

开发者 https://www.devze.com 2023-01-26 05:19 出处:网络
Does Android provide a notification of being in vicinity of a new Wifi Network? Whether the device is configured to connect to that wifi net开发者_如何学编程work depends on whether the device has the

Does Android provide a notification of being in vicinity of a new Wifi Network? Whether the device is configured to connect to that wifi net开发者_如何学编程work 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?


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消