Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
11 Views

Unable to check Internet Connection

I'm currently trying to check if my app user is or isn't connected to internet.

I've added the plugin Device,Network Information.

This works completely fine in the emulator but when I test in through WIFI on a iPad and a HTC phone the alert does not work.

Code I am using is below.

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

function onDeviceReady() {

alert(intel.xdk.device.connection);

}

 

Tags (2)
0 Kudos
3 Replies
Highlighted
Valued Contributor I
11 Views

Hi,

The api intel.xdk.device is retired, so is not advisable to use it. Here you have more information:

https://software.intel.com/en-us/xdk/docs/intel-xdk-api-cordova-plugin-methods-properties-events

One alternative is 

https://github.com/apache/cordova-plugin-network-information

Regards,

Diego

0 Kudos
Highlighted
Beginner
11 Views

Hi Diego,

I already have the plugin network-information.

I tried to use this code as listed below. However this now no longer works in the emulator or test.

 checkConnection();

            function checkConnection() {
                var networkState = navigator.connection.type;

                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.CELL] = 'Cell generic connection';
                states[Connection.NONE] = 'No network connection';

                alert('Connection type: ' + states[networkState]);
            }

 

0 Kudos
Highlighted
Valued Contributor I
11 Views

It is working form me, both in emulator and in App Preview in an iPhone.

In Emulator in my Mac, the alert says Ethernet connection.

In App Preview, gives wi-fi connection. If I turn off wi-fi, says No network connection (I expect 3G, but no).

What I did is, I an HTML5+Cordova project I added the Network Information plugin from Core Plugins, drop a button and add to the click event your code above.

The plugin is version 1.2.0 and set android permissions to android.permission.ACCESS_NETWORK_STATE

Regards,

Diego

0 Kudos