Reputation: 2693
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.
Upvotes: 0
Views: 11697
Reputation: 23
Use this code:
var networkState = navigator.connection.type;
Instead of:
var networkState = navigator.network.connection.type;
Upvotes: 0
Reputation: 1775
There is another change when going from pre 2.3.0 to post 2.3.0.
Before Cordova 2.3.0, the Connection object existed at: navigator.network.connection.
To match the spec, this was changed to navigator.connection in 2.3.0.
navigator.network.connection still exists, but is now deprecated and will be removed in a future release.
So change this
var networkState = navigator.network.connection.type;
To This
var networkState = navigator.connection.type;
Basically remove the network from your Javascript. Hope this helps.
Upvotes: 1
Reputation: 11
For the cordova version prior to 1.6 you need to use:
<plugin name="Network Status" value="org.apache.cordova.NetworkManager"/>
Instead of:
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
Please change the plugin name to "Network Status" with space!
Upvotes: 1
Reputation: 1376
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
<plugin name="Network Status" value="org.apache.cordova.NetworkManager"/>
to be:
<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager"/>
(You need to remove the space in "Network Status")
Upvotes: 1
Reputation: 362
Make sure you have added
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
in AndroidManifest.xml
Upvotes: 2
Reputation: 11
I'm currently using PhoneGap 1.4.1 and Sencha Touch 1.1.1. on Android 2.3.5 (tested with HTC Desire S). It seems that the "navigator.network.connection.type" statement does effectively return 'null' with Phonegap 1.4.1, even if called after 'deviceready' event has been fired (confirmed after checking the source code). However it perfectly works with Cordova (ex-PhoneGap) 1.5.0. So I suggest to upgrade asap :-)
Eric.
Upvotes: 1