Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get current wifi connection info in android

Tags:

android

I'm trying to find if scanResult is the current 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 on getting scanresult.

Always I'm getting currentWifi is null.

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

like image 499
Okan Kocyigit Avatar asked Jan 10 '12 22:01

Okan Kocyigit


People also ask

How can I get connected Wi-Fi information?

Need to know the wifi access Point name in android null) { WifiInfo info = wifiManager. getConnectionInfo(); if (info != null) { String ssid = info. getSSID(); ... } }

How do I see if Wi-Fi is connected on Android?

To check the Wi-Fi statusOn the home screen, tap Apps > Settings. Under Network Connections, tap Wi-Fi; then tap the connected Wi-Fi network. Check the Signal strength.

How do I find the Bssid on my Android phone?

With most Android smartphones you can dial *#*#4636#*#8* to get a menu with among the choices “wifi information”. This will tell you which BSSID you are connected to and at what link speed.


3 Answers

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;
}
like image 194
Maciej Łopaciński Avatar answered Oct 21 '22 17:10

Maciej Łopaciński


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;
}
like image 19
Evan Parsons Avatar answered Oct 21 '22 17:10

Evan Parsons


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
        }
like image 6
Hitesh Sahu Avatar answered Oct 21 '22 17:10

Hitesh Sahu