I need to check the current connected wifi SSID on android. I checked with Nokia 6 and OnePlus 5 with respectively Oreo 8.1.0 and Oreo 8.0. Other phones with different OS v
You need to request permission in manifest :
and request it in your activity :
private void grantPermm()
{
try
{
if (ContextCompat.checkSelfPermission(MainScreenActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
}
else
{
ActivityCompat.requestPermissions(MainScreenActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
101);
}
}
catch (Exception xx){}
}
then use this function :
public static String getWifiName(Context context)
{
String result="";
try {
WifiManager manager = (WifiManager) context.getSystemService(Context.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)
{
result = wifiInfo.getSSID();
if(result.equalsIgnoreCase(""))
{
Toast.makeText(context,"You are connected to mobile data", Toast.LENGTH_SHORT).show();
result="";
}
return result;
}
else
{
Toast.makeText(context,"WIFI not connected", Toast.LENGTH_SHORT).show();
return "";
}
}
else
{
Toast.makeText(context,"No WIFI Information", Toast.LENGTH_SHORT).show();
return "";
}
}
else
{
Toast.makeText(context,"WIFI not enabled", Toast.LENGTH_SHORT).show();
return "";
}
}
catch (Exception xx)
{
Toast.makeText(context, xx.getMessage().toString(), Toast.LENGTH_SHORT).show();
return "";
}
}