For some reason the Phonegap connection API returns null
function checkConnection() {
var networkState = navigator.network.connection.type;
alert(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';
document.getElementById('connections').innerHTML = states[networkState];
}
I run this function from the onDeviceReady function which is called when the device is running.
When everything is turned off I get null. Shouldn’t I get the Connection.NONE back instead of a null? It’s really annoying since I can’t check whether the phone has connection or not. It works fine when Wifi is turned on though.
Have a look at the PhoneGap discussion group: https://groups.google.com/forum/?fromgroups#!searchin/phonegap/navigator.network.connection.type/phonegap/oAggxryQzrw/hE8uhwN3ONgJ
If you’re using 1.6.0, you need to update res/xml/plugins.xml from
to be:
(You need to remove the space in “Network Status”)