munder
munder

Reputation: 137

Phonegap/Cordova 3 Android: Connection is not defined

I've tried the setTimeout solution recommended in this post, but logcat still reports:

Uncaught ReferenceError: Connection is not defined

I have checked with "phonegap plugin list" that the plugin is indeed installed. config.xml includes:

<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>

and my manifest includes:

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

(sorry I don't seem to be able to format the above correctly) but I continue to get "Connection is not defined". What am I missing?

Upvotes: 2

Views: 2586

Answers (3)

Yann Stoneman
Yann Stoneman

Reputation: 1208

I got rid of the "Connection is not defined" reference error by defining Connection.

Cordova says that

"The connection object, exposed via navigator.connection, provides information about the device's cellular and wifi connection."

Cordova gave me a quick example of how to use cordova-plugin-network-information:

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]);
}

But when I ran it as onClick event, I got the "Connection is not defined" reference error.

So I added let Connection = navigator.connection; inside the function and then it worked:

function checkConnection() {
    let Connection = navigator.connection;

    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]);
}

Upvotes: 2

Marci Blum
Marci Blum

Reputation: 23

For me the solution was to call it after "deviceready" fired. Before it was undefined. I guess that's why the solution with timeout works, the device ready gets called while we are waiting.

Upvotes: 0

QuickFix
QuickFix

Reputation: 11721

From phonegap doc:

Accessing the Feature

As of version 3.0, Cordova implements device-level APIs as plugins. Use the CLI's plugin command, described in The Command-Line Interface, to add or remove this feature for a project:

$ cordova plugin add org.apache.cordova.network-information
or
$ phonegap plugin add org.apache.cordova.network-information

And don't forget to rebuild the project after you add the plugin.

Upvotes: 5

Related Questions