TelephonyManager.getDeviceId (0) returns different results

For some specific reasons, I need to get the IMEI at some point in my android app. Here is the code I'm using:

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
    // API available only >= 6.0
    // Get first slot to avoid issue if we have multiple sim cards
    String imei = tm.getDeviceId(0);
}
else
{
    String imei = tm.getDeviceId();
}

      

It works great in most cases. However, some devices (like the Huawei Honor 7) offer dual SIM capabilities. In the settings, the user has the ability to switch between two SIM cards to support 3G / 4G.

When I have two SIMs and I do this switch, the IMEI I get is different.

As far as I know, IMEI is associated with a physical slot and shouldn't change. It looks like a bad constructor implementation.

Any idea for a workaround?

+1


source to share


1 answer


As you said, the IMEI / Device ID is tagged in the sim slot.

For two SIM phones, there are three IMEI values โ€‹โ€‹(one for each slot) and IMEI-SV.

Let's say the IMEI for slot 1 is 123456789012345

IMEI for slot 2: 012500123456789

Depending on the scenarios, below is the return value of telephonyManagerObj.getDeviceId ():



  • If you don't have a SIM card, the method will return the IMEI for slot 1, i.e. 123456789012345
  • When you have a SIM card in slot 1, the method returns the IMEI for slot 1, i.e. 123456789012345
  • When you have a SIM card in both slots, the method will return the IMEI for slot 1, i.e. 123456789012345
  • But when you only have a SIM in slot 2, the method returns the IMEI for slot2, which is 012500123456789.
  • I found that on one device, when I incorrectly insert the SIM into slot 1, the returned IMEI-SV method

To maintain consistency, you need to save the IMEI to SharedPreference / Sqlite as soon as you can restore it.

So when you need the IMEI value in your code, you can first check if it is available in your local storage. If it is not available, extract the IMEI and save it for next time use.

Be careful, getDeviceId () is deprecated in Android O. Check like this for alternatives

+1


source







All Articles