Get Wifi Information Programmatically in Android

Scott
3 min readDec 22, 2022

--

To get the WiFi Access Point name (aka the SSID or Service Set Identifier) using Java programming language in an Android application, you will need to use the Android Wi-Fi API. This API allows you to access the Wi-Fi functionality of an Android device, including scanning for available WiFi networks, connecting to a WiFi network, and getting the SSID of the currently connected WiFi network.

Here is an example of how to get the WiFi Access Point name in an Android application using Java:

  1. First, you will need to add the permissions to your AndroidManifest.xml file. These permissions allow your application to access the Wi-Fi functionality of the device.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

2. Next, you will need to import the following classes:

import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;

3. In your activity or fragment, get a reference to the WifiManager object:

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

4. Use the WifiManager object to get the WifiInfo object, which contains information about the currently connected WiFi network:

WifiInfo wifiInfo = wifiManager.getConnectionInfo();

5. Use the WifiInfo object to get the SSID of the currently connected WiFi network:

String wifiname = wifiInfo.getSSID();

If the device is not currently connected to a WiFi network, the getSSID() method will return a null value. Also, the result from getSSID might have quotes which doesn’t look very nice, it’s better to remove them as well. I summarize all the steps in the following example:

public String myGetWifiName(Context context) {
String wifiname = null;
try {
WifiManager manager = (WifiManager) context.getApplicationContext().getSystemService(WIFI_SERVICE);
if (manager.isWifiEnabled()) {
WifiInfo wifiInfo = manager.getConnectionInfo();
if (wifiInfo != null) {
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
wifiname = wifiInfo.getSSID();
if (wifiname.startsWith("\"") && wifiname.endsWith("\""))
wifiname = wifiname.substring(1, wifiname.length() - 1);
Log.d("WifiName", "WifiName is: " + wifiname);
return wifiname;
}
}
}
} catch (Exception e) {
}
return "";
}

In some cases, your phone may switch between different Wifi networks, and it’s better for your app to be notified when that happens. That’s more efficient than your app constantly check the Wifi AP name.

In that case, you need to create a BroadcastReceiver class:

public class WifiBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

String action = intent.getAction();
if (WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(action)) {
SupplicantState state = intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
if (SupplicantState.isValidState(state) && state == SupplicantState.COMPLETED) {
wifiname = myGetWifiName(context);

//notified each of your listners
Iterator it = mWifiListeners.iterator();
while(it.hasNext()){
WiFiNameChangeListener listerner = (WiFiNameChangeListener) it.next();
listerner.wifiNameChanged(wifiname);
}
}
}
}
}

And then register it in your activity :

BroadcastReceiver broadcastReceiver = new WifiBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
context.registerReceiver(broadcastReceiver, intentFilter);

I hope this helps! If you want to see a live demo, please try one of my apps from Google Play. It always shows Wifi AP name on top-right corner of the screen.

Let me know if you have any other questions.

--

--