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. 







19 comments:

  1. For some cause the picture just isn’t loading appropriately, is at this time there an issue? ipad template

    ReplyDelete
  2. I simply wanted to thank you a lot more for your amazing website you have developed here. It can be full of useful tips for those who are actually interested in this specific subject, primarily this very post. Your all so sweet in addition to thoughtful of others and reading the blog posts is a great delight in my opinion. And thats a generous present! Dan and I usually have enjoyment making use of your recommendations in what we need to do in the near future. Our checklist is a distance long and tips will certainly be put to excellent use. ipad sketch

    ReplyDelete
  3. A person essentially help to make seriously articles I would state. This is the first time I frequented your website page and thus far? I surprised with the research you made to make this particular publish incredible. Fantastic job! apple tablet mockup

    ReplyDelete
  4. Kinds of Engines vacation packages ‘re affordable, of benefit nevertheless, you pick up every single single single ability required by specific bargain. Search Engine Optimization apple desktop mockup

    ReplyDelete
  5. This is such a great resource that you’re offering and you provide out at no cost. I appreciate seeing sites that realize the worth of offering a perfect useful resource totally free. I genuinely loved reading your submit. android mockups

    ReplyDelete
  6. Immigration… [...]the time to read or visit the content or sites we have linked to below the[...]… android mock up

    ReplyDelete
  7. awesome issues here. I’m very satisfied to peer your post. Thanks a lot and i am having a look forward to touch you. Will you kindly drop me a e-mail? app store mockup

    ReplyDelete
  8. I thought it was going to be some boring old publish, but it really compensated for my time. I will publish a link to this page on my blog. I am confident my visitors will discover that quite useful app screenshot

    ReplyDelete
  9. I likewise conceive thus, perfectly written post! . imac mockup png

    ReplyDelete
  10. And Im running from a standard users account with strict limitations, which I think may be the limiting factor, but Im running the cmd as the system I am currently working on. apple watch mockup psd

    ReplyDelete
  11. I think this is among the most vital info for me. And i’m satisfied studying your article. However should commentary on few normal things, The website taste is ideal, the articles is in reality excellent . Excellent activity, cheers. frontend services

    ReplyDelete
  12. Very informative and great complex body part of articles , now that’s user pleasant (:. front end service

    ReplyDelete
  13. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. It's a GPL Plugins selling website

    ReplyDelete
  14. I’m not sure where you’re getting your info, but great topic. I needs to spend some time learning much more or understanding more. los angeles web agency

    ReplyDelete
  15. ive begun to visit this cool site a couple of times now and i have to tell you that i find it quite nice actually. itll be nice to read more in the future! =p web design agencies los angeles

    ReplyDelete
  16. I really got into this article. I found it to be interesting and loaded with unique points of interest. I like to read material that makes me think. Thank you for writing this great content. web design agency

    ReplyDelete
  17. I would like to say that this blog really convinced me to do it! Thanks, very good post. encrypted sim card

    ReplyDelete
  18. There are many dissertation internet sites on-line after you uncover unsurprisingly identified in your website page. MVNO platform

    ReplyDelete