checking of internet connection of device using phonegap
Asked Answered
P

3

6

Trying to use cordova 2.0.0 and using this code for checking of internet connection

document.addEventListener("deviceready", onDeviceReady(), false);

function onDeviceReady() {
    alert("ready");
    db = window.openDatabase("loginintro", "1.0", "loginintro", 1000000);
    db.transaction(populateDB, errorCB, successCB);
    checkConnection();
}
function checkConnection()
{   alert("connection");
    network = navigator.network.connection.type;
    alert("fdfd");
    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';
    alert('Connection type: ' + states[networkState]);
}

but getting error on this line

network = navigator.network.connection.type;

and error is:

04-09 15:20:23.989: E/Web Console(13329): Uncaught TypeError: Cannot read property 'connection' of undefined at file:///android_asset/www/js/lib/helloworldjs/alldatabse.js:14
04-09 15:20:39.419: E/CordovaWebView(13329): CordovaWebView: TIMEOUT ERROR!
04-09 15:20:39.419: D/Cordova(13329): CordovaWebViewClient.onReceivedError: Error code=-6 Description=The connection to the server was unsuccessful. URL=file:///android_asset/www/index.html
Per answered 9/4, 2013 at 5:47 Comment(3)
You have small mistake in the code. It should be alert('Connection type: ' + states[network])Semantic
good catch that was the only mistake now my function is working fine THANKSPer
its onDeviceReady not onDeviceReady()Shredding
S
4

You have small mistake in the code. It should be alert('Connection type: ' + states[network]);

In addition, make sure you have these permissions:

Android

app/res/xml/plugins.xml

<plugin name="NetworkStatus" value="org.apache.cordova.NetworkManager" />

app/AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Semantic answered 9/4, 2013 at 7:57 Comment(0)
V
3

the API has been changed, and you should be referring to navigator.connection.

network = navigator.connection.type;

Then, see benka's answer

Valvate answered 10/1, 2014 at 10:43 Comment(0)
G
0

I had this problem with the latest version of cordova. I had the network plugin in the config.xml but it was not downloaded into the plugins directory.

This fixed it

cordova plugin add cordova-plugin-network-information cordova build

The repository name has changed in case you are used to:

cordova plugin add org.apache.cordova.network-information

Gebhart answered 23/3, 2016 at 0:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.