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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With