12

my first time experimenting with Apache Cordova 3.0.

downloaded lib, unziped cordova-android and cordova-js and created a project:

./create ~/Documents/andriod-projects/HelloWorld com.x.HelloWorld HelloWorld
- OK

res/xml/config.xml

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

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" />

on index.js device ready:

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {

    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("Network: "+states[networkState]);
}

when I emulate the project on my andriod I got in LogCat Error:Connection is not defined:

enter image description here

what I am missing? I have to attach a .js in order to Connection be declared?

Leo
  • 1,495
  • 23
  • 41
panchicore
  • 11,451
  • 12
  • 74
  • 100

14 Answers14

12

Uncaught Refference error: Connection is not defined

is related to lack of a "Connection" object, which based on my experience with corodva 3.1.0 does not become available, even after a delay as benka suggested. This particular issue can be solved by using the constants of the navigator.connection object as below:

var states = {};
states[navigator.connection.UNKNOWN]  = 'Unknown connection';
states[navigator.connection.ETHERNET] = 'Ethernet connection';
states[navigator.connection.WIFI]     = 'WiFi connection';
states[navigator.connection.CELL_2G]  = 'Cell 2G connection';
states[navigator.connection.CELL_3G]  = 'Cell 3G connection';
states[navigator.connection.CELL_4G]  = 'Cell 4G connection';
states[navigator.connection.CELL]     = 'Cell generic connection';
states[navigator.connection.NONE]     = 'No network connection';

unfortunately in my case this was only the beginning of issues with network status on android as

navigator.connection.type

would always return 0 which is Unknown connection. Both on the android emulator and a device. A workaround which works for me is to call the plugin class directly:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    var conn = checkConnection();
    alert("Connection:"+conn);


}
function checkConnection(){
        var networkState;
        var test = cordova.exec(
                function(winParam) {networkState = winParam;},
                function(error) {alert("Network Manager error: "+error);},
                "NetworkStatus",
                "getConnectionInfo",
                []
        );
        return networkState;
}

this code has an ugly networkState assignment inside a function that could potentially be executed asynchronously after the checkConnection return statement, but as the native code returns PluginResult inside the execute function - this works. The networkState value returned does not match the navigator.connection. constants like:

navigator.connection.WIFI

You can see the values returned in the plugins source code here: https://github.com/apache/cordova-plugin-network-information/blob/master/src/android/NetworkManager.java

dadasign
  • 408
  • 4
  • 7
7

I got the same issue with Phonegap 3.0 on Android 4.2.2 Api 17.

Tried removing and reinstalling the Connection plugin trying both commands: Cordova or Phonegap local but didn't work.

What I have noticed in the logs is the following right after the ERROR message:

10-11 14:31:40.360: E/Web Console(): Uncaught ReferenceError: Connection is not defined
10-11 14:31:40.380: D/CordovaNetworkManager(): Connection Type: wifi

So I was thinking that it actually looks like it was an async callback after successfully initializing Connection.type from CordovaNetworkManager() however it shouldn't be.

So I tried the following:

var networkState = navigator.connection.type;

setTimeout(function(){
    networkState = navigator.connection.type;
    alert('networkState = '+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.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

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

I know it looks a bit stupid but works every time. What it does is first calls navigator.connection.type and then runs the whole function 500ms later giving time to CordovaNetworkManager to initialize the connection.type.

benka
  • 4,732
  • 35
  • 47
  • 58
  • thank you. after searching and searching, this is the ONLY solution that reliably gives me an accurate network status. – KC Coder May 26 '16 at 15:29
6

I had the same exact problem, but was able to fix it. Running the below commands as per the Phonegap Connection docs doesn't seem to work:

$ cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git
$ cordova plugin rm org.apache.cordova.core.network-information

Instead, I had to use:

$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git

Once I did that, it worked.

Julian
  • 167
  • 6
  • I don't have phonegap. – Leo Oct 01 '13 at 14:28
  • Does this basically mean the official plugin is broken? Seems a pretty fundamental problem that it's not registering the Connection constants object to the window. – Ade Sep 10 '16 at 16:29
5

If you have included the plugin and are having the same problem it could be the order in which you included the plugin.

In order for the plugin to work I had to include the plugin after having added the platform.

$ cordova create

$ cordova platform add android 

$ cordova plugin add org.apache.cordova.network-information
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
Leo
  • 1,495
  • 23
  • 41
4

I resolved by:

$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git
Regis
  • 147
  • 2
  • 15
1

I'm using cordova 3.4.1 adding the plugin org.apache.cordova.network-information it fix my problem, try this:

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

I did not change any part of the code, as the help says:

https://github.com/apache/cordova-plugin-network-information/blob/master/doc/index.md

0

I am playing with the same thing and I think you need to install the features using this page by the use of cordova CLI.

Mardox
  • 11
  • 4
0

for me adding the plugin was not enough and didn't solve my issue...

Then I did in the CLI after I added the plugin

$ cordova build

and everything worked perfectly ! Make sure your /www file has the updated files because it will be used to erase the platforms/xxx/www files

Bruno
  • 196
  • 1
  • 7
0

I face the same problem, searching about this issue I found this question. I try many ideas, but the one did works was: 1- uninstall the network-information plugin 2- in the CMD -> cordova build 3- Install the plugin again 4- Build again.

After this The connection example works fine.

aolba
  • 1
  • 3
0

Much in the same time at the end was that no load the js plugins that referenced.          cordova_plugins.js

it may happen that your project is not in the correct folder ... but in any case you can copy directly to a folder known for.

         assets / www / plugins / org.apache.cordova.network-information

      network.js "

      Connection.js "

and loads in your html

  • The sentence structure here is making this nearly unintelligible. Can you please restructure? – Lizz Apr 03 '14 at 22:07
0

For me it was because I was calling checkConnection() inside $(function(){}). It should be called by

document.addEventListener("deviceready", function(){
    checkConnection();
});
Sergey Dirin
  • 435
  • 6
  • 12
0

Now days in cordova 3.5 it seems

<access origin="*" />

is the correct CORS config.xml entry.

Note clarification on it being "ORIGIN" not "uri" and "subdomains" as it was before I believe.

MistereeDevlord
  • 876
  • 7
  • 10
0

I had the same problem with Cordova 3.5.0: navigator.connection.type returned 0 all the time and navigator.onLine was true. It was frustrating.

Then I found that the problem is only on my phone (with Android 2.3) and it works on emulator.

I guess that the problem can relate with Android version too.

Fenix
  • 2,271
  • 1
  • 16
  • 24
0

I had the same problem with PhoneGap 3.5.0:navigator.connection.typereturned me0(unknown connection) andnavigator.onLine(from HTML5) wastrue` all the time.

The problem was caused by the phone (probably I have too old Android version on my phone) because it works with emulator and with tablet (where is Android 4.2) correctly :-(

Fenix
  • 2,271
  • 1
  • 16
  • 24