36

I'm trying to find if scanResult is the currently connected wifi network.

here is my code

public boolean IsCurrentConnectedWifi(ScanResult scanResult) 
{
    WifiManager mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo currentWifi = mainWifi.getConnectionInfo();
    if(currentWifi != null)
    {
        if(currentWifi.getSSID() != null) 
        {
            if(currentWifi.getSSID() == scanResult.SSID)
            return true;
        }
    }  
    return false;
}

I have no problem with getting scanresult.

I'm always getting the currentWifi null.

Where am I doing wrong or is there any alternative method to do this?

3 Answers 3

62

Most probably you have already found answer: currentWifi.getSSID() is quoted in most cases where scanResult.SSID is not (and of course you must not use == on strings :)).

Try something like this, it returns current SSID or null:

public static String getCurrentSsid(Context context) {
  String ssid = null;
  ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if (networkInfo.isConnected()) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
      ssid = connectionInfo.getSSID();
    }
  }
  return ssid;
}

also permissions are required:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

StringUtil is not a standard Android class, so you can use TextUtils instead. The code then looks like this:

public static String getCurrentSsid(Context context) {
  String ssid = null;
  ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if (networkInfo.isConnected()) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
      ssid = connectionInfo.getSSID();
    }
  }
  return ssid;
}
3
  • 4
    as of now connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);is officially deprecated. so sad.
    – guness
    Aug 26, 2015 at 8:45
  • 1
    getNetworkInfo is deprecated, use getActiveNetworkInfo with networkInfo.getType() == ConnectivityManager.TYPE_WIFI
    – zed
    Sep 27, 2017 at 9:26
  • 2
    Please note that with latest Android versions you also need location permissions of some kind to actually get the SSID
    – nsndvd
    Oct 11, 2018 at 8:16
19

My (used to be) non-deprecated, modified approach to the current answer

https://developer.android.com/reference/android/net/ConnectivityManager#getActiveNetworkInfo()

public static String getCurrentSsid(Context context) {
        String ssid = null;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo == null) {
            return null;
        }

        if (networkInfo.isConnected()) {
            final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
            if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
                ssid = connectionInfo.getSSID();
            }
        }

    return ssid;
}
4
  • 1
    Better to use TextUtils.isEmpty() instead of StringUtil.isBlank()
    – Onkar Nene
    Dec 22, 2018 at 7:35
  • Any particular reason why? Jan 2, 2019 at 2:55
  • when i run this code its just return 02:00:00:00:00:00 that it ! :|
    – Ninja
    Apr 2, 2019 at 12:18
  • Well, now it's deprecated. (Deprecated since API 29) Jun 12 at 7:38
6

Update Android Oreo. You will also need to add ACCESS_FINE_LOCATION Permission in Manifest otherwise it will return unknown SSID

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


 fun getCurrentSsid(context: Context): String {
            var ssid = "NA"
            val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            if (networkInfo.isConnected) {
                val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
                val connectionInfo = wifiManager.connectionInfo
                if (connectionInfo != null && !connectionInfo.ssid.isEmpty()) {
                    ssid = connectionInfo.ssid
                }
            }
            return ssid
        }
1
  • 1
    when i run this code its just return 02:00:00:00:00:00 that it ! :|
    – Ninja
    Apr 2, 2019 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.