NOTE! You are browsing legacy documentation. For latest visit docs.nativescript.org.

NativeScript Angular

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

import { Component, OnInit, OnDestroy } from "@angular/core";
import {
    getConnectionType,
    startMonitoring,
    stopMonitoring
} from "tns-core-modules/connectivity";
import * as connectivityModule from "tns-core-modules/connectivity";

@Component({
    moduleId: module.id,
    templateUrl: "./usage.component.html"
})

export class UsageComponent implements OnInit, OnDestroy {
    connectionType: string;

    constructor() {
        let type = getConnectionType();

        switch (type) {
            case connectivityModule.connectionType.none:
                this.connectionType = "None";
                break;
            case connectivityModule.connectionType.wifi:
                this.connectionType = "Wi-Fi";
                break;
            case connectivityModule.connectionType.mobile:
                this.connectionType = "Mobile";
                break;
            case connectivityModule.connectionType.bluetooth:
                this.connectionType = "Bluetooth";
                break;
            default:
                break;
        }
    }

    ngOnInit() {
        startMonitoring((type) => {
            switch (type) {
                case connectivityModule.connectionType.none:
                    this.connectionType = "None";
                    console.log("Connection type changed to none.");
                    break;
                case connectivityModule.connectionType.wifi:
                    this.connectionType = "Wi-Fi";
                    console.log("Connection type changed to WiFi.");
                    break;
                case connectivityModule.connectionType.mobile:
                    this.connectionType = "Mobile";
                    console.log("Connection type changed to mobile.");
                    break;
                case connectivityModule.connectionType.bluetooth:
                    this.connectionType = "Bluetooth";
                    console.log("Connection type changed to Bluetooth.");
                    break;
                default:
                    break;
            }
        });
    }

    ngOnDestroy() {
        // Stoping the connection monitoring
        stopMonitoring();
    }
}

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
connectionType Enum

Native Component

Android iOS
CONNECTIVITY_SERVICE (android.content.Context) SCNetworkReachability