getimei() giving me imei of 2nd sim slot in some andorid Devices?

331 Views Asked by At

Sometimes, I am getting the IMEI of the second SIM SLOT when getting from this code :

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                    String imei = telephonyManager.getImei();

                    return imei;

                } else {

                    String imei = telephonyManager.getDeviceId();
                    return imei;
                }

This is affecting the users where if their deviceId gets changed they will get logged out. Because of the mismatch in the device.

What are the chances of getting IMEI of the second SIM even if I am querying the IMEI of the SIM SLOT 1?

1

There are 1 best solutions below

0
Anupam On

static boolean handleDeviceIdDisplay(Context context, String input) { TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    if (telephonyManager != null && input.equals(MMI_IMEI_DISPLAY)) {
        int labelResId = (telephonyManager.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) ?
                R.string.imei : R.string.meid;

        List<string> deviceIds = new ArrayList<string>();
        if (TelephonyManagerCompat.getPhoneCount(telephonyManager) > 1 &&
                CompatUtils.isMethodAvailable(TelephonyManagerCompat.TELEPHONY_MANAGER_CLASS,
                        "getDeviceId", Integer.TYPE)) {
            for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {
                String deviceId = telephonyManager.getDeviceId(slot);
                if (!TextUtils.isEmpty(deviceId)) {
                    deviceIds.add(deviceId);
                }
            }
        } else {
            deviceIds.add(telephonyManager.getDeviceId());
        }

        AlertDialog alert = new AlertDialog.Builder(context)
                .setTitle(labelResId)
                .setItems(deviceIds.toArray(new String[deviceIds.size()]), null)
                .setPositiveButton(android.R.string.ok, null)
                .setCancelable(false)
                .show();
        return true;
    }
    return false;
}

Hope it helps