6

I am working on an application that checks, before enabling a download, that the connection is reliable (basically the connection should be an infrastructure wifi and not data pack) But in case the user is either

  1. using an ad hoc network, or
  2. mobile device's internet connection as WAP

and then connecting and starting the download on desktop, it is still undesired. Is there a way to detect if some wifi connection is actually not from ad hoc or using phone's WAP?

notrai
  • 696
  • 1
  • 5
  • 15
  • Using a mobile device as a hotspot is also infrastructure. Ad-Hoc is a connection between two devices only, but the phone is the WAP when used as a hotspot since multiple clients can connect to it. – Ron Maupin Jan 18 '16 at 18:24
  • thanks RON for correcting me on this, now that this is clear, i will edit the question accordingly – notrai Jan 19 '16 at 05:51
  • If you are doing this from a desktop, I don't think you can tell the difference between a regular infrastructure WAP or a phone as a hotspot WAP, since the phone is just another infrastructure WAP. An ad hoc connection should not have access to the Internet, so that is easy. – Ron Maupin Feb 06 '16 at 17:45
  • @rai: any comment on my answer below? Does it solve your problem? – PhilLab Feb 10 '16 at 09:09

3 Answers3

3

You can detect...

  1. ...the Ad-Hoc Network by checking if you actually can access a server on the internet - usually, Ad-Hoc does not include fully connectivity so if you have network but no internet access, a download won't work
  2. ...the usage of a phone access point by measuring the round-trip time of a request - they are usually quite high on mobile broadband.

    long start = System.nanoTime();
    HttpGet requestForTest = new HttpGet("http://m.google.com");
    try {
        new DefaultHttpClient().execute(requestForTest); // can last...
    } 
    catch (Exception e) {
    }
    long rtt = System.nanoTime() - start;
    // ... evaluate the rtt
    

    This table may be relevant for the evaluation (source)

    Generation | Data rate      | Latency
    2G         | 100–400 Kbit/s | 300–1000 ms
    3G         | 0.5–5 Mbit/s   | 100–500 ms
    4G         | 1–50 Mbit/s    | < 100 ms
    

Apart from those two options: why do you specifically ban Ad-Hoc or mobile broadband? Shouldn't you either

  • ban nothing and let the user decide if they want to wait for ages

or

  • ban ALL slow connections, i.e. monitoring the transfer rate for a couple of seconds and cancel the download automatically if it is too small
Community
  • 1
  • 1
PhilLab
  • 4,777
  • 1
  • 25
  • 77
1

Essentially, you're writing an Application on layer 7 of the ISO/OSI model and you want to know contents of layer 2. That is explicitly not how it should work.

  • If your download is big, you should protect the user from burning up their mobile contract's data volume and notify the user before executing the download.
    That is also how the Google Play store handles downloads bigger than a few MBytes.
  • you could have an option in the preferences of your app to only allow downloads when on Wi-Fi so users can be sure to save their data plan.
  • If you absolutely don't want a user to download your files over mobile network, only allow Wi-Fi and then do a pind and a download of a 1MB file to measure the bandwidth before deciding if the actual download will take forever or not.

All the rest should be handled by the system and doesn't need to concern you.

TabascoEye
  • 656
  • 5
  • 24
  • How can the desktop know that the Wi-Fi is via the mobile phone? All it knows is that it is connected to Wi-Fi. – Ron Maupin Feb 09 '16 at 09:19
  • The Desktop isn't supposed to know. Wanting to know how a device is connected on layer 2 is just a lame excuse for wanting to be in control of how your app is used (be it desktop or mobile). If your app can't handle slow/bad connections then fix your app, not force the user to get more bandwidth. – TabascoEye Feb 09 '16 at 10:24
  • But that is the question. The desktop needs to know if it is connected to a regular infrastructure or a mobile hotspot. I didn't ask the question, but that is the question the OP needs answered. I don't think the desktop can reliably tell the difference – Ron Maupin Feb 09 '16 at 10:29
  • Well then the only answer can be "no it doesn't need to know and there is no way it can know, apart from half-educated guesses like MAC address of the AP which can be spoofed" – TabascoEye Feb 09 '16 at 10:33
-2

to check if its connected to wifi:

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (wifi.isConnected()) {
    // is connected to wifi
}

don't forget to add permission to Manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
633kM
  • 300
  • 1
  • 9