Checking for a network
When you start working with your mobile devices, you can't always take the network's presence for granted. The signal may be nonexistent or it may be very weak, or the user might switch from cellular network to WiFi or back again. But now the PhoneGap API includes a connection Object, which gives you access to the device's cellular and WiFi connection information.
Determining the Connection Type
If you want to find out the connection information of your Windows Phone using PhoneGap you can use the connection object. An example:
function checkConnection()
{
var networkState = navigator.network.connection.type;
//return a specific state
}
Here the navigator.network.connection.type - lets the developers know about the current device's connection states and in simple terms lets you know the type of connection that your Windows Phone is using.
The connection.type function will return one of a possible list of connection types available:
- Connection.UNKNOWN
- Connection.ETHERNET
- ConnectionCELL_2G
- ConnectionCELL_3G
- ConnectionCELL_4G
- ConnectionNONE
It's a good idea to dress up these messages by providing users with a customer's information.
For example
function checkConnection()
{
var networkState = navigator.network.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.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
To determine what kind of connection can be made, we will be using connection.type
. For example, if the connection type were NONE, then the alert box would contain the message No network connection.
Checking Network Availability
In this exercise you will create a simple script that checks for network connectivity. Enter the following code:
<!DOCTYPE html>
<html>
<head>
<title>navigator.network.connection.type Example</title>
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady()
{
checkConnection();
}
function checkConnection() {
var networkState = navigator.network.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.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
}
</script>
</head>
<body>
<p>A dialog box will report the network state.</p>
</body>
</html>
Supported Platforms
- iOS
- Android
- BlackBerry WebWorks (OS 5.0 and higher)
Resources
Here are some useful related resources.
Working With JavaScript, CSS And HTML in PhoneGap.
How to Get Device Information Using PhoneGap.
Working with Accelerometer in PhoneGap.
How to find Longitude & Latitude of Geolocation in PhoneGap.