Requirement:- Need to run some network calls, only when the device is only connected to an actual WiFi access point, not to a mobile hotspot or cellular data.
We need to check that the currently connected network is a mobile hotspot. Currently, I am using the below code if it is connected to WiFi or Cellular network.
Network activeNetwork = connectivityManager.getActiveNetwork();
NetworkCapabilities networkCapabilities = mConnectivityManager.getNetworkCapabilities(activeNetwork);
boolean isWiFi = networkCapabilities.hasTransport(NetworkCapablities.TRANSPORT_WIFI);
To identify if the wifi connection is a mobile hotspot, I tried a workaround, by using the below method.
connectivityManager.isActiveNetworkMetered()
Along with active connection metered check, the Updated condition is as below
boolean isWiFiNotAMobileHotspot = networkCapablities.hasTransport(NetworkCapablities.TRANSPORT_WIFI) && connectivityManager.isActiveNetworkMetered();
Since isActiveNetworkMetered() only helps to identify if we are connected a metered connection (With a restriction of daily/ monthly data limits.)
So for most of the mobile hotspots, it gives the expected results but if the mobile hotspot has an unlimited data plan then isActiveNetworkMetered() returns false.
So, unfortunately, this solution is not the exact way to find if the device is connected to a mobile hotspot.
Kindly help me with the solution to check if the device is connected to a mobile hotspot or not.