get UUID Key - Android Hardware

Android examples for Hardware:Device ID

Description

get UUID Key

Demo Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

import android.annotation.SuppressLint;

import android.content.Context;

import android.content.pm.PackageManager;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import android.os.Build;

import android.provider.Settings;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

public class Main {
    public static final String UNKNOW = "Unknown";

    @SuppressLint("InlinedApi")
    public static final String getUUIDKey(Context context) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(getSessionId(context));
        stringBuffer.append("-");
        stringBuffer.append(getCPUInfo());
        stringBuffer.append("-");
        stringBuffer.append(getMAC(context));
        stringBuffer.append("-");
        try {/*  w  ww.  j av  a2  s.c  om*/
            stringBuffer.append(Build.HARDWARE);
        } catch (Throwable e) {
        }
        if (checkPermission(context, "android.permission.READ_PHONE_STATE")) {
            stringBuffer.append("-");
            stringBuffer.append(getIMEI(context));
        }
        return stringBuffer.toString();
    }

    public static final String getSessionId(Context context) {
        return Settings.Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID);
    }

    public static final String getCPUInfo() {
        String str = null;
        try {
            FileReader localFileReader = new FileReader("/proc/cpuinfo");
            if (localFileReader != null) {
                BufferedReader localBufferedReader = new BufferedReader(
                        localFileReader, 1024);
                str = localBufferedReader.readLine();
                localBufferedReader.close();
                localFileReader.close();
            }
        } catch (IOException localFileNotFoundException) {
        }
        if (!TextUtils.isEmpty(str)) {
            return str.substring(str.indexOf(':') + 1).trim();
        }
        return str;
    }

    public static final String getMAC(Context context) {
        try {
            WifiManager cWifiManager = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            if (checkPermission(context,
                    "android.permission.ACCESS_WIFI_STATE")) {
                WifiInfo localWifiInfo = cWifiManager.getConnectionInfo();
                return TextUtils.isEmpty(localWifiInfo.getMacAddress()) ? UNKNOW
                        : localWifiInfo.getMacAddress();
            }
        } catch (Exception localException) {
        }
        return UNKNOW;
    }

    public static final boolean checkPermission(Context context,
            String premission) {
        PackageManager localPackageManager = context.getPackageManager();
        if (localPackageManager.checkPermission(premission,
                context.getPackageName()) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
            return false;
        }
        return true;
    }

    public static final String getIMEI(Context context) {
        if (checkPermission(context, "android.permission.READ_PHONE_STATE")) {
            TelephonyManager localTelephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            if (localTelephonyManager == null) {
                return UNKNOW;
            } else {
                return localTelephonyManager.getDeviceId();
            }
        }
        return UNKNOW;
    }
}

Related Tutorials