Programmatically retrieve IMEI number for dual SIM in android

徘徊边缘 提交于 2019-11-27 20:45:39
Vijay Shelke

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));

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

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

user7036069

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);
Ravindra Barnwal
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());
Sahil Mahajan Mj

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.

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

user6676551

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();
    }   
}

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));
}

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) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!