85

I'm trying to get the SSID of the WIFI network when my android device is connected to WIFI.

I've registered a BroadcastReceiver listening for android.net.wifi.supplicant.CONNECTION_CHANGE . I get the notification when WIFI is disconnected or reconnected. Unfortunately, I can't get the network's SSID.

I'm using the following code to find the SSID:

WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();

Instead of the SSID, I get the string <unknown ssid> back.

These are the permissions in the manifest (I've added ACCESS_NETWORK_STATE just to check, I don't actually need it)

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

Why does this happen? How can I get the actual SSID? Is the broadcast fired to early, before the connection is established? Is there another broadcast I should listen to? I'm only interested in WIFI connections, not 3G connections.

Update: I just checked, wifiInfo.getBSSID() returns null.

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • Interesting, the docs state you should receive `null` from `getSSID()` if the connection is not available, so I don't think it's a connection problem. Just wondering, what is the SSID of the network you're testing on? – Ben Siver Jan 27 '14 at 20:54
  • What is the detailed state and suplicant state? – Eric Woodruff Jan 27 '14 at 21:12
  • 1
    @Ben S, check the link I supplied in the question, is Android 4.2's behavior, and possibly newer versions as well. It's an Android bug. – zmbq Jan 27 '14 at 21:19
  • Try this SO answer. https://stackoverflow.com/questions/49977395/on-oreo-8-1-0-not-getting-the-correct-wifi-ssid-its-showing-unknown-ssid-t/51518480#51518480 – Yaswant Narayan Jul 25 '18 at 12:10

10 Answers10

115

I listen for WifiManager.NETWORK_STATE_CHANGED_ACTION in a broadcast receiver

if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
    NetworkInfo netInfo = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO);
    if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {
        ...
    }
}

I check for netInfo.isConnected(). Then I am able to use

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid  = info.getSSID();

UPDATE

From android 8.0 onwards we wont be getting SSID of the connected network unless location services are enabled and your app has the permission to access it.

Louis CAD
  • 10,965
  • 2
  • 39
  • 58
Eric Woodruff
  • 6,380
  • 3
  • 36
  • 33
  • @Eric : would you please share full code with broadcast reciever as well. – Rakesh Gondaliya Jun 02 '15 at 07:44
  • I did share it in the first code block. "action" comes from the broadcast receiver's received intent. – Eric Woodruff Jun 02 '15 at 14:05
  • 12
    You should change `WifiManager wifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);` to `WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);` Because WiFiManager must use Application Context (not Activity Context), otherwise a memory leak can occur. – Pavlos Panteliadis Jun 12 '17 at 00:42
  • Are you sure this works on Android 8 even with GPS turned on? NETWORK_STATE_CHANGED is not in the list of [implicit broadcast exceptions](https://developer.android.com/guide/components/broadcast-exceptions), so I don't think the system will call your BroadcastReceiver any longer. – Michael Krause Sep 13 '18 at 03:51
  • 3
    That UPDATE is important! Even with all the right permissions, you will still get an unknown SSID until GPS is actually turned on! (Tested only on Android 9) – Kedar Paranjape Dec 14 '18 at 15:28
  • Giving COARSE location permission is enough. There is no need to turn on GPS. – Kostadin Nov 28 '19 at 11:57
  • Android 8.0 solution see https://stackoverflow.com/a/21391836/2447726 – Guy West May 04 '20 at 09:20
  • I wonder if it's the GPS that needs to be turned on, or if it's enough to have Location Services enabled (and corresponding permissions granted, of course). – dbm May 28 '21 at 13:47
60

Starting with Android 8.1 (API 27), apps must be granted the ACCESS_COARSE_LOCATION (or ACCESS_FINE_LOCATION) permission in order to obtain results from WifiInfo.getSSID() or WifiInfo.getBSSID(). Apps that target API 29 or higher (Android 10) must be granted ACCESS_FINE_LOCATION.

This permission is also needed to obtain results from WifiManager.getConnectionInfo() and WifiManager.getScanResults() although it is not clear if this is new in 8.1 or was required previously.

Source: "BSSID/SSID can be used to deduce location, so require the same location permissions for access to these WifiInfo fields requested using WifiManager.getConnectionInfo() as for WifiManager.getScanResults()."

mike47
  • 2,217
  • 1
  • 27
  • 50
44

If you don't want to make Broadcast Receiver then simple try

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo;

wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
    ssid = wifiInfo.getSSID();
}

Remember every time user disconnect or connect to new SSID or any wifi state change then you need to initialize WifiInfo i.e wifiInfo = wifiManager.getConnectionInfo();

Ziem
  • 6,579
  • 8
  • 53
  • 86
Pranav
  • 4,172
  • 3
  • 30
  • 31
  • 2
    This looks oh so much better than the accepted answer. Why listen to net state changes when there might actually be none at the specific run-time of our necessity to check this information (SSID of connected network)? I'm assuming though, there might be the possibility of WifiInfo being null because of the documentation on getConnectionInfo, so guard checks are due IMHO. – leRobot Apr 07 '16 at 13:41
  • 1
    It should be WifiManager not WiFiManager – behelit May 16 '16 at 23:07
  • 3
    Works. Just need to set permissions : INTERNET, ACCESS_WIFI_STATE , and location permission: ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION – android developer Oct 26 '18 at 13:59
16

I found interesting solution to get SSID of currently connected Wifi AP. You simply need to use iterate WifiManager.getConfiguredNetworks() and find configuration with specific WifiInfo.getNetworkId()

My example

in Broadcast receiver with action WifiManager.NETWORK_STATE_CHANGED_ACTION I'm getting current connection state from intent

NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
nwInfo.getState()

If NetworkInfo.getState is equal to NetworkInfo.State.CONNECTED then i can get current WifiInfo object

WifiManager wifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo ();

And after that

public String findSSIDForWifiInfo(WifiManager manager, WifiInfo wifiInfo) {

    List<WifiConfiguration> listOfConfigurations = manager.getConfiguredNetworks();

    for (int index = 0; index < listOfConfigurations.size(); index++) {
        WifiConfiguration configuration = listOfConfigurations.get(index);
        if (configuration.networkId == wifiInfo.getNetworkId()) {
            return configuration.SSID;
        }
    }

    return null;
}

And very important thing this method doesn't require Location nor Location Permisions

In API29 Google redesigned Wifi API so this solution is outdated for Android 10.

TomMannson
  • 249
  • 3
  • 7
  • works great; I skipped all your things/description before receiving wifiInfo (still had this + the manager before). wondering whether this will change with android 9 – Kaspatoo Mar 14 '19 at 11:40
  • 2
    You're welcome. I tested this solution in "Pie" and it still working – TomMannson Mar 14 '19 at 12:42
  • this is not working in android Q without location permission, getConfiguredNetworks is returning empty list and getNetworkId is returning -1. It is working with location permission. – Nithin Jul 26 '19 at 15:23
  • 1
    WifiConfiguration is deprecated in API 29 = Android 10 – Werner Schäffer Dec 18 '19 at 18:13
  • You're right, but for backward compatibility purposes it can be useful. Thanks for your feadback – TomMannson Dec 19 '19 at 10:36
5

In Android 8.1 it is must to turned Location on to get SSID, if not you can get connection state but not SSID

WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = null;
if (wifiManager != null) 
wifiInfo = wifiManager.getConnectionInfo();
 String ssid = null;
if (wifiInfo != null) 
ssid = wifiInfo.getSSID(); /*you will get SSID <unknown ssid> if location turned off*/
user3143487
  • 235
  • 3
  • 7
5

This is a follow up to the answer given by @EricWoodruff.

You could use netInfo's getExtraInfo() to get wifi SSID.

if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals (action)) {
    NetworkInfo netInfo = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO);
    if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {
        String ssid = info.getExtraInfo()
        Log.d(TAG, "WiFi SSID: " + ssid)
    }
}

If you are not using BroadcastReceiver check this answer to get SSID using Context

This is tested on Android Oreo 8.1.0

Yaswant Narayan
  • 1,297
  • 1
  • 15
  • 20
3

Answer In Kotlin Give Permissions

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



 private fun getCurrentNetworkDetail() {
    val connManager =
        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
    if (networkInfo.isConnected) {
        val wifiManager =
            context.getApplicationContext().getSystemService(Context.WIFI_SERVICE) as WifiManager
        val connectionInfo = wifiManager.connectionInfo
        if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.ssid)) {
            Log.e("ssid", connectionInfo.ssid)
        }
    }
    else{
        Log.e("ssid", "No Connection")
    }

}
RanaUmer
  • 561
  • 5
  • 9
3

For me it only worked when I set the permission on the phone itself (settings -> app permissions -> location always on).

0

According to the explanation of the official document, on Android 10+(API 29 and higher), declaring ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions is not enough.

1. For foreground location:

You also need to declare a foregroundServiceType of location in your Service to use foreground location.

<service
    android:name="MyNavigationService"
    android:foregroundServiceType="location" ... >
    <!-- Any inner elements would go here. -->
</service>

Or if your code running in the Activity, the activity should be visible to the user.

2. For background location:

You should declare the ACCESS_BACKGROUND_LOCATION permission in your app's manifest in order to request background location access at runtime.

Note: The Google Play Store has a location policy concerning device location, restricting background location access to apps that need it for their core functionality and meet related policy requirements.

Official document:

https://developer.android.com/training/location/permissions https://developer.android.com/guide/topics/connectivity/wifi-scan#wifi-scan-permissions

codezjx
  • 9,012
  • 5
  • 47
  • 57
-3

Android 9 SSID showing NULL values use this code..

ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo.isConnected()) {
            WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            wifiInfo.getSSID();
            String name = networkInfo.getExtraInfo();
            String ssid = wifiInfo.getSSID();
            return ssid.replaceAll("^\"|\"$", "");
        }
tung
  • 719
  • 2
  • 14
  • 30