Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SSID when WIFI is connected

Tags:

android

wifi

ssid

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.

like image 538
zmbq Avatar asked Jan 27 '14 20:01

zmbq


People also ask

What is SSID when it comes to Wi-Fi?

A service set identifier (SSID) is a sequence of characters that uniquely names a wireless local area network (WLAN). An SSID is sometimes referred to as a "network name." This name allows stations to connect to the desired network when multiple independent networks operate in the same physical area.


1 Answers

I listen for WifiManager.NETWORK_STATE_CHANGED_ACTION in a broadcast receiver

if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {     NetworkInfo netInfo = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO);     if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) {         ...     } } 

I check for netInfo.isConnected(). Then I am able to use

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifiManager.getConnectionInfo(); String ssid  = info.getSSID(); 

UPDATE

From android 8.0 onwards we wont be getting SSID of the connected network unless location services are enabled and your app has the permission to access it.

like image 166
Eric Woodruff Avatar answered Sep 29 '22 11:09

Eric Woodruff