Programmatically retrieve IMEI number for dual SIM in android
Asked Answered
F

12

15

For single SIM following code works:

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String imei= tm.getDeviceId();

For dual SIM I tried code on following link:

Android : Check whether the phone is dual SIM

But it didnt work for me.

Let me know if any other solutions possible.

Fauch answered 20/3, 2014 at 5:46 Comment(3)
have a look at #13473451Michaella
Check out this link #14517838Soniasonic
which API version are you targetting? I too am developing a dual-SIM app that needs to work very old phones and new alike.Trophoplasm
L
6

Try using getDeviceId(int slotId) added in API level 23.

Returns the unique device ID of a subscription, for example, the IMEI for GSM and the MEID for CDMA phones. Return null if device ID is not available.

Requires Permission: READ_PHONE_STATE

Logarithmic answered 7/3, 2016 at 5:58 Comment(0)
H
5

We can check whether phone single or dual sim by using Android API and IMEI for each sim Card

TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Log.i("OmSai ", "Single or Dula Sim "+manager.getPhoneCount());
Log.i("OmSai ", "Defualt device ID "+manager.getDeviceId());
Log.i("OmSai ", "Single 1 "+manager.getDeviceId(0));
Log.i("OmSai ", "Single 2 "+manager.getDeviceId(1));
Hardunn answered 21/11, 2015 at 9:50 Comment(4)
This is used for Android 23+ as mentioned in Android SDK developer.android.com/reference/android/telephony/…Borderer
For which version your looking.Hardunn
I am looking for API 15+Unremitting
getPhoneCount () method is not availableApteral
D
1
TelephonyManager telephony = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    try {

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getFirstMethod = telephonyClass.getMethod("getDeviceId", parameter);
        Log.d("SimData", getFirstMethod.toString());
        Object[] obParameter = new Object[1];
        obParameter[0] = 0;
        TelephonyManager manager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String first = (String) getFirstMethod.invoke(telephony, obParameter);
        Log.d("SimData", "first :" + first);
        obParameter[0] = 1;
        String second = (String) getFirstMethod.invoke(telephony, obParameter);
        Log.d("SimData", "Second :" + second);

    } catch (Exception e) {
        e.printStackTrace();
    }

try using this,this should help getting both device id on android lollipop

Decretal answered 14/11, 2016 at 6:59 Comment(0)
B
1

Yes we can get both IMEI numbers Using this below code

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imeiNumber1 = tm.getDeviceId(1); //(API level 23)   
String imeiNumber2 = tm.getDeviceId(2);
Bung answered 25/11, 2016 at 7:6 Comment(2)
I followed your answer but it given me reverse(i got "imeiNumber1" as secondary IMEI and "imeiNumber2" as primary IMEI,then i did more research and got the solution. TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String imeiNumber1 = tm.getDeviceId(0); String imeiNumber2 = tm.getDeviceId(1);Inconveniency
suppose i have to create contact programtically in sim1 and sim2 then can you please tell me what Sim URI we will Use thanx . @BungTopless
A
1
TelephonyManager first = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

Log.d(TAG, "Device Id: " + first.getDeviceId() + ", device status: " + first.getSimState() + ", operator: " + first.getNetworkOperator() + "/" + first.getNetworkOperatorName());

obParameter[0] = 1;
TelephonyManager second = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

Log.d(TAG, "Device Id: " + second.getDeviceId() + ", device status: " + second.getSimState()+ ", operator: " + second.getNetworkOperator() + "/" + second.getNetworkOperatorName());
Abandoned answered 29/5, 2017 at 12:7 Comment(3)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Hurd
Welcome to SO. Please read this how-to-answer for providing quality answer.Nathannathanael
suppose i have to create contact programtically in sim1 and sim2 then can you please tell me what Sim URI we will Use thanx . @Ravindra BarnwalTopless
I
0

AFAIK it is not possible. The java reflection, you used could work for some devices but not all. However there might be some manufacturer specific API's that allows for this.

Quoting Commonsware,

Android does not support multiple SIMs, at least from the SDK. Device manufacturers who have created multi-SIM devices are doing so on their own. You are welcome to contact your device manufacturer and see if they have an SDK add-on or something that allows you to access the second SIM.

Insnare answered 20/3, 2014 at 6:14 Comment(1)
This is not correct anymore. You can both imeis in recent versions of AndroidDuky
R
0

Yes Android currently not supported Dual Sim. But you can retrive all possible details by using Java reflection.

I research for fetching dual SIMs details and it works on bellowed devices
Samsung Galaxy Neo
Moto E
Micromax A52
Micromax Canvas
Linovo P780
HTC Dreem
Moto G
LG
HUAWEI Y520-U22
LG-P705
Sony ST26i
I successfully get dual SIM Detials from above models

Rockey answered 3/3, 2015 at 5:33 Comment(3)
SubscriptionManager is a class in which you can get all cell info above android 5.1Rockey
Are you able to get both sims carrier/operator name using Reflections?Halvorson
suppose i have to create contact programtically in sim1 and sim2 then can you please tell me what Sim URI we will Use thanx . @RockeyTopless
N
0

You can use this method to get both imei. Sorry for inconvenience. I was in a hurry.

public static void samsungTwoSims(Context context) {
    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 getFirstMethod = telephonyClass.getMethod("getDefault", parameter);

        Log.d(TAG, getFirstMethod.toString());

        Object[] obParameter = new Object[1];
        obParameter[0] = 0;
        TelephonyManager first = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

        Log.d(TAG, "Device Id: " + first.getDeviceId() + ", device status: " + first.getSimState() + ", operator: " + first.getNetworkOperator() + "/" + first.getNetworkOperatorName());

        obParameter[0] = 1;
        TelephonyManager second = (TelephonyManager) getFirstMethod.invoke(null, obParameter);

        Log.d(TAG, "Device Id: " + second.getDeviceId() + ", device status: " + second.getSimState()+ ", operator: " + second.getNetworkOperator() + "/" + second.getNetworkOperatorName());
    } catch (Exception e) {
        e.printStackTrace();
    }   
}
Nonstop answered 4/8, 2016 at 6:36 Comment(0)
A
0

You can IMEI in Android O or above.

Set<String> deviceIdList = new HashSet<>();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int phoneCount = telephonyManager.getPhoneCount();
for (int i = 0; i < phoneCount; i++) {
   deviceIdList.add(telephonyManager.getImei(i));
}
Altostratus answered 23/6, 2018 at 12:4 Comment(0)
M
0

Steps: 1 > You must have READ_PHONE_STATE Permission enabled

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

2 > For Android SDK v23<= get SIM 1 & SIM 2 IMEI by this:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    Log.e("IMEI---1:", tm.getDeviceId(0) );
    Log.e("IMEI---2:", tm.getDeviceId(1) );
Municipal answered 22/1, 2019 at 7:40 Comment(0)
C
0

Try following code to get IMEI Number for an Android Device:

telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);

    btn_imei.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        public void onClick(View v) {
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) 
            {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_CODE);
                return;
            }

            StringBuilder stringBuilder = new StringBuilder();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                for (int i = 0; i < telephonyManager.getPhoneCount(); i++) {
                    stringBuilder.append(telephonyManager.getImei(i));
                    stringBuilder.append("\n");
                }
            }
            txt_imei.setText(stringBuilder.toString());
        }
    });
Culm answered 4/3, 2020 at 4:24 Comment(0)
I
0

TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);

    String imeiSIM1 = telephonyInfo.getImsiSIM1();
    String imeiSIM2 = telephonyInfo.getImsiSIM2();

    boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
    boolean isSIM2Ready = telephonyInfo.isSIM2Ready();

    boolean isDualSIM = telephonyInfo.isDualSIM();

    TextView tv = (TextView) findViewById(R.id.txt_imei);
    tv.setText(" IME1 : " + imeiSIM1 + "\n" +
            " IME2 : " + imeiSIM2 + "\n" +
            " IS DUAL SIM : " + isDualSIM + "\n" +
            " IS SIM1 READY : " + isSIM1Ready + "\n" +
            " IS SIM2 READY : " + isSIM2Ready + "\n");

}
Interrogatory answered 1/9, 2020 at 5:46 Comment(1)
Your answer needs formatting and it seems to be missing both part of the code and an explanation.Kastroprauxel

© 2022 - 2024 — McMap. All rights reserved.