Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check device network in Phonegap

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 921
    Comment on it

    Cordova provides cordova-plugin-network-information that checks the cellular and wifi connection of the device and also it can check the internet connection.

    Installation:

    To install this plugin run the following command
     

    cordova plugin add cordova-plugin-network-information

    Supported Platforms:

       * Amazon Fire OS
       * Android
       * BlackBerry 10
       * Browser
       * iOS
       * Windows Phone 7 and 8
       * Tizen
       * Windows
       * Firefox OS

    Connection: There is an object connection which you can get by using navigator.connection. This provides information about networks of device.

    Properties:

       * connection.type

    Constants

       * Connection.UNKNOWN
       * Connection.ETHERNET
       * Connection.WIFI
       * Connection.CELL_2G
       * Connection.CELL_3G
       * Connection.CELL_4G
       * Connection.CELL
       * Connection.NONE

    connection.type:

    It identifies the network information state and connection type of the device.

    Example:

    function checkNetwork() {
        var network_State = navigator.connection.type;
    
        var n_states = {};
        n_states[Connection.UNKNOWN]  = 'Unknown connection';
        n_states[Connection.ETHERNET] = 'Ethernet connection';
        n_states[Connection.WIFI]     = 'WiFi connection';
        n_states[Connection.CELL_2G]  = 'Cell 2G connection';
        n_states[Connection.CELL_3G]  = 'Cell 3G connection';
        n_states[Connection.CELL_4G]  = 'Cell 4G connection';
        n_states[Connection.CELL]     = 'Cell generic connection';
        n_states[Connection.NONE]     = 'No network connection';
    
        console.log(Connection type: ' + n_states[network_State]);
    }
    
    checkNetwork();

    Network-related Events:

    offline

    when the device has no internet connection, this event fires.

    document.addEventListener("offline", onOffline, false);
    
    function onOffline() {
        // Handle the offline event
    }
    
    


    online

    When a device which is not connected to internet changes its state to connected to internet, this event fires.

    document.addEventListener("online", onOnline, false);
    
    function onOnline() {
        // Handle the online event
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: