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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…