Get device id(if null, use mac. - Android Hardware

Android examples for Hardware:Device ID

Description

Get device id(if null, use mac.

Demo Code

import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;

public class Main {
  private static final String NETWORT_ETHERNET = "eth0";
  private static final String NETWORT_WIFI = "wlan0";

  /**/* w  w  w  . java  2s  . co m*/
   * Get device id(if null, use mac. If mac is null either, use android id)
   *
   * @param context
   *          The context of the application
   * @return Return device's id
   */
  public static String getDeviceId(Context context) {
    android.telephony.TelephonyManager telephonyManager = (android.telephony.TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);

    String deviceId = "";

    try {
      if (checkPermissionGranted(context, "android.permission.READ_PHONE_STATE")) {
        deviceId = telephonyManager.getDeviceId();
      }
    } catch (Exception e) {

    }

    if (TextUtils.isEmpty(deviceId)) {

      deviceId = getDeviceMac(context);

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

  private static boolean checkPermissionGranted(Context context, String permission) {
    PackageManager packageManager = context.getPackageManager();
    if (packageManager.checkPermission(permission, context.getPackageName()) != PackageManager.PERMISSION_GRANTED) {
      return false;
    }
    return true;
  }

  /**
   * Get the MAC address.
   *
   * @param context
   *          The context of the application
   * @return Return device's mac address
   */
  public static String getDeviceMac(Context context) {
    // Use wifi manager to get mac address
    android.net.wifi.WifiManager wifi = (android.net.wifi.WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    String deviceMac = wifi.getConnectionInfo().getMacAddress();
    if (!TextUtils.isEmpty(deviceMac)) {
      return deviceMac;
    }

    // Get ethernet mac address
    deviceMac = getMACAddress(NETWORT_ETHERNET);
    if (!TextUtils.isEmpty(deviceMac)) {
      return deviceMac;
    }

    // Get wifi mac address
    deviceMac = getMACAddress(NETWORT_WIFI);
    if (!TextUtils.isEmpty(deviceMac)) {
      return deviceMac;
    }

    // Get other mac address
    deviceMac = getMACAddress(null);
    if (!TextUtils.isEmpty(deviceMac)) {
      return deviceMac;
    }

    return "";
  }

  private static String getMACAddress(String interfaceName) {
    try {
      List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
      for (NetworkInterface intf : interfaces) {
        if (interfaceName != null) {
          if (!intf.getName().equalsIgnoreCase(interfaceName)) {
            continue;
          }
        }
        byte[] mac = intf.getHardwareAddress();
        if (mac == null) {
          return "";
        }
        StringBuilder buf = new StringBuilder();
        for (int idx = 0; idx < mac.length; idx++)
          buf.append(String.format("%02X:", mac[idx]));
        if (buf.length() > 0)
          buf.deleteCharAt(buf.length() - 1);
        return buf.toString();
      }
    } catch (Exception ex) {
      // for now eat exceptions
    }
    return "";
  }
}

Related Tutorials