5

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.

Shalu T D
  • 3,921
  • 2
  • 26
  • 37
Priyavrat
  • 451
  • 3
  • 14
  • 1
    in general, `mobile hotspot` has no difference from `WiFi access point`. In fact, every `mobile hotspot` is the `WiFi access point` itself. Same way some WIFI routers have either SIM-card slot, or USB for mobile modem connection. – Vladyslav Matviienko Aug 22 '19 at 09:40
  • Thanks @Vladyslav for explaining it more, but I want to check if the mobile hotspot/ access point's source is a 2G/3G/4G then I have to ignore it. – Priyavrat Aug 22 '19 at 09:43
  • I don;t think you can check the source. only thing I can think of - checking by IP address for the ISP. So if the ISP is in the list of mobile operators - then it is *likely* mobile network – Vladyslav Matviienko Aug 22 '19 at 09:45
  • May you help me with the code that how can we find the ISP from IP address, so that we can figure it out, is it a mobile operator or not. – Priyavrat Aug 22 '19 at 09:46
  • there is no such code. You need to find such online API to get ISP from the IP address – Vladyslav Matviienko Aug 22 '19 at 09:52
  • Maybe meassuring the connection speed would be another approach? if the connetion speed is bellow avarage wifi speed you could assume the connection is eiter 2G or 3G. if the connection is above or equla that assume WIFI or hotspot 4G. Again not clear what is the difference if the hotsport host connection is 2G/3G/4G I would only assume security issues or speed issues?? – SomethingCool Sep 02 '19 at 15:14
  • Any update on this? @Priyavrat – Shailendra Madda Nov 02 '21 at 09:34
  • @ShailendraMadda Not yet, if you find anything, kindly mark it here. – Priyavrat Nov 03 '21 at 01:05

4 Answers4

0

Once there's a connection change, you can use a broadcast receiver to check whether you're connected to Wi-Fi or not.

class NetworkChangeReceiver(private val application: Application) : 
ConnectivityManager.NetworkCallback() {

private val NETWORK_CHANGE_TAG = NetworkChangeReceiver::class.java.simpleName
private var networkRequest: NetworkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build()

init {
val connectivityManager =     application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.registerNetworkCallback(networkRequest, this)
}

override fun onAvailable(network: Network?) {
super.onAvailable(network)
Timber.i(NETWORK_CHANGE_TAG.plus("onAvailable"))
application.toast("onAvailable")
}

override fun onLost(network: Network?) {
super.onLost(network)
Timber.i(NETWORK_CHANGE_TAG.plus("onLost"))
application.toast("onLost")
    }
}

To determine the operator of the connected network, you can get an account from here. Just give a call to https://ipinfo.io?token=$TOKEN with your token.

You will be given a JSON response with complete details of the network connected. For example:

ip: "115.xxx.xx.xxx"
city: ""
region: ""
country: "IN"
loc: "2x.0000,7x.0000"
asn: Object
asn: "AS18101"
name: "Reliance Communications Ltd"
domain: "reliance.com"
route: "115.xxx.xx.0/22"
type: "isp"
company: Object
name: "RCOM-Static-DIA"
domain: "reliance.com"
type: "isp"
abuse: Object
address: "Reliance Communication Ltd"
country: "IN"
email: "support@reliance.com"
name: "Antiabus"
network: "115.xxx.xx.0/20"
phone: "xx30334141-5"

You can use this service for free, if you require only less than 50K API calls per month.

Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77
  • Using broadcast receiver is anyway, deprecated. I don't to use any third party Url for same. Kindly help me with if there is any way via android API's to check that whether it is connected to WiFi access point or Mobile hotspot. – Priyavrat Aug 29 '19 at 09:57
  • **Using broadcast receiver is anyway, deprecated.** who told you that, without any third party API, i don't think there's a possible solution for it @Priyavrat – Manoj Perumarath Aug 29 '19 at 10:16
  • https://developer.android.com/reference/android/net/ConnectivityManager Please go through this, It tells the API deprecated from connectivity manager. – Priyavrat Aug 30 '19 at 07:24
  • @Priyavrat for every deprecated method there's an alternative method provided by google. There's `NetworkCallback` for that purpose, this is the newer method – Manoj Perumarath Aug 30 '19 at 07:28
  • Agree with you @Manoj, that for every deprecated method there is alternative method, I am already using the NetworkCallback by the way, but there is no such way to identify, if we can find whether it is connected to mobile access point or actual wifi access point. Please let me know, if you can help in the same way, otherwise we can close the discussion here. Thanks for all you help. – Priyavrat Aug 30 '19 at 07:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/198715/discussion-between-manoj-perumarath-and-priyavrat). – Manoj Perumarath Aug 30 '19 at 09:28
0

I researched a bit on this and got to a point that all the MOBILE DEVICES use an ipv4 range of 192.168.43.1 to 192.168.43.254 so it is possible to differentiate mobile AP from ACTUAL wifi on the basis of this.

I wrote a little code to make it clear:

  WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    System.out.println(ip);
    String []wifi_ip = ip.split(".");
    if(wifi_ip[3].equals(43)){
        if(parseInt(wifi_ip[4])>=1 && parseInt(wifi_ip[4])<=254){
            //your code goes here
        }
    }

add this to your manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

What this code does ?
It actually check the device's IPv4 address and then checks whether ip of the device lies in this range or not.

Rishabh Sehgal
  • 45
  • 2
  • 11
  • I have below queries on same. - What if the IP address is IPv6 ? - wm.getConnectionInfo().getIpAddress() finds the mobile hotspot's IP ? Please confirm. - As you mentioned 192.168.43.1 to 192.168.43.254, is the range of IPv4, May you please tell me that where it is mentioned ? Anyways, thanks for all your efforts here. :) – Priyavrat Aug 30 '19 at 07:35
  • hey as per your questions here are the answers, > if the IP address is based on IPv6 then this method will not work as we don't have enough android devices assigning IPv6 to their connected devices so its hard to predict the IPv6 addresses based upon their make/model. > wm.getConnectionInfo().getIpAddress() finds the actual ip assigned to your phone when its connected to WIFI. > its been tested several times that android devices operate upon this range of IPv4 address Reference: https://stackoverflow.com/questions/17302220/android-get-ip-address-of-a-hotspot-providing-device – Rishabh Sehgal Aug 30 '19 at 07:58
0

checking the default gateway of the network solves this issue.in general,hotspots gateways are unreachable where as the routers gateways loads their default setup page or login page whenever you hit the default gateway address in the browser.

BE Aware
  • 3
  • 2
  • Welcome to Stack Overflow. You appear to have misunderstood the question, which was asking how to write code that checks if a device is connected to Wi-Fi. You've answered a totally unrelated question about fixing network errors. – Ryan M Oct 21 '20 at 06:26
-3

use this library i will update instantly.

implementation 'com.github.pwittchen:reactivenetwork-rx2:3.0.4'
olibiaz
  • 2,551
  • 4
  • 29
  • 31
  • May you please tell me, what this library does. Also it will be great if you share the url/ link to the same. – Priyavrat Aug 22 '19 at 09:59
  • you can check connection status mobile data or wifi – ujwall patel Aug 22 '19 at 10:02
  • That's not what I need. I need to check if the connected Wifi network is a mobile hotspot. Kindly check the question. – Priyavrat Aug 22 '19 at 10:03
  • 1
    This is not an answer. You have provided no information on how this library works, what it does or how it solves the problem. You haven't even provided a link for documentation regarding this library. – a_local_nobody Aug 27 '19 at 06:57