how to get both sim serial number below lollipop 5.0

607 views Asked by At

I try below code but it return null for both.

TelephonyManager telephonyInfo = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

telephonyInfo.serialSIM1 = telephonyManager.getSimSerialNumber();
        telephonyInfo.serialSIM2 = null;

        try {
            telephonyInfo.serialSIM1 = getSerialBySlot(context, "getSimSerialNumberGemini", 0);
            telephonyInfo.serialSIM2 = getSerialBySlot(context, "getSimSerialNumberGemini", 1);
        } catch (GeminiMethodNotFoundException e) {

            e.printStackTrace();

            try {
                telephonyInfo.serialSIM1 = getSerialBySlot(context, "getSimSerialNumber", 0);
                telephonyInfo.serialSIM2 = getSerialBySlot(context, "getSimSerialNumber", 1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }

private static String getSerialBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    String lsSerialNumber = null;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try{

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimID.invoke(telephony, obParameter);

        if(ob_phone != null){
            lsSerialNumber = ob_phone.toString();

        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return lsSerialNumber;
}


private static class GeminiMethodNotFoundException extends Exception {

    private static final long serialVersionUID = -996812356902545308L;

    public GeminiMethodNotFoundException(String info) {
        super(info);
    }
}

Then use it.

String getSerialNumber1 = telephonyInfo.getSerialSIM1();
String getSerialNumber2 = telephonyInfo.getSerialSIM2();

Log.i(TAG, "state getSerialNumber1 : " + getSerialNumber1);
Log.i(TAG, "state getSerialNumber2 : " + getSerialNumber2);

Below is the log which is got in logcat

09-16 07:20:07.042 6973-6973/app.myApp I/SimChangeReceiver: state getSerialNumber1 : null
09-16 07:20:07.042 6973-6973/app.myApp I/SimChangeReceiver: state getSerialNumber2 : null

I can get IMEI and the dual sim or not. I search around for the related question,But I did not found any other method.

1

There are 1 answers

1
Pratik Tank On

try this i have not tested but it might help you.

Instead of using getSimSerialNumber, you can use SubscriptionManager.

// Requires, android.permission.READ_PHONE_STATE
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();