Jake Graham Arnold
Jake Graham Arnold

Reputation: 1446

Alternative for ConnectivityManager - NetworkInfo - getExtraInfo() as its null in Android P

This method started returning null in Android P (API 28)

private String getSSID() {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE)
    final NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.getExtraInfo() != null) {
        return StringUtils.removeQuotes(networkInfo.getExtraInfo());
    } 

    return null;
}

Upvotes: 0

Views: 797

Answers (1)

Jake Graham Arnold
Jake Graham Arnold

Reputation: 1446

This can be updated to...

private String getSSID() {
    final WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    fina WifiInfo wifiInfo = wifiManager.getConnectionInfo();

    if (wifiInfo != null && wifiInfo.getSSID() != null) {
        return StringUtils.removeQuotes(wifiInfo.getSSID());
    }

    return null;
}

NetworkInfo

getExtraInfo()

Returns null when not available.

Based on my testing with Android P - it seems to always be null

Upvotes: 2

Related Questions