Sali
Sali

Reputation: 101

TelephonyManager.getDeviceId only returns 14 digit

TelephonyManager.getDeiceId() only return 14 digits of IMEI number. But under Setting->Phone->Status show 15 digits.

I want to get 15 digit as appear under phone settings.

Upvotes: 2

Views: 5449

Answers (4)

AndroidCoolestRulest
AndroidCoolestRulest

Reputation: 563

Some devices don't add last digit, we need to calculate last digit instead using Luhn algorithm:

private int getImeiCheckDigit(String imei14digits) {
    if (imei14digits == null || imei14digits.length() != 14) {
        throw new IllegalArgumentException("IMEI should be 14 digits");
    }
    int[] imeiArray = new int[imei14digits.length()];
    final int DIVIDER = 10;
    char[] chars = imei14digits.toCharArray();
    for (int i = 0; i < chars.length; i++) {
        imeiArray[i] = Character.getNumericValue(chars[i]);
    }
    int sum = 0;
    for (int i = 0; i < imeiArray.length; i++) {
        if (i % 2 == 0) {
            sum += imeiArray[i];
        } else {
            int multi = imeiArray[i] * 2;
            if (multi >= DIVIDER) {
                sum += multi % DIVIDER;
                sum += multi / DIVIDER;
            } else {
                sum += multi;
            }
        }
    }
    return (DIVIDER - sum % DIVIDER) % DIVIDER;
}

Upvotes: 1

Hemendra Sharma
Hemendra Sharma

Reputation: 1060

Or you can manually calculate the check-sum of the 14-digit IMEI number. e.g.

private int GetCheckSumDigit(String id) {
        int digit = -1;
        try{
            if(id.length() == 14)
            {
                String str = "";
                char[] digits = new char[id.length()];
                id.getChars(0, id.length(), digits, 0);
                for(int i=0; i<digits.length; i++)
                {
                    String ch = digits[i]+"";
                    if((i+1)%2==0)
                    {
                        int x = Integer.parseInt(digits[i]+"");
                        x *= 2;
                        ch = x+"";
                    }
                    str += ch;
                }
                digits = new char[str.length()];
                str.getChars(0, str.length(), digits, 0);
                int total = 0;
                for(int i=0; i<str.length(); i++)
                    total += Integer.parseInt(digits[i]+"");
                //
                int count = 0;
                while((total+count)%10 != 0)
                    count++;
                digit = count;
            }
        }catch(Exception exx)
        {
            exx.printStackTrace();
        }
        return digit;
    }

Good Luck.

Upvotes: 1

Code_Life
Code_Life

Reputation: 5892


TelephonyManager mTelephonyMgr;
 mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 mTelephonyMgr.getDeviceId()

This is what i have done and i getting all the 15 digits may this can help u ...

Upvotes: 1

Waza_Be
Waza_Be

Reputation: 39558

The IMEI (14 digits) is complemented by a check digit. The check digit is not part of the digits transmitted at IMEI check occasions. The Check Digit shall avoid manual transmission errors, e.g. when customers register stolen mobiles at the operator's customer care desk.

http://www.tele-servizi.com/Janus/texts/imei.txt

http://en.wikipedia.org/wiki/International_Mobile_Equipment_Identity

Upvotes: 3

Related Questions