3

I read this solution : How to check network connection enable or disable in WIFI and 3G(data plan) in mobile? .BUT, it just check whether wifi network is connected or not. How to check whether the wifi is connected to the specific SSID or not?

Community
  • 1
  • 1
conanlive
  • 243
  • 2
  • 5
  • 11

3 Answers3

9

The code is similar, again use a system service, make sure you are connected to something and get the SSID:

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
    String ssid = wifiInfo.getSSID();
}
satur9nine
  • 13,927
  • 5
  • 80
  • 123
  • 1
    If my wireless network SSID is Linksys and I go to my friend's house who also has his SSID set to Linksys, and I can connect to either one (I have the password for both), this test will be true for both places, correct? Is there a way to be sure it is MY Linksys network? Using MAC Address, perhaps? – MrGibbage Jun 20 '13 at 17:11
  • 1
    for some reason, it doesn't work. I have to use `ConnectivityManager` and `NetworkInfo`. what could be the cause? – chip Aug 04 '14 at 03:55
1
public static boolean IsWiFiConnected(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
        .getApplicationContext().getSystemService(
            Context.CONNECTIVITY_SERVICE);

    if (connectivity != null) {
      NetworkInfo[] info = connectivity.getAllNetworkInfo();
      if (info != null) {
        for (int i = 0; i < info.length; i++) {
          if (info[i].getTypeName().equals("WIFI")
              && info[i].isConnected())
            return true;
        }
      }
    }

    return false;
  }
Ashok Domadiya
  • 1,124
  • 13
  • 31
1

The best that worked for me:

Menifest:

<receiver android:name="com.AEDesign.communication.WifiReceiver" >
        <intent-filter android:priority="100">
            <action android:name="android.net.wifi.STATE_CHANGE" />
        </intent-filter>
    </receiver>

Class:

 public class WifiReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {

            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if(info!=null){

                if(info.isConnected()){
                                    //Do your work. 

                                    //To check the Network Name or other info:
                        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                        String ssid = wifiInfo.getSSID();

                }
            }

Permissions:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
  • Excuse me. Do this `android.net.wifi.STATE_CHANGE` fire when the device is switching from one SSID to another SSID? – Yeung May 12 '14 at 09:52
  • This should fire. Cz state changes to "disconnected" and then back to "connected". I haven't checked it though – M. Usman Khan May 12 '14 at 10:00