I'm trying to find if scanResult
is the current connected wifi network.
here is my code
public boolean IsCurrentConnectedWifi(ScanResult scanResult)
{
WifiManager mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo currentWifi = mainWifi.getConnectionInfo();
if(currentWifi != null)
{
if(currentWifi.getSSID() != null)
{
if(currentWifi.getSSID() == scanResult.SSID)
return true;
}
}
return false;
}
I have no problem on getting scanresult.
Always I'm getting currentWifi is null.
Where am I wrong or Is there any alternative method to do this?
Need to know the wifi access Point name in android null) { WifiInfo info = wifiManager. getConnectionInfo(); if (info != null) { String ssid = info. getSSID(); ... } }
To check the Wi-Fi statusOn the home screen, tap Apps > Settings. Under Network Connections, tap Wi-Fi; then tap the connected Wi-Fi network. Check the Signal strength.
With most Android smartphones you can dial *#*#4636#*#8* to get a menu with among the choices “wifi information”. This will tell you which BSSID you are connected to and at what link speed.
Most probably you have already found answer: currentWifi.getSSID()
is quoted in most cases where scanResult.SSID
is not (and of course you must not use ==
on strings :)).
Try something like this, it returns current SSID
or null
:
public static String getCurrentSsid(Context context) {
String ssid = null;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
ssid = connectionInfo.getSSID();
}
}
return ssid;
}
also permissions are required:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
StringUtil
is not a standard Android class, so you can use TextUtils
instead. The code then looks like this:
public static String getCurrentSsid(Context context) {
String ssid = null;
ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
ssid = connectionInfo.getSSID();
}
}
return ssid;
}
My (used to be) non-deprecated, modified approach to the current answer
https://developer.android.com/reference/android/net/ConnectivityManager#getActiveNetworkInfo()
public static String getCurrentSsid(Context context) {
String ssid = null;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo == null) {
return null;
}
if (networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
ssid = connectionInfo.getSSID();
}
}
return ssid;
}
Update Android Oreo. You will also need to add ACCESS_FINE_LOCATION Permission in Manifest otherwise it will return unknown SSID
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
fun getCurrentSsid(context: Context): String {
var ssid = "NA"
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
if (networkInfo.isConnected) {
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val connectionInfo = wifiManager.connectionInfo
if (connectionInfo != null && !connectionInfo.ssid.isEmpty()) {
ssid = connectionInfo.ssid
}
}
return ssid
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With