DEV Community

Weavri
Weavri

Posted on

Android 12+ returns <unknown ssid> for WiFi SSID — the permission fix

You ship a WiFi feature. Users report it "doesn't work". You log the SSID and find this:

<unknown ssid>
Enter fullscreen mode Exit fullscreen mode

Your code is fine. Your manifest has ACCESS_WIFI_STATE. And yet. Welcome to the Android 12 permission model.

The symptom

val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val ssid = wifiManager.connectionInfo?.ssid   // "<unknown ssid>"
Enter fullscreen mode Exit fullscreen mode

No crash. No exception. Just silent garbage — which is the worst kind of failure.

The root cause

Starting with Android 12 (API 31), reading the WiFi SSID requires a runtime permission. Declaring it in the manifest is not enough.

You have two options:

  • ACCESS_FINE_LOCATION — the legacy route, still works, still prompts
  • NEARBY_WIFI_DEVICES — new in Android 13 (API 33), and the semantically correct one for "I just want to see WiFi devices, not track location"

If the permission is declared but not granted, the API returns <unknown ssid> instead of throwing.

Working code

fun getCurrentSsid(context: Context): String? {
    val wifiManager = context.applicationContext
        .getSystemService(Context.WIFI_SERVICE) as WifiManager
    val info = wifiManager.connectionInfo ?: return null
    return info.ssid?.removeSurrounding("\"")
}
Enter fullscreen mode Exit fullscreen mode

And the runtime request, version-gated:

if (Build.VERSION.SDK_INT >= 33) {
    requestPermission(Manifest.permission.NEARBY_WIFI_DEVICES)
} else if (Build.VERSION.SDK_INT >= 31) {
    requestPermission(Manifest.permission.ACCESS_FINE_LOCATION)
}
Enter fullscreen mode Exit fullscreen mode

Three traps I actually hit

1. The quote trap. connectionInfo.ssid comes back quoted — literally "HomeWiFi" with the double quotes included. Always removeSurrounding("\"") before comparing, or your SSID match will never fire.

2. On newer OEM builds, NEARBY_WIFI_DEVICES is a hard dependency. Tested on a Huawei device running Android 14: without the nearby-devices permission you get <unknown ssid> even with location granted. Don't assume location alone is enough on API 33+.

3. Never treat <unknown ssid> as a valid value. Explicitly null-check and reject the sentinel. Otherwise you'll end up with a saved rule named <unknown ssid> that silently matches nothing.

Where I used this

WiFi Beacon (wifiwarn) matches a combination: the currently connected WiFi SSID and the foreground app. When both match, it warns you.

Source: github.com/weavrikx/wifiwarn — issues and stars welcome.


I'm Weavri, an indie Android developer. Everything I ship is tested on real hardware (Android 14) and reproducible.

Top comments (0)