NativeScript Core

Connectivity

The connectivity module provides a common abstraction of the functionality responsible for receiving information about the connection type and availability of the network.

Usage

const connectivityModule = require("tns-core-modules/connectivity");


exports.onNavigatedTo = function(args) {
    const page = args.object;
    let connectionTypeString;

    const type = connectivityModule.getConnectionType();

    switch (type) {
        case connectivityModule.connectionType.none:
            console.log("No connection");
            connectionTypeString = "No Internet connectivity!";
            break;
        case connectivityModule.connectionType.wifi:
            console.log("WiFi connection");
            connectionTypeString = "WiFI connectivity!";
            break;
        case connectivityModule.connectionType.mobile:
            console.log("Mobile connection");
            connectionTypeString = "Mobile connectivity!";
            break;
        case connectivityModule.connectionType.ethernet:
            console.log("Ethernet connection");
            connectionTypeString = "Ethernet connectivity!";
            break;
        case connectivityModule.connectionType.bluetooth:
            console.log("Bluetooth connection");
            connectionTypeString = "Bluetooth connectivity!";
            break;
        default:
            break;
    }

    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;
        }
    });

    // Stoping the connection monitoring
    connectivityModule.stopMonitoring();

    page.bindingContext = { connectionType: connectionTypeString };
};
import { connectionType, getConnectionType, startMonitoring, stopMonitoring }from "tns-core-modules/connectivity";

export function onNavigatedTo(args) {
    const page = args.object;
    let connectionTypeString;

    const type = getConnectionType();

    switch (type) {
        case connectionType.none:
            console.log("No connection");
            connectionTypeString = "No Internet connectivity!";
            break;
        case connectionType.wifi:
            console.log("WiFi connection");
            connectionTypeString = "WiFI connectivity!";
            break;
        case connectionType.mobile:
            console.log("Mobile connection");
            connectionTypeString = "Mobile connectivity!";
            break;
        case connectionType.ethernet:
            console.log("Ethernet connection");
            connectionTypeString = "Ethernet connectivity!";
            break;
        case connectionType.bluetooth:
            console.log("Bluetooth connection");
            connectionTypeString = "Bluetooth connectivity!";
            break;
        default:
            break;
    }

    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;
        }
    });

    // Stoping the connection monitoring
    stopMonitoring();

    page.bindingContext = { connectionType: connectionTypeString };
}

Improve this document

Demo Source


Methods

Name Type Description
getConnectionType number Gets the type of connection. Returns a value from the connectivityModule.connectionType enumeration. To use this method on Android you need to have the android.permission.ACCESS_NETWORK_STATE permission added to the AndroidManifest.xml file.
startMonitoring(connectionTypeChangedCallback: function) void Starts monitoring the connection type.
stopMonitoring void Stops monitoring the connection type.

API References

Name Type
tns-core-modules/connectivity Module
connectionTypoe Enum

Native Component

Android iOS
CONNECTIVITY_SERVICE (android.content.Context) SCNetworkReachability