I'm trying to get the SSID of the WIFI network when my android device is connected to WIFI.
I've registered a BroadcastReceiver listening for android.net.wifi.supplicant.CONNECTION_CHANGE
. I get the notification when WIFI is disconnected or reconnected. Unfortunately, I can't get the network's SSID.
I'm using the following code to find the SSID:
WifiManager wifiManager = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
Instead of the SSID, I get the string <unknown ssid>
back.
These are the permissions in the manifest (I've added ACCESS_NETWORK_STATE just to check, I don't actually need it)
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Why does this happen? How can I get the actual SSID? Is the broadcast fired to early, before the connection is established? Is there another broadcast I should listen to? I'm only interested in WIFI connections, not 3G connections.
Update: I just checked, wifiInfo.getBSSID()
returns null.