Example usage for android.telephony TelephonyManager getDeviceId

List of usage examples for android.telephony TelephonyManager getDeviceId

Introduction

In this page you can find the example usage for android.telephony TelephonyManager getDeviceId.

Prototype

@Deprecated
@SuppressAutoDoc 
@RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
public String getDeviceId() 

Source Link

Document

Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.

Usage

From source file:com.firesoft.member.Member.java

public static String getDeviceId(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
    String deviceId = tm.getDeviceId();
    return deviceId;
}

From source file:Main.java

public static String getDeviceInfo(Context context) {
    try {//from   ww w.  j av  a  2 s  .  c o m
        org.json.JSONObject json = new org.json.JSONObject();
        android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        String device_id = tm.getDeviceId();

        android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);

        String mac = wifi.getConnectionInfo().getMacAddress();
        json.put("mac", mac);

        if (TextUtils.isEmpty(device_id)) {
            device_id = mac;
        }

        if (TextUtils.isEmpty(device_id)) {
            device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                    android.provider.Settings.Secure.ANDROID_ID);
        }

        json.put("device_id", device_id);

        return json.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * this generate the device Id/* w  w  w .ja  v a2  s  .  com*/
 *
 * @param baseContext
 * @param contentResolver
 * @return
 */
//http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id
public static String generateDeviceId(Context baseContext, ContentResolver contentResolver) {

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

    final String tmDevice, tmSerial, androidId;
    tmDevice = String.valueOf(tm.getDeviceId());
    tmSerial = String.valueOf(tm.getSimSerialNumber());
    androidId = String.valueOf(android.provider.Settings.Secure.getString(contentResolver,
            android.provider.Settings.Secure.ANDROID_ID));

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return deviceUuid.toString();

}

From source file:com.android.projectz.teamrocket.thebusapp.util.Device.java

/**
 * permette di prendere il codice IMEI del telefono
 *
 * @param context/*ww w  . j  av a  2  s .  c o  m*/
 * @return
 */
public static String getDeviceIMEI(Context context) {
    if (ActivityCompat.checkSelfPermission(context,
            Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
        return "ERR_PERMISSION";
    }
    String identifier = null;
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (tm != null) {
        identifier = tm.getDeviceId();
    }

    if (identifier == null || identifier.length() == 0) {
        identifier = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    return identifier;
}

From source file:com.njlabs.amrita.aid.util.Identifier.java

@SuppressLint("HardwareIds")
public static String identify(Context context) {
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, androidId;
    tmDevice = "" + tm.getDeviceId();
    tmSerial = "" + tm.getSimSerialNumber();
    androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),
            android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return deviceUuid.toString();
}

From source file:Main.java

public static String getDeviceUUID(Context context) {

    @SuppressWarnings("static-access")
    final TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);

    final String tmDevice, tmSerial, tmPhone, androidId;

    tmDevice = "" + tm.getDeviceId();

    tmSerial = "" + tm.getSimSerialNumber();

    androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(),
            android.provider.Settings.Secure.ANDROID_ID);

    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());

    String uniqueId = deviceUuid.toString();

    Log.d("debug", "uuid=" + uniqueId);

    return uniqueId;
}

From source file:Main.java

/**
 * @Thach//from w  w  w  .j  a  va  2 s  .c  o m
 * @Description: get and save deviceId into sharePreference
 * @param context
 * @return deviceId 
 */
public static String getDeviceId(Context context, String keyPref) {
    String deviceId = null;
    if (context != null) {
        // Get from sharePref first 
        deviceId = onGetPref(context, keyPref);
        // If this is first time
        if (deviceId == null) {
            TelephonyManager telephonyManager;
            telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            deviceId = telephonyManager.getDeviceId();
            // Get deviceId with other method - MAC address 
            if (deviceId == null) {
                deviceId = initMacAddress(context);
            }
        }
        // save to preference
        onSavePref(context, keyPref, deviceId);
    }
    return deviceId;
}

From source file:com.mpower.mintel.android.application.MIntel.java

public static String getDeviceId() {
    TelephonyManager tm = (TelephonyManager) getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
    return tm.getDeviceId();
}

From source file:Main.java

private static String getDeviceId(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String tmDevice = null;/*from  w w w .  j ava  2s  .c  om*/
    String tmSerial = null;
    String androidId = null;
    try {
        tmDevice = tm.getDeviceId();
    } catch (Exception e) {
        Log.w(LOG_TAG, e);
    }
    try {
        tmSerial = tm.getSimSerialNumber();
    } catch (Exception e) {
        Log.w(LOG_TAG, e);
    }
    androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    if (tmDevice == null) {
        tmDevice = "";
    }
    if (tmSerial == null) {
        tmSerial = "";
    }
    if (androidId == null) {
        androidId = "";
    }
    UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
    return "lsa-kp" + deviceUuid.toString();
}

From source file:Main.java

public static final String getOblyDevicesID(Context mContext) {

    String m_szImei = "";
    try {//from www .j  av  a 2 s.co  m
        TelephonyManager TelephonyMgr = null;
        TelephonyMgr = (TelephonyManager) mContext.getSystemService(Activity.TELEPHONY_SERVICE);
        m_szImei = TelephonyMgr.getDeviceId();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szDevIDShort = null;
    try {
        m_szDevIDShort = "35" + //we make this look like a valid IMEI

                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 10
                + Build.DEVICE.length() % 10 + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
                + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 + Build.MODEL.length() % 10
                + Build.PRODUCT.length() % 10 + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
                + Build.USER.length() % 10; //13 digits
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    String m_szAndroidID = "";
    try {
        m_szAndroidID = "";
        Settings.Secure.getString(mContext.getContentResolver(), Settings.Secure.ANDROID_ID);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szWLANMAC = "";
    try {
        WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
        m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szBTMAC = null;
    try {
        BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter
        m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        m_szBTMAC = m_BluetoothAdapter.getAddress();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID + m_szWLANMAC + m_szBTMAC;
    // compute md5
    MessageDigest m = null;
    try {
        m = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
    // get md5 bytes
    byte p_md5Data[] = m.digest();
    // create a hex string
    String m_szUniqueID = new String();
    for (int i = 0; i < p_md5Data.length; i++) {
        int b = (0xFF & p_md5Data[i]);
        // if it is a single digit, make sure it have 0 in front (proper padding)
        if (b <= 0xF)
            m_szUniqueID += "0";
        // add number to string
        m_szUniqueID += Integer.toHexString(b);
    } // hex string to uppercase
    m_szUniqueID = m_szUniqueID.toUpperCase();

    return m_szUniqueID;
}