Network Information
You can access information about the connection (such as cellular, Wi-Fi, or Ethernet) by reading the navigator.connection.type
property. It is a textual representation of the current network status. Each time a connection is established or closed, the type changes.
The Network Information API is mandatory for Tizen Mobile, Wearable, and TV profiles, which means that it is supported on all mobile, wearable, and TV devices. All mandatory APIs are supported on the Tizen emulators.
The main features of the Network Information API include:
-
Getting the connection status
You can compare the
navigator.connection.type
value with one of the values in theConnection
global dictionary to check the current connection status. -
Monitoring network status changes
To monitor the changes in the network status, you can use the
document.addEventListener()
method to register the required callbacks.
Prerequisites
To enable your application to use the network information functionality:
-
To perform any Cordova-related operations, you must wait until Cordova is fully set up (the
deviceready
event occurs):document.addEventListener('deviceready', onDeviceReady, false); function onDeviceReady() { console.log('Cordova features now available'); }
-
To use the Network Information API (in mobile, wearable, and TV applications), the application has to request permission by adding the following privilege to the
config.xml
file:<tizen:privilege name="http://tizen.org/privilege/telephony"/>
Checking the Connection Status
The following table lists the available connection types in the Connection
global dictionary, which you can use to determine the current connection status.
Table: Connection types
Property | Value description |
---|---|
Connection.UNKNOWN |
Connected, unknown connection |
Connection.ETHERNET |
Ethernet connection |
Connection.WIFI |
Wi-Fi connection |
Connection.CELL_2G |
Cellular 2G connection |
Connection.CELL_3G |
Cellular 3G connection |
Connection.CELL_4G |
Cellular 4G connection |
Connection.CELL |
Cellular generic connection |
Connection.NONE |
Not connected |
To determine whether the device is connected to a Wi-Fi network:
-
Place a
div
element with theid="wifi-indicator"
attribute somewhere in the application HTML for text output:<div id="wifi-indicator"> </div>
-
Get the connection type.
It is a case-sensitive string in the
navigator.connection
object.var state = navigator.connection.type;
-
Compare the connection type to one of the defined values in the
Connection
global dictionary:if (state == Connection.WIFI) { document.querySelector('#wifi-indicator').textContent = 'Connected to WiFi'; } else { document.querySelector('#wifi-indicator').textContent = 'Not connected to WiFi'; }
The above code fills the div
element with text, based on whether the device is connected to a Wi-Fi network.
Handling Network-related Events
Manage the situations where the device connects to and disconnects from a network:
-
Register event handlers after Cordova is set up. The most convenient way is to use the
deviceready
event callback.document.addEventListener('deviceready', register); function register() { document.addEventListener('online', went_online); document.addEventListener('offline', went_offline); }
-
Define the handlers:
function went_online() { console.log('Went online'); } function went_offline() { console.log('Went offline'); }
The
online
event fires whenconnection.type
changes fromConnection.NONE
to any other value. Similarly, theoffline
event fires whenconnection.type
becomesConnection.NONE
.
Related Information
- Dependencies
- Tizen 3.0 and Higher for Mobile
- Tizen 3.0 and Higher for Wearable
- Tizen 3.0 and Higher for TV