Advertisement

Latest Post

Thursday, 23 November 2017

Cordova get device info using Cordova-plugin-device


Cordova-plugin-device
Cordova provide device plugin (Cordova-plugin-device) to get the device's hardware and software details. Cordova device plugin defines a global device object. It describes the hardware and software of the device.

Installation
You can install Cordova device plugin using below command
cordova plugin add cordova-plugin-device

Uninstallation
If you don’t want to use cordova device plugin , you can remove this plugin from the project by using below command
cordova plugin remove cordova-plugin-device

Properties
Following are properties of the device object that we can be used to expose the information of hardware and software of device.
Properties
Description
device.cordova 
Get the version of Cordova running on the device.
device.model
It returns the name of the device's model or product.
device.platform 
Get the device's operating system name.
device.uuid
Get the device's Universally Unique Identifier (UUID).
device.version
Get the operating system version
device.manufacturer 
Get the device's manufacturer.
device.isVirtual  
whether the device is running on a simulator.
device.serial    
Get the device hardware serial number


Complete Example of cordova-plugin-device

Index.html

    <button type="button" id="btndevice">Get Device Info</button>
    <br />
        <div class="item" id="divDeviceInfo" style="display:none">
            <h2 style="text-align:center">Device Information</h2>
        </div>
    <div class="item" id="DeviceInformation" style="width:100%">
      
    </div>

Index.js

var app = {
    // Application Constructor
    initialize: function () {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },
    onDeviceReady: function () {
     
        document.getElementById("btndevice").addEventListener("click", GetDeviceInfo);
     
    }
};

function GetDeviceInfo() {
    var listeningElement = document.getElementById('divDeviceInfo');
    listeningElement.setAttribute('style', 'display:none;');
    
    var tblHmlt = '<table style="width:100%"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';
    tblHmlt += '<tr><td>Device Name</td><td>' + device.name + '</td></tr>' +
                  '<tr><td>Device PhoneGap</td><td>' + device.phonegap + '</td></tr>' +
                  '<tr><td>Device Platform</td><td>' + device.platform + '</td></tr>' +
                   '<tr><td>Device UUID</td><td>' + device.uuid + '</td></tr>' +
                    '<tr><td>Device model</td><td>' + device.model + '</td></tr>' +
                     '<tr><td>Device serial</td><td>' + device.serial + '</td></tr>' +
                  '<tr><td>Device cordova</td><td>' + device.cordova + '</td></tr>' +
                  '<tr><td>Device Version</td><td>' + device.version + '</td></tr>' +
                    '</tbody></table>';

    var element = document.getElementById('DeviceInformation');
    element.innerHTML = tblHmlt;
   
    listeningElement.setAttribute('style', 'display:block;');
}

app.initialize();


Output



Government Jobs