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.
In Android 8.1 it is must to turned Location on to get SSID, if not you can get connection state but not SSID
Android 9 SSID showing NULL values use this code..
Check via NetworkInfo for wifi-type if it is connected. And then use wifiinfo getSSid(). You might want to remove double slashes from returnd SSID
https://play.google.com/store/apps/details?id=com.connect.freewifi
You should check out this application and developer api from http://developer.android.com/reference/android/net/wifi/WifiInfo.html
It will help you with your task.
If you don't want to make Broadcast Receiver then simple try
Remember every time user disconnect or connect to new SSID or any wifi state change then you need to initialize WifiInfo i.e
wifiInfo = wifiManager.getConnectionInfo();
Starting with Android 8.1 (API 27), apps must be granted the
ACCESS_COARSE_LOCATION
(orACCESS_FINE_LOCATION
) permission in order to obtain results fromWifiInfo.getSSID()
orWifiInfo.getBSSID()
.This permission is also needed to obtain results from
WifiManager.getConnectionInfo()
andWifiManager.getScanResults()
although it is not clear if this is new in 8.1 or was required previously.Source: "BSSID/SSID can be used to deduce location, so require the same location permissions for access to these WifiInfo fields requested using WifiManager.getConnectionInfo() as for WifiManager.getScanResults()."
This is a follow up to the answer given by @EricWoodruff.
You could use
netInfo
'sgetExtraInfo()
to get wifi SSID.If you are not using BroadcastReceiver check this answer to get SSID using
Context
This is tested on Android Oreo 8.1.0