14

i have a similar requirement as in link below but i have to handle it by using JavaScript. where i have to detect whether the mobile internet connection is 2g/3g or it is WIFI . based on connection i have to perform diffent operations.. note : mobile can b of any OS like andriod/ iOS/BB .. i need to handle any mobile OS.

Is there a way to detect what kind of connection I'm using ? WiFi, 3G or Ethernet?

request masters to help me with inputs. thanks :)

Community
  • 1
  • 1
user1708054
  • 191
  • 1
  • 1
  • 19
  • possible duplicate of [How do I check connection type (WiFi/LAN/WWAN) using HTML5/JavaScript?](http://stackoverflow.com/questions/11701328/how-do-i-check-connection-type-wifi-lan-wwan-using-html5-javascript) – CD.. Sep 22 '14 at 06:52

2 Answers2

29

Network Information API (This is an experimental technology):

The Network Information API provides information about the system's connection, which is in term of general connection type (e.g., 'wifi', 'cellular', etc.). This can be used to select high definition content or low definition content based on the user's connection. The entire API consists of the addition of the domxref("NetworkInformation") interface and a single property to the Navigator interface: Navigator.connection.

var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
var type = connection.type;

function updateConnectionStatus() {
  alert("Connection type is change from " + type + " to " + connection.type);
}

connection.addEventListener('typechange', updateConnectionStatus);
CD..
  • 72,281
  • 25
  • 154
  • 163
-3

I wrote a small utility to do this. You can try it here http://ashanbh.github.io/detectClientSpeed/example2.html and fork it on github: https://github.com/ashanbh/detectClientSpeed

Usage:

<script src="scripts/require.js"></script>
<script>
        requirejs(['scripts/detectSpeed'], function (detectSpeed) {

            //Callback to receive timing information
            var callback = function (timings) {
                console.log(timings);
            }
            detectSpeed.startSpeedCheck("https://s3-us-west-1.amazonaws.com/amit.shanbhag/3g/coffee-apple-iphone-laptop.jpg", callback);

        });
</script>
  • This is an inaccurate way to detect download speed not connection type. There are several variables that come into the portrait when detecting download speed, one of which being latency. – THE AMAZING Sep 15 '15 at 15:43
  • True. It detects speed, not connection type. However, practically speaking, when i'm deciding to turn feature on/off on the client side, i care more about actual perceived speed. – Amit Shanbhag Oct 05 '15 at 03:19
  • I hope you realize latency,jitter, and bandwidth restrictions in networks can limit any form of network; rendering this form of detection useless. – THE AMAZING Oct 05 '15 at 17:29
  • You would be better off detecting screen resolution and user agent. Since in most cases users will be on a mobile device if they aren't connected through wifi or ethernet. Just my opinion. – THE AMAZING Oct 05 '15 at 17:36
  • "You would be better off detecting screen resolution and user agent." So the user-agent nor resolution changes tells me when my phone switches from 3g to wifi. – Amit Shanbhag Oct 12 '15 at 08:20
  • No but it can make an assumption that the user is on a mobile browser. What you are trying to achieve can not be done at this time. – THE AMAZING Oct 13 '15 at 23:19