Keywords: Android | Wi-Fi | SSID | Permission Management | BroadcastReceiver
Abstract: This article provides an in-depth exploration of techniques for retrieving the SSID when an Android device connects to a Wi-Fi network. It addresses common issues such as receiving <unknown ssid> by detailing the correct use of BroadcastReceiver to listen for WifiManager.NETWORK_STATE_CHANGED_ACTION, and emphasizes the importance of location permissions from Android 8.0 onwards. With code examples and permission configurations, it offers a holistic solution from basic implementation to compatibility considerations.
Problem Background and Common Pitfalls
In Android development, retrieving the SSID of the currently connected Wi-Fi network is a frequent requirement, but developers often encounter issues where <unknown ssid> or null values are returned. This is typically due to improper timing or incomplete permission configurations. Many developers initially attempt to use the android.net.wifi.supplicant.CONNECTION_CHANGE broadcast, but this may fire before the network connection is fully established, resulting in SSID information in the WifiInfo object not being updated yet.
Core Solution: Monitoring Network State Changes
The best practice is to register a BroadcastReceiver to listen for WifiManager.NETWORK_STATE_CHANGED_ACTION. This broadcast triggers when network state changes occur, including connection establishment and disconnection. In the receiver, the SSID can be retrieved as follows:
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
if (ConnectivityManager.TYPE_WIFI == netInfo.getType()) {
if (netInfo.isConnected()) {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
}
}
}
This approach ensures that SSID retrieval is attempted only after the Wi-Fi connection is fully established, avoiding the <unknown ssid> issue caused by premature access.
Critical Updates in Permission Configuration
Starting from Android 8.0 (API level 26), location permissions are required to obtain SSID and BSSID, as this information can be used to infer device geolocation. Specific permission requirements are:
- Android 8.0 to 8.1 (API 26-27):
ACCESS_COARSE_LOCATIONorACCESS_FINE_LOCATIONpermission is needed. - Android 10 and above (API 29+):
ACCESS_FINE_LOCATIONpermission is mandatory.
In AndroidManifest.xml, add:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Additionally, from Android 6.0 (API 23) onwards, ACCESS_FINE_LOCATION is a dangerous permission that must be requested dynamically at runtime.
Alternative Methods and Considerations
Beyond broadcast listening, another approach is to directly check the SupplicantState of WifiInfo. When the state is COMPLETED, it indicates authentication is finished, allowing safe SSID retrieval:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
String ssid = wifiInfo.getSSID();
}
However, this method requires developers to manually manage the timing of state checks, such as reinitializing the WifiInfo object on connection changes.
Compatibility and Best Practices
To ensure the application functions correctly across different Android versions, it is recommended to:
- Always check network connectivity status before retrieving SSID to avoid calling related methods when not connected.
- For Android 8.0 and above, explain the necessity of location permissions to users before requesting them (e.g., for network identification features).
- Handle permission denials in code with user-friendly prompts.
- Test the application's behavior during Wi-Fi connection, disconnection, and network switching to ensure accurate SSID retrieval.
By combining proper broadcast monitoring, permission management, and state verification, developers can reliably retrieve the SSID of the current Wi-Fi network on Android devices, enhancing the user experience of network-related functionalities.