NativeScript Core

Connectivity

The connectivity module contains connectivity utility methods for observing the connection type and availability.

To control and monitor the Internet connectivity , you need to import the tns-core-modules/connectivity module.

const connectivityModule = require("tns-core-modules/connectivity");
import { connectionType, getConnectionType, startMonitoring, stopMonitoring }from "tns-core-modules/connectivity";

Basics

Getting Connection Type

Using the connectivity module with getConnectionType method, we can get the current Internet connection type.

// result is ConnectionType enumeration (none, wifi or mobile)
const myConnectionType = connectivityModule.getConnectionType();

switch (myConnectionType) {
    case connectivityModule.connectionType.none:
        // Denotes no Internet connection.
        console.log("No connection");
        break;
    case connectivityModule.connectionType.wifi:
        // Denotes a WiFi connection.
        console.log("WiFi connection");
        break;
    case connectivityModule.connectionType.mobile:
        // Denotes a mobile connection, i.e. cellular network or WAN.
        console.log("Mobile connection");
        break;
    case connectivityModule.connectionType.ethernet:
        // Denotes a ethernet connection.
        console.log("Ethernet connection");
        break;
    case connectivityModule.connectionType.bluetooth:
        // Denotes a bluetooth connection.
        console.log("Bluetooth connection");
        break;
    default:
        break;
}
// result is ConnectionType enumeration (none, wifi or mobile)
const myConnectionType = getConnectionType();

switch (myConnectionType) {
    case connectionType.none:
        // Denotes no Internet connection.
        console.log("No connection");
        break;
    case connectionType.wifi:
        // Denotes a WiFi connection.
        console.log("WiFi connection");
        break;
    case connectionType.mobile:
        // Denotes a mobile connection, i.e. cellular network or WAN.
        console.log("Mobile connection");
        break;
    case connectionType.ethernet:
        // Denotes a ethernet connection.
        console.log("Ethernet connection");
        break;
    case connectionType.bluetooth:
        // Denotes a bluetooth connection.
        console.log("Bluetooth connection");
        break;
    default:
        break;
}

Android specifics: On Android to access any connection related information we will need explicit permission from the user. To enable the permission request add the following in app/App_Resources/Android/AndroidManifest/xml

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Monitoring Connection

Monitoring connection type is achieved through startMonitoring method.

connectivityModule.startMonitoring((newConnectionType) => {
    switch (newConnectionType) {
        case connectivityModule.connectionType.none:
            console.log("Connection type changed to none.");
            break;
        case connectivityModule.connectionType.wifi:
            console.log("Connection type changed to WiFi.");
            break;
        case connectivityModule.connectionType.mobile:
            console.log("Connection type changed to mobile.");
            break;
        case connectivityModule.connectionType.ethernet:
            console.log("Connection type changed to ethernet.");
            break;
        case connectivityModule.connectionType.bluetooth:
            console.log("Connection type changed to bluetooth.");
            break;
        default:
            break;
    }
});

// Explicitly stopping the monitoring
connectivityModule.stopMonitoring();
startMonitoring((newConnectionType) => {
    switch (newConnectionType) {
        case connectionType.none:
            console.log("Connection type changed to none.");
            break;
        case connectionType.wifi:
            console.log("Connection type changed to WiFi.");
            break;
        case connectionType.mobile:
            console.log("Connection type changed to mobile.");
            break;
        case connectionType.ethernet:
            console.log("Connection type changed to ethernet.");
            break;
        case connectionType.bluetooth:
            console.log("Connection type changed to bluetooth.");
            break;
        default:
            break;
    }
});

// Explicitly stopping the monitoring
stopMonitoring();

Improve this document

Demo Source