11

How to get WIFI SSID in Android 9.0(PIE)? My code is working fine till android version 8.1.0 but it is not working on Android 9.0

ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo.isConnected()) {
            WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            wifiInfo.getSSID();
            String name = networkInfo.getExtraInfo();
            String ssid = "\"" + wifiInfo.getSSID() + "\"";
}

But this code is not working on Android 9.0

Manohar
  • 22,116
  • 9
  • 108
  • 144
Abhinav Tiwari
  • 131
  • 1
  • 1
  • 5

3 Answers3

13

Android version 9 (Pie) changes require Location to be enabled (by the user), or the call to getConnectioInfo() will fail, even if your app has correct permissions. This is documented in the Android 9 changes (excerpt below):


Restricted access to Wi-Fi location and connection information

In Android 9, the permission requirements for an app to perform Wi-Fi scans are more strict than in previous versions. For details, see Wi-Fi scanning restrictions.

Similar restrictions also apply to the getConnectionInfo() method, which returns a WifiInfo object describing the current Wi-Fi connection. You can only use this object's methods to retrieve SSID and BSSID values if the calling app has the following permissions:

  • ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
  • ACCESS_WIFI_STATE

Retrieving the SSID or BSSID also requires location services to be enabled on the device (under Settings > Location).


In my case, I call the function below (only if running on Android 9+) to detect if location is enabled, prior to attempting to read the WIFI SSID.

public static boolean isLocnEnabled(Context context) {
    List locnProviders = null;
    try {
        LocationManager lm =(LocationManager) context.getApplicationContext().getSystemService(Activity.LOCATION_SERVICE);
        locnProviders = lm.getProviders(true);

        return (locnProviders.size() != 0);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (BuildConfig.DEBUG) {
            if ((locnProviders == null) || (locnProviders.isEmpty()))
                Log.d(TAG, "Location services disabled");
            else
                Log.d(TAG, "locnProviders: " + locnProviders.toString());
        }
    }
    return(false);
}

If location is not enabled, I pop up a dialog that says it's required, and allows the user to jump to the location settings page (see code below):

context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
gOnZo
  • 489
  • 4
  • 15
  • My locnProviders contains a "passive" even my location is disabled. I think it should be excluded. – Jks Liu Apr 29 '19 at 09:30
  • On a Google Pixel running Android 9, with 'Use Location' turned OFF, locnProviders.isEmpty() returns true. What platform & android version are you testing? – gOnZo May 01 '19 at 19:32
  • Samsung Galaxy S9+ & Android 9 – Jks Liu May 07 '19 at 03:32
1

This is related to permissions....since API level 27 you need either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission. You may also need CHANGE_WIFI_STATE for Android 9 (that's the case for wifi scan anyway as per https://developer.android.com/guide/topics/connectivity/wifi-scan)

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
  • I already tried this, NOT WORKING, getting networkInfo.getExtraInfo() as null and wifiInfo.getSSID() as "". – Abhinav Tiwari Oct 26 '18 at 09:56
  • Are you specifically requesting those permissions from the user? – John O'Reilly Oct 26 '18 at 10:02
  • 1
    @AbhinavTiwari since Android 6 you also have to explicitly ask user for those permissions (it's not sufficient to just declare them in manifest file) - https://developer.android.com/training/permissions/requesting – John O'Reilly Oct 29 '18 at 07:53
1

You can access to the ssid wifi that you are connnected to it in Android 8 with location permission, but in android 9 you have to turn on location. read more here: https://developer.android.com/about/versions/pie/android-9.0-changes-all

Mohammad Davari
  • 430
  • 3
  • 13