📺 "One project. One language. Three apps." Learn more in our upcoming code sharing webinar!

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

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;
    // Bluetooth functionality in master branch (to be released with 5.0.0)
    // case connectionType.bluetooth:
    //     // Denotes a ethernet 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;
    // Bluetooth functionality in master branch (to be released with 5.0.0)
    // case connectionType.bluetooth:
    //     // Denotes a ethernet 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 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;
        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:
        //     // feature coming in NativeScript 5.0.0
        //     console.log("Connection type changed to bluetooth.");
        //     break;
        default:
            break;
    }
});

// Explicitly stopping the monitoring
stopMonitoring();

Improve this document

Demo Source