13

How can I get the ICCID number of the phone?

I looked in the TelephonyManager class, but didn't found there a way to get the ICCID number, there is only a method which tells if the ICC card is present.

Alexei
  • 1,028
  • 1
  • 11
  • 17
  • Point of clarification: a _phone_ **does not have** an ICCID. A _sim card_ **does have** an ICCID, and a phone may have zero, one or more sim cards inserted into it at any point in time (depending on the phone model). – jaimet Apr 19 '23 at 12:46

3 Answers3

20

I believe getSimSerialNumber() will get iccid.

UPDATE for Android 5.1 (credit Erick M Sprengel)

"Multiple SIM Card Support" was added for Android 5.1 (API 22). Instead of using getSimSerialNumber, you can use SubscriptionManager.

SubscriptionManager sm = SubscriptionManager.from(mContext);

// it returns a list with a SubscriptionInfo instance for each simcard
// there is other methods to retrieve SubscriptionInfos (see [2])
List<SubscriptionInfo> sis = sm.getActiveSubscriptionInfoList();

// getting first SubscriptionInfo
SubscriptionInfo si = sis.get(0);

// getting iccId
String iccId = si.getIccId();

You will need the permission: (credit Damian)

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Reed
  • 14,703
  • 8
  • 66
  • 110
  • iccid is of 20 digits according to http://en.wikipedia.org/wiki/Subscriber_identity_module but this method returns a 19 digit number – Siddhesh Sep 26 '12 at 14:34
  • To back that up, my ATT SIM has 20 digits. It's possible that it is different for different carriers. I have tested the function many times and it always functions to my needs, though I haven't actually checked to see how many characters it returns. – Reed Sep 28 '12 at 21:16
  • 4
    My understanding is SIM serial number and ICCID are same things. – Lavekush Agrawal Jul 23 '15 at 10:04
  • What will happen when the device has multiple sims inserted? @Reed – Akshay Kumar S Apr 18 '19 at 04:29
  • @Reed Does ICCID number depend on the type of SIM like Airtel, Idea, Vodaphone company vendor? Please help me on it – Ravindra Kushwaha Oct 22 '20 at 11:34
  • @RavindraKushwaha, I haven't done mobile dev or tinkered with phones in so long that I don't personally know, but this looks like it might help: his should help: https://www.hologram.io/blog/whats-an-iccid-number-and-why-does-it-matter-for-cellular-iot – Reed Oct 22 '20 at 11:56
  • 1
    @Reed ... I have checked the above code with different SIM provider,The ID changes as the SIM change in the device.. – Ravindra Kushwaha Oct 23 '20 at 06:04
  • Oh, I maybe misunderstood. Yeah, the ICCID is the ID of the sim card itself. From [that link](https://www.hologram.io/blog/whats-an-iccid-number-and-why-does-it-matter-for-cellular-iot), it looked like some of the characters identify the carrier. – Reed Oct 27 '20 at 21:11
  • ok,i got iccic length is 12 .but right iccid length is 20 ? – Bingchean Apr 23 '21 at 06:23
  • @Bingchean, https://www.imei.info/faq-what-is-ICCID/ says the "Account Id" is 12 digits. Maybe its returning the account id without the country & character information? I don't know though. I wonder if the phone's hardware settings show you something different? Or if the iccid printed on the physical card is only 12 digits? Maybe its carrier dependent? – Reed Apr 27 '21 at 17:09
7

It's best not to use this, as this is one of the identifiers that recently got restricted, just as was done recently on Android 10.

On Android 11 (API 30), even if you don't target it, you will most likely get just an empty string.

The recommendation of what to use is getSubscriptionId instead (requires READ_PHONE_STATE permission).

The reason:

Returns the ICC ID. Starting with API level 30, returns the ICC ID if the calling app has been granted the READ_PRIVILEGED_PHONE_STATE permission, has carrier privileges (see TelephonyManager#hasCarrierPrivileges), or is a device owner or profile owner that has been granted the READ_PHONE_STATE permission. The profile owner is an app that owns a managed profile on the device; for more details see Work profiles. Profile owner access is deprecated and will be removed in a future release.

so it returns "the ICC ID, or an empty string if one of these requirements is not met" . For third party apps, this means you will probably always get an empty string.

If you don't meet these (and you usually don't), you can use it.

final SubscriptionManager subscriptionManager = SubscriptionManager.from(context);
final List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();

And then for each of them, you can use subscriptionInfo.getIccId() .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Does ICCID number depend on the type of SIM like Airtel, Idea, Vodaphone company vendor? Please help me on it – Ravindra Kushwaha Oct 22 '20 at 11:38
  • @RavindraKushwaha I don't know, but as I wrote, it's recommended not to use it. – android developer Oct 22 '20 at 23:42
  • Thanks for the suggestion!!! I have one query please help me out from it that How can uniquely identify any device ? Is there any way or trick to do it? Thanks again – Ravindra Kushwaha Oct 23 '20 at 06:10
  • @RavindraKushwaha Sadly Google decided to restrict it more and more over Android versions, so technically you can gather all that you can from what you get, but know that eventually you will (and it's probably already this way) get multiple devices have the same "ID" that you've generated. There is no reliable way today to identify a device. You might get a "good enough" solution, but it will never be 100% reliable. One method of getting ID of the device used to be various fields of "android.os.Build" , for example. – android developer Oct 23 '20 at 14:11
  • @RavindraKushwaha OK I think I got one thing that you might be able to do, but only in specific cases: https://developer.android.com/reference/android/os/Build#getSerial() . For example, if your app is the default SMS app, you can get the serial of the device. There might be other similar things. I don't know. I just suggest to leave this idea alone. – android developer Oct 24 '20 at 10:32
  • Thanks Sir again – Ravindra Kushwaha Oct 24 '20 at 13:39
  • @RavindraKushwaha If you wish, there is also `Secure.getString(context.contentResolver, Secure.ANDROID_ID)` , but it says that it can get reset on some rare cases, and that it's different for each user on the device – android developer Oct 26 '20 at 11:40
  • Yes, It become different for the each applications, I have tested this one earlier.. If in the future , you get some solution on it then please let me know..Again thanks :) – Ravindra Kushwaha Oct 26 '20 at 11:43
  • @RavindraKushwaha You can read the link I've provided for all possible solutions, but again, many can be reset. In the future I don't think Google will make it easier. They tend to make things harder for security/privacy. – android developer Oct 26 '20 at 12:07
  • Yes you well said..Way will not be easy for it – Ravindra Kushwaha Oct 26 '20 at 12:37
2

@Jakar, thank you very much! Here's the same code in C# using Xamarin / Mono.Android

Requires the permission set in your AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Simple C#

using System.Collections.Generic;
using Android.Telephony;
// ...
protected override void OnCreate(Bundle bundle)
{
  base.OnCreate(bundle);

  // Requires, android.permission.READ_PHONE_STATE
  SubscriptionManager sm = Android.Telephony.SubscriptionManager.From(Android.App.Application.Context);
  IList<SubscriptionInfo> sis = sm.ActiveSubscriptionInfoList;
  SubscriptionInfo si = sis[0];

  string carrier = si.CarrierName;
  string iccId = si.IccId;
  string phoneNum = si.Number;

  System.Diagnostics.Debug.WriteLine("Carrier: " + carrier);
  System.Diagnostics.Debug.WriteLine("SIM Card IccId: " + iccId);
  System.Diagnostics.Debug.WriteLine("Phone Number: " + phoneNum);

  // Bonus, Get the IMEI Id (aka, DeviceId)
  TelephonyManager tm = (TelephonyManager)GetSystemService(Android.Content.Context.TelephonyService);
  string deviceId = tm.DeviceId;

  System.Diagnostics.Debug.WriteLine("Device Id: " + deviceId);
}

Both SubscriptionManager and SubscriptionInfo are found under the Android.Telephony namespace

Truth be told this example was needed to activate my phone on the fly and not having the SIM card tool handy to pull it out. And, my eyes aren't what they use to be. View it on DroidTelephonySimInfo.cs on gist.github.com

Damian
  • 1,209
  • 14
  • 24
  • @VladMatvienko, since this method is using SubscriptionManager I believe the answer is, yes. Feel free to try it out though. https://developer.android.com/about/versions/android-5.1.html – Damian Jun 22 '17 at 15:34