cordova 3.0: Android: Connection is not defined
Asked Answered
B

14

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?

Banket answered 21/7, 2013 at 0:17 Comment(0)
T
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

Teleview answered 10/11, 2013 at 20:37 Comment(4)
They have SUCH frustratingly piss-poor documentation. This was the only way it worked for me, thanks very much mate!Macedoine
Only way that works, yes you heard it right. But they will probably fix it. Thanks!Calceolaria
If there is no internet connection. Then what is the error message coming? i got "none"Kaela
Add <script type="text/javascript" charset="utf-8" src="cordova.js"></script> in <head> tagObtuse
C
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.

Commandment answered 11/10, 2013 at 13:51 Comment(1)
thank you. after searching and searching, this is the ONLY solution that reliably gives me an accurate network status.Demetria
E
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.

Enlightenment answered 9/8, 2013 at 5:34 Comment(2)
I don't have phonegap.Aperiodic
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.Unship
A
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
Aperiodic answered 2/10, 2013 at 21:29 Comment(0)
B
4

I resolved by:

$ phonegap local plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-network-information.git
Burch answered 3/10, 2013 at 20:53 Comment(0)
S
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

Sokoto answered 19/5, 2014 at 12:35 Comment(0)
M
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.

Muliebrity answered 21/7, 2013 at 4:40 Comment(0)
D
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

Doe answered 3/1, 2014 at 15:22 Comment(1)
Just warned you about it in my answer :/Doe
C
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.

Centralization answered 6/2, 2014 at 6:0 Comment(0)
D
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

Divagate answered 3/4, 2014 at 21:48 Comment(1)
The sentence structure here is making this nearly unintelligible. Can you please restructure?Kissie
P
0

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

document.addEventListener("deviceready", function(){
    checkConnection();
});
Plat answered 18/7, 2014 at 6:39 Comment(0)
S
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.

Socrates answered 5/9, 2014 at 23:35 Comment(0)
G
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.

Goldarn answered 27/2, 2015 at 8:39 Comment(0)
G
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 :-(

Goldarn answered 10/3, 2015 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.