I am working on an android project and I am trying to check if an android device is connected to Wifi, however, my function is always returning false, even though my device is successfully connected to Wifi.
In the constructor for the class I pass the context of the calling method, and within the constructor I create an instance of the ConnnectivityManager
Below is the constructor
public NetworkManagement(Context context)
{
this.context = context;
connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
}
Below is my function that I am having the problem with
public boolean isConnectedToWifi()
{
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected())
{
return true;
}
else
{
return false;
}
}
Even though my device is connected to a wifi network and I can successfully connect to the internet, the function above will always return false but I do not understand why.
Thanks for any help you can provide.