Yes, there is a way. You need a class called WifiInfo
Add these permissions to the manifest
file:
<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"/>
And the code specifically, without the use of a broadcast:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo;
String ssid = null;
wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
ssid = wifiInfo.getSSID();
}
- From Android 8.1 (API 27), you also need to add this permission to
manifest
, if you are working with this API:
ACCESS_COARSE_LOCATION
or ACCESS_FINE_LOCATION
Source: link