0

I am developing an app in which I need to start a service when the WiFi is connected to a predefined SSID. Can anyone help me with this? Is there a broadcast in android that is fired when WiFi is connected (not turned on) ?

Simar
  • 600
  • 4
  • 17

2 Answers2

0

Yes, its called 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:

netInfo.getType() == ConnectivityManager.TYPE_WIFI

See this post for more information.

EDIT

To obtain the SSID try:

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

as seen here

Community
  • 1
  • 1
Marcel Herd
  • 392
  • 1
  • 8
0

Yes, there's a Broadcast for this: NETWORK_STATE_CHANGED_ACTION. When the new state is CONNECTED, the Intent includes the BSSID as an extra (cf. the documentation linked above). You can check the state via the included NetworkInfo object.

cygery
  • 2,309
  • 3
  • 18
  • 25