Search code examples
androidioscordovaandroid-wifismartphone

Cordova - Checking WIFI connection to internet


I use Cordova to develop a smartphone application.

In this app, I need to check the internet connection before send request to a server.

In order to do that I use the Cordova Connection API, but in the case that the device is connected to a WIFI network with no Internet connection, this API say that we have WIFI connection,

is there any way to check if internet is available on the WIFI network ?


Solution

  • Send an Dummy ajax request before you send the actual request, If you get and Error Code as '0' it means there is no internet connectivity.

    $.ajax({
        url: 'TestUrl',
        type: 'GET',
        success: function (data) {
                    // Go ahead with you request
        },
        error: function (x, y, z) {
            if (x.status == 0) {
                alert("Please connect to the internet");
            }
           else{
               alert("Other Error Occured")
            }
        }
    });
    

    Secondly you can also make you of HTML 5 navigator

    var condition = navigator.onLine ? "ONLINE" : "OFFLINE";