Get Imei on Android using Java

Asked

Viewed 798 times

1

I’m needing to get a unique identifier for my app, I’m trying to get the device IMEI and I’m not getting, below follows the code I tried to get the IMEI and it always stops at the first if that has the return "imei error";

Method to obtain the IMEI

public String obterImei() {
        String imeinum = "huhu";


        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return "imei error";
        }else{

            imeinum = tm.getDeviceId();

            return imeinum;
        }
    }

Method call:

   String imei = obterImei();

2 answers

0


I used the method below to get the MAC. Creditworthiness

public static String getMacAddr() {
    try {
        List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface nif : all) {
            if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

            byte[] macBytes = nif.getHardwareAddress();
            if (macBytes == null) {
                return "";
            }

            StringBuilder res1 = new StringBuilder();
            for (byte b : macBytes) {
                res1.append(String.format("%02X:",b));
            }

            if (res1.length() > 0) {
                res1.deleteCharAt(res1.length() - 1);
            }
            return res1.toString();
        }
    } catch (Exception ex) {
    }
    return "02:00:00:00:00:00";
}

0

Try to use it like this to get the IMEI of the device.

/**
 * Returns the unique identifier for the device
 *
 * @return unique identifier for the device
 */
public String getDeviceIMEI() {
    String deviceUniqueIdentifier = null;
    TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    if (null != tm) {
        deviceUniqueIdentifier = tm.getDeviceId();
    }
    if (null == deviceUniqueIdentifier || 0 == deviceUniqueIdentifier.length()) {
        deviceUniqueIdentifier = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
    }
    return deviceUniqueIdentifier;
}

But don’t forget to put it on Manifest.xml.

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

Creditworthiness: How to get the device’s IMEI/ESN programmatically in android?

  • didn’t work out he asks checkPermission

  • On this link is the permission check part if you can take a look you will see that there are more complete answers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.