network connection status from PhoneGap does not update in Ripple
Asked Answered
M

2

7

The standard PhoneGap API for checking the current network connection (see below) does not seem to update it's result in Ripple Emulator. When I change connection type and execute checkConnection(), it returns the connection type from the first call to this function (at deviceready)

function checkConnection() {    // Checks current network status
    networkState = navigator.connection.type;
    console.log(networkState);

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.NONE]     = 'No network connection';

    // DEBUG: notify on network state
    console.log("Connection type: " + states[networkState]);
}

Is there anything else I need to do (add an event listener?) to get this to work? Normally a device will only fire an event when the status changes from online to offline, but I need to distinguish between 'free' internet and xG connections.

Morna answered 11/4, 2013 at 13:42 Comment(1)
Is this really such a poor question? Or did nobody encounter this problem, ever?Morna
T
8

Apparently navigator.connection.type isn't updating in cordova on the windows platform. Looking at the code, navigator.connection.type is only set once after the 'deviceready' event.

Trick is to manually update it using the Connection#getInfo() on related events

var connection = navigator.connection;
function errorCallback(e) {
  console.warn(e);
};
function updateConnection(info) {
  connection.type = info;
}
document.addEventListener("resume", function() {
  connection.getInfo(updateConnection, errorCallback);
});
document.addEventListener("online", function() {
  connection.getInfo(updateConnection, errorCallback);
});
document.addEventListener("offline", function() {
  connection.getInfo(updateConnection, errorCallback);
});

Disclaimer: Refactored/Copy-pasted pieces from of my own code, should work out of the box.

Twine answered 6/1, 2015 at 8:32 Comment(1)
This totally works, but how the heck did you discover it? I can't find anything about this except your answer and, now that I know what to look for, the cordova source.Martz
L
-1

I would say try to add a timeout function to delay the call. I've seen similar issues already when this solved the problem. First do the:

networkState = navigator.connection.type;

then:

setTimeout(function(){

    networkState = navigator.connection.type; // have to do this second time to pick up the refreshed value
    console.log(networkState);

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.NONE]     = 'No network connection';

    // DEBUG: notify on network state
    console.log("Connection type: " + states[networkState]);
}, 500);

This gives time (500 ms in this example) to navigator.connection.type to refresh it's value.

Lashkar answered 24/10, 2013 at 16:28 Comment(1)
@bleuscyther if that works consistently that's a lot better solution. I'm pretty sure my network status initialization ran after onDeviceReady() but it still failed sometimes. I'll need to check it again ;) Cheers for the updateLashkar

© 2022 - 2024 — McMap. All rights reserved.