Latest Post

Thursday, 23 November 2017

CORDOVA GET SIM INFO USING Cordova-plugin-sim



Cordova-plugin-sim
cordova provide the plugin to get information from the SIM card like the carrier name, mcc, mnc and country code and other system dependent additional info.

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

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

Usage


function onDeviceReady() {
    window.plugins.sim.getSimInfo(successCallback, errorCallback);
}

function successCallback(result) {
    console.log(result);
}

function errorCallback(error) {
    console.log(error);
}

// Android only: check permission
function hasReadPermission() {
    window.plugins.sim.hasReadPermission(successCallback, errorCallback);
}

// Android only: request permission
function requestReadPermission() {
    window.plugins.sim.requestReadPermission(successCallback, errorCallback);

}

The plugin returns a JSON object


{ "CARRIERNAME": "AIRTEL", "COUNTRYCODE": "IN", "MCC": "404", "MNC": "10", "CALLSTATE": 0, "DATAACTIVITY": 4, "NETWORKTYPE": 13, "PHONETYPE": 1, "SIMSTATE": 5, "ISNETWORKROAMING": FALSE, "PHONECOUNT": 2, "ACTIVESUBSCRIPTIONINFOCOUNT": 2, "ACTIVESUBSCRIPTIONINFOCOUNTMAX": 2, "PHONENUMBER": "9632154870", "DEVICEID": "3589600611120202", "DEVICESOFTWAREVERSION": "10", "SIMSERIALNUMBER": "8991101605077898564", "SUBSCRIBERID": "404100507781869", "CARDS": [ { "CARRIERNAME": "AIRTEL", "DISPLAYNAME": "AIRTEL", "COUNTRYCODE": "IN", "MCC": 404, "MNC": 10, "ISNETWORKROAMING": FALSE, "ISDATAROAMING": true, "SIMSLOTINDEX": 0, "PHONENUMBER": "9632154870", "DEVICEID": "3589600611120202", "SIMSERIALNUMBER": "8991101605077898564", "SUBSCRIPTIONID": 2 }, { "CARRIERNAME": "VODAFONE", "DISPLAYNAME": "VODAFONE", "COUNTRYCODE": "IN", "MCC": 404, "MNC": 10, "ISNETWORKROAMING": FALSE, "ISDATAROAMING": true, "SIMSLOTINDEX": 0, "PHONENUMBER": "9875641230", "DEVICEID": "358960061114156", "SIMSERIALNUMBER": "89911016050778180321", "SUBSCRIPTIONID": 4 } ] }

Return object details

Json ValueTypeDescription
carrierName {String} Service Provider Name (SPN)
countryCode {String} ISO country code equivalent for the SIM provider's country code
mcc {String} MCC (mobile country code) of the provider of the SIM
mnc {String} MNC (mobile network code) of the provider of the SIM
callState{Number} call state (cellular) on the device List of Call State values and Descrition
dataActivity {Number} type of activity on a data connection (cellular) List of Data Activity values and Descriptions
networkType {Number} the NETWORK_TYPE_xxxx for current data connection List of Network Type values and Descriptions
phoneType {Number} device phone type. This indicates the type of radio used to transmit voice calls List of Phone Type Values and Descriptions
simState {Number} the state of the device SIM card List of SIM State Values and Descriptions
isNetworkRoaming {Boolean} true if the device is considered roaming on the current network, for GSM purposes
phoneCount {Number} the number of phones available. Returns 0 if none of voice, sms, data is not supported. Returns 1 for Single standby mode (Single SIM functionality). Returns 2 for Dual standby mode (Dual SIM functionality)
activeSubscriptionInfoCount {Number} [READ_PHONE_STATE] the current number of active subscriptions
activeSubscriptionInfoCountMax {Number} [READ_PHONE_STATE] the maximum number of active subscriptions
phoneNumber {String} [READ_PHONE_STATE] - phone number string for line 1, for example, the MSISDN for a GSM phone 1
deviceId {String} [READ_PHONE_STATE] the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones
deviceSoftwareVersion {String} [READ_PHONE_STATE] the software version number for the device, for example, the IMEI/SV for GSM phones
simSerialNumber {String} [READ_PHONE_STATE] the serial number of the SIM, if applicable
subscriberId {String} [READ_PHONE_STATE] the unique subscriber ID, for example, the IMSI for a GSM phone
cards {Array} [READ_PHONE_STATE] List of SIM cards
carrierName {String} the name displayed to the user that identifies Subscription provider name
displayName {String} the name displayed to the user that identifies this subscription
countryCode {String} the ISO country code
mcc {String} MCC (mobile country code) of the provider of the SIM
mnc {String} MNC (mobile network code) of the provider of the SIM
isNetworkRoaming {Boolean} Returns true if the device is considered roaming on the current network for a subscription
isDataRoaming {Boolean} the data roaming state for this subscription
simSlotIndex {Number} the slot index of this Subscription's SIM card
phoneNumber {String} the number of this subscription
deviceId {String} the unique device ID of a subscription, for example, the IMEI for GSM and the MEID for CDMA phones
simSerialNumber {String} ICC ID
subscriptionId {String} Subscription Identifier, this is a device unique number


Complete Example of cordova-plugin-device

Index.html

<div class="item">
        <h2 style="text-align:center">SIM Information</h2>
    </div>
    <div class="item" style="width:100%">
        <p id="SIMInformation" class="item-note" ></p>
       
        <p id="SIMInfo" class="item-note"></p>
    </div>

Index.js
var app = {
    // Application Constructor
    initialize: function () {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function () {
        this.receivedEvent('deviceready');
        hasReadPermission();
    },

    // Update DOM on a Received Event
    receivedEvent: function (id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:none;');

        console.log('Received Event: ' + id);
    }
};
function simInfosuccessCallback(result) {
    var element = document.getElementById('SIMInformation');
    if (result !== undefined && result != null && result != '') {
        var tblHmlt = '<h3>Sim basic Information </h3><table style="width:100%"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';
        tblHmlt += '<tr><td>carrierName</td><td> ' + result.carrierName + '</td></tr>' +
                 '<tr><td>countryCode</td><td> ' + result.countryCode + '</td></tr>' +
                 '<tr><td>mcc</td><td> ' + result.mcc + '</td></tr>' +
                 '<tr><td>mnc</td><td>' + result.mnc + '</td></tr>' +
                 '<tr><td>callState</td><td> ' + result.callState + '</td></tr>' +
                 '<tr><td>dataActivity</td><td> ' + result.dataActivity + '</td></tr>' +
                 '<tr><td>networkType</td><td> ' + result.networkType + '</td></tr>' +
                 '<tr><td>phoneType</td><td> ' + result.phoneType + '</td></tr>' +
                 '<tr><td>simState</td><td> ' + result.simState + '</td></tr>' +
                 '<tr><td>isNetworkRoaming</td><td> ' + result.isNetworkRoaming + '</td></tr>' +
                 '<tr><td>phoneCount</td><td> ' + result.phoneCount + '</td></tr>' +
                 '<tr><td>activeSubscriptionInfoCount</td><td> ' + result.activeSubscriptionInfoCount + '</td></tr>' +
                 '<tr><td>activeSubInfoCountMax</td><td>' + result.activeSubscriptionInfoCountMax + '</td></tr>' +
                 '<tr><td>phoneNumber</td><td> ' + result.phoneNumber + '</td></tr>' +
                 '<tr><td>deviceId</td><td> ' + result.deviceId + '</td></tr>' +
                 '<tr><td>deviceSoftwareVersion</td><td> ' + result.deviceSoftwareVersion + '</td></tr>' +
                 '<tr><td>simSerialNumber</td><td> ' + result.simSerialNumber + '</td></tr>' +
                 '<tr><td>subscriberId</td><td> ' + result.subscriberId + '</td></tr>' +
                  '</tbody></table>';

        element.innerHTML = tblHmlt;
    }

    var elementSimInfo = document.getElementById('SIMInfo');
    if (result.cards.length > 0 && result.phoneCount > 1) {
        var innerHtml = '';
        var totalSim = 1;
        for (var i = 0; i < result.cards.length; i++) {

            var tblHmlt = '<h3>Sim ' + totalSim + ' Information </h3>' + '<br /><table style="width:100%"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';

            tblHmlt += '<tr><td>carrierName</td><td>' + result.cards[i].carrierName + '</td></tr>' +
                                   '<tr><td>displayName</td><td>' + result.cards[i].displayName + '</td></tr>' +
                                   '<tr><td>countryCode</td><td>' + result.cards[i].countryCode + '</td></tr>' +
                                   '<tr><td>mcc</td><td>' + result.cards[i].mcc + '</td></tr>' +
                                   '<tr><td>mnc</td><td>' + result.cards[i].mnc + '</td></tr>' +
                                   '<tr><td>isNetworkRoaming</td><td>' + result.cards[i].isNetworkRoaming + '</td></tr>' +
                                   '<tr><td>isDataRoaming</td><td>' + result.cards[i].isDataRoaming + '</td></tr>' +
                                   '<tr><td>simSlotIndex</td><td>' + result.cards[i].simSlotIndex + '</td></tr>' +
                                   '<tr><td>phoneNumber</td><td>' + result.cards[i].phoneNumber + '</td></tr>' +
                                   '<tr><td>deviceId</td><td>' + result.cards[i].deviceId + '</td></tr>' +
                                   '<tr><td>simSerialNumber</td><td>' + result.cards[i].simSerialNumber + '</td></tr>' +
                                   '<tr><td>subscriptionId</td><td>' + result.cards[i].subscriptionId + '</td></tr>' +
                                    '</tbody></table>';
            totalSim++;
            innerHtml += tblHmlt
        }
        elementSimInfo.innerHTML = innerHtml;
    }
}

function HasPerminionSuccessCallback(result) {

    if (result) {
        window.plugins.sim.getSimInfo(simInfosuccessCallback, errorCallback);
    } else {
        requestReadPermission();
    }
}


function errorCallback(error) {

    var elementSimInfo = document.getElementById('ErrorInfo');
    elementSimInfo.innerHTML = error;

}
function requestSuccessCallback(result) {
    if (result) {
        hasReadPermission();
    }
}
// Android only: check permission
function hasReadPermission() {
    window.plugins.sim.hasReadPermission(HasPerminionSuccessCallback, errorCallback);
}

// Android only: request permission
function requestReadPermission() {
    window.plugins.sim.requestReadPermission(requestSuccessCallback, errorCallback);

}

Output
Fisrt sreen will be show to allow to give the permission to acees the sim info. 







Government Jobs