5

in which cases this method returns a null reference? Can only depend on the sim card? Doesn't exist, in these cases, an alternative to retrieve an identification number of the latter?

Thanks to all!

kannappan
  • 2,250
  • 3
  • 25
  • 35
Vincenzo
  • 51
  • 1
  • 1
  • 2
  • Telephony sims serial no is restricted in Android 10. We can use the TELEPHONY_SUBSCRIPTION_SERVICE instead (>= Android 5.1). See my answer below for details. – Adarsh Chithran Jan 22 '20 at 12:40

3 Answers3

3

getSimSerialNumber() needs privileged permissions from Android 10 onwards, and third party apps can't register this permission.

See : https://developer.android.com/about/versions/10/privacy/changes#non-resettable-device-ids

A possible solution is to use the TELEPHONY_SUBSCRIPTION_SERVICE from Android 5.1, to retrieve the sim serial number. Steps below:

  • Check for READ_PHONE_STATE permission.
  • Get Active subscription list.( Returns the list of all active sim cards)
  • Retrieve the sim details from Subscription Object.

    if ( isPermissionGranted(READ_PHONE_STATE) ) {

            String simSerialNo="";
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
    
                SubscriptionManager subsManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE); 
    
                List<SubscriptionInfo> subsList = subsManager.getActiveSubscriptionInfoList();
    
                if (subsList!=null) {
                    for (SubscriptionInfo subsInfo : subsList) {
                        if (subsInfo != null) {
                            simSerialNo  = subsInfo.getIccId());
                        }
                    }
                }
            } else {
                TelephonyManager tMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                simSerialNo = tMgr.getSimSerialNumber();
            }
        }
    

Check if this helps

Adarsh Chithran
  • 218
  • 3
  • 6
2

Please make sure whether you have added the following permission in the Android Manifest, if not please add this statement add try again.

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

Note: Add this permission tag outside the application tag..

Sample Snippet:

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

All the Best

Nandagopal T
  • 2,037
  • 9
  • 42
  • 54
0

try the below code..i will help full for u..

TelehponyManager manager = (TelehponyManager)getSystemService(TELEPHONY_SERVICE);

String imei = manager.getDeviceId();

String imsi = manager.getSubscriberId();
King of Masses
  • 18,405
  • 4
  • 60
  • 77
kannappan
  • 2,250
  • 3
  • 25
  • 35
  • Hi, thanks for your answer, but it, unfortunately, doesn't solve my problem. I need to uniquely identify the sim card. The IMEI is paired to the device and the "subscriber id" identifies the account within the operator's network, accordingly if the user changes operator taking the phone number (with the number portability) the IMSI could change but the sim card is the same. – Vincenzo Apr 06 '11 at 08:56