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
This is related to permissions....since API level 27 you need either
ACCESS_FINE_LOCATIONorACCESS_COARSE_LOCATIONpermission. You may also needCHANGE_WIFI_STATEfor Android 9 (that's the case for wifi scan anyway as per https://developer.android.com/guide/topics/connectivity/wifi-scan)