简体   繁体   English

知道wifi何时连接到特定的SSID

[英]Know when wifi is connected to a particular SSID

I am developing an app in which I need to start a service when the WiFi is connected to a predefined SSID. 我正在开发一个应用程序,当WiFi连接到预定义的SSID时,我需要在其中启动服务。 Can anyone help me with this? 谁能帮我这个? Is there a broadcast in android that is fired when WiFi is connected (not turned on) ? 连接WiFi(未打开)时,Android中是否存在会触发的广播?

Yes, its called ConnectivityManager : 是的,它叫做ConnectivityManager

ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo netInfo = conMan.getActiveNetworkInfo();

You can use getType() to check whether it is a WiFi connection: 您可以使用getType()来检查它是否为WiFi连接:

netInfo.getType() == ConnectivityManager.TYPE_WIFI

See this post for more information. 有关更多信息,请参见此帖子

EDIT 编辑

To obtain the SSID try: 要获取SSID,请尝试:

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
connectionInfo.getSSID();

as seen here 这里所见

Yes, there's a Broadcast for this: NETWORK_STATE_CHANGED_ACTION . 是的,为此有一个BroadcastNETWORK_STATE_CHANGED_ACTION When the new state is CONNECTED, the Intent includes the BSSID as an extra (cf. the documentation linked above). 当新状态为CONNECTED时, Intent包括BSSID作为额外的内容(请参见上面链接的文档)。 You can check the state via the included NetworkInfo object. 您可以通过随附的NetworkInfo对象检查状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM