Home

Awesome

cordova-plugin-sim

npm Code Climate Platform Donate

This is a cordova plugin to get data from the SIM card like the carrier name, mcc, mnc and country code and other system dependent additional info.

Installation

cordova plugin add cordova-plugin-sim

Supported Platforms

Usage

document.addEventListener("deviceready", onDeviceReady, false);

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. Return values:

Field carrierName may remain empty, dependent on the mobile provider.

On Windows Phone access to countryCode, MCC and MNC is not made provided (returns empty string).

You can extract country and carrier data from MCC and MNC codes, read further on Wikipedia and ITU-T.

You can find the name of mobile provider using mcc-mnc-list npm package.

Ionic 2 Usage

import { Sim } from 'ionic-native';

Sim.getSimInfo().then(
  (info) => console.log('Sim info: ', info),
  (err) => console.log('Unable to get sim info: ', err)
);

Sim.hasReadPermission().then(
  (info) => console.log('Has permission:', info)
);

Sim.requestReadPermission().then(
  () => console.log('Permission granted'),
  () => console.log('Permission denied')
);

Required: ionic-native v2.2.13

See Ionic Native documentation.

Android Quirks

Under the hood

This plugin uses two different Android APIs to receive SIM data:

Since Android 6 (API level 23) a few methods of TelephonyManager require permission READ_PHONE_STATE.

All methods of SubscriptionManager require permission READ_PHONE_STATE.

SubscriptionManager is able to access multiple SIM data. The return object of this cordova plugin provides the details of the available sim cards in an array (cards).

Return object

<sup>1)</sup> Notice: the content of phoneNumber is unreliable (see this, this, and this article). Sometimes phoneNumber is only an empty string.

Android Emulator results

{
  "carrierName": "Android",
  "countryCode": "us",
  "mcc": "310",
  "mnc": "260",
  "phoneNumber": "15555215554",
  "deviceId": "0000000000000000",
  "simSerialNumber": "89014103211118510720",
  "subscriberId": "310260000000000",
  "callState": 0,
  "dataActivity": 0,
  "networkType": 3,
  "phoneType": 1,
  "simState": 5,
  "isNetworkRoaming": false
}

List of Call State Codes and Meanings

CodeConstantMeaning
0CALL_STATE_IDLENo activity
1CALL_STATE_RINGINGRinging. A new call arrived and is ringing or waiting. In the latter case, another call is already active.
2CALL_STATE_OFFHOOKOff-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.

List of Data Activity Codes and Meanings

CodeConstantMeaning
0DATA_ACTIVITY_NONENo traffic.
1DATA_ACTIVITY_INCurrently receiving IP PPP traffic.
2DATA_ACTIVITY_OUTCurrently sending IP PPP traffic.
3DATA_ACTIVITY_INOUTCurrently both sending and receiving IP PPP traffic.
4DATA_ACTIVITY_DORMANTData connection is active, but physical link is down

List of Network Type Codes and Meanings

CodeConstantMeaning
0NETWORK_TYPE_UNKNOWNunknown
1NETWORK_TYPE_GPRSGPRS
2NETWORK_TYPE_EDGEEDGE
3NETWORK_TYPE_UMTSUMTS
4NETWORK_TYPE_CDMACDMA: Either IS95A or IS95B
5NETWORK_TYPE_EVDO_0EVDO revision 0
6NETWORK_TYPE_EVDO_AEVDO revision A
7NETWORK_TYPE_1xRTT1xRTT
8NETWORK_TYPE_HSDPAHSDPA
9NETWORK_TYPE_HSUPAHSUPA
10NETWORK_TYPE_HSPAHSPA
11NETWORK_TYPE_IDENiDen
12NETWORK_TYPE_EVDO_BEVDO revision B
13NETWORK_TYPE_LTELTE
14NETWORK_TYPE_EHRPDeHRPD
15NETWORK_TYPE_HSPAPHSPA+
16NETWORK_TYPE_GSMGSM
17NETWORK_TYPE_TD_SCDMATD-SCDMA
18NETWORK_TYPE_IWLANIWLAN

List of Phone Type Codes and Meanings

CodeConstantMeaning
0PHONE_TYPE_NONEnone
1PHONE_TYPE_GSMGSM
2PHONE_TYPE_CDMACDMA
3PHONE_TYPE_SIPSIP

List of SIM State Codes and Meanings

CodeConstantMeaning
0SIM_STATE_UNKNOWNUnknown. Signifies that the SIM is in transition between states. For example, when the user inputs the SIM pin under PIN_REQUIRED state, a query for sim status returns this state before turning to SIM_STATE_READY.
1SIM_STATE_ABSENTNo SIM card is available in the device
2SIM_STATE_PIN_REQUIREDLocked: requires the user's SIM PIN to unlock
3SIM_STATE_PUK_REQUIREDLocked: requires the user's SIM PUK to unlock
4SIM_STATE_NETWORK_LOCKEDLocked: requires a network PIN to unlock
5SIM_STATE_READYReady

Android 6.0 Permissions

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

If the device is running Android 6.0 or higher, and your app's target SDK is 23 or higher: The app has to list the permissions in the manifest, and it must request each dangerous permission it needs while the app is running. The user can grant or deny each permission, and the app can continue to run with limited capabilities even if the user denies a permission request.

Note: Beginning with Android 6.0 (API level 23), users can revoke permissions from any app at any time, even if the app targets a lower API level. You should test your app to verify that it behaves properly when it's missing a needed permission, regardless of what API level your app targets.

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

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

This plugin needs READ_PHONE_STATE permission for getting the following values:

Wiki: How to test permissions

Build conflict with phonegap-facebook-plugin

Android SDK uses Gradle build tool to create Android builds. phonegap-facebook-plugin does not implement android-support-v4 dependency the recommended way, Gradle is not able to resolve the dependencies. The build process will fail.

I suggest to use another plugin for facebook usage:

iOS Quirks

Additional return value:

iOS Emulator results

{
  "carrierName": "",
  "countryCode": "",
  "mcc": "",
  "mnc": "",
  "allowsVOIP": false
}

Windows Phone Quirks

Additional return values:

Author

Peter Bakondy

LICENSE

cordova-plugin-sim is licensed under the MIT Open Source license. For more information, see the LICENSE file in this repository.