public class DeviceIdUtils {
//保存文件的路径
private static final String CACHE_IMAGE_DIR = "aray/cache/devices";
//保存的文件 采用隐藏文件的形式进行保存
private static final String DEVICES_FILE_NAME = ".DEVICES";
/**
* 获取设备唯一标识符
*
* @param context
* @return
*/
public static String getDeviceId(Context context) {
String deviceId = SpUtils.getString(context, SpConstant.SP_DEVICES_ID, "");
if (!StringUtils.isEmpty(deviceId)) {
//生成deviceId并且保存到file以及sp
return deviceId;
}
//读取保存的在sd卡中的唯一标识符
deviceId = readDeviceID(context);
//判断是否已经生成过,
if (!StringUtils.isEmpty(deviceId)) {
//保存到sp中
SpUtils.putString(context, SpConstant.SP_DEVICES_ID, deviceId);
return deviceId;
}
//用于生成最终的唯一标识符
StringBuffer s = new StringBuffer();
try {
//获取设备的MACAddress地址 去掉中间相隔的冒号
deviceId = getLocalMac(context);
if(!StringUtils.isEmpty(deviceId)){
deviceId = deviceId.replace(":", "");
s.append(deviceId);
}
} catch (Exception e) {
e.printStackTrace();
}
//UUID生成随机字符串
UUID uuid = UUID.randomUUID();
deviceId = uuid.toString().replace("-", "");
s.append(deviceId);
//为了统一格式对设备的唯一标识进行md5加密 最终生成32位字符串
String md5 = getMD5(s.toString(), false);
if (s.length() > 0) {
//持久化操作, 进行保存到SD卡中
saveDeviceID(md5, context);
//保存到sp中
SpUtils.putString(context, SpConstant.SP_DEVICES_ID, md5);
}
return md5;
}
/**
* 读取固定的文件中的内容,这里就是读取sd卡中保存的设备唯一标识符
*
* @param context
* @return
*/
public static String readDeviceID(Context context) {
File file = getDevicesDir(context);
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
Reader in = new BufferedReader(isr);
int i;
while ((i = in.read()) > -1) {
buffer.append((char) i);
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取设备MAC 地址 由于 6.0 以后 WifiManager 得到的 MacAddress得到都是 相同的没有意义的内容
* 所以采用以下方法获取Mac地址
*
* @param context
* @return
*/
private static String getLocalMac(Context context) {
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "";
}
return macAddress;
}
/**
* 保存 内容到 SD卡中, 这里保存的就是 设备唯一标识符
*
* @param str
* @param context
*/
public static void saveDeviceID(String str, Context context) {
File file = getDevicesDir(context);
try {
FileOutputStream fos = new FileOutputStream(file);
Writer out = new OutputStreamWriter(fos, "UTF-8");
out.write(str);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 对挺特定的 内容进行 md5 加密
*
* @param message 加密明文
* @param upperCase 加密以后的字符串是是大写还是小写 true 大写 false 小写
* @return
*/
public static String getMD5(String message, boolean upperCase) {
String md5str = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] input = message.getBytes();
byte[] buff = md.digest(input);
md5str = bytesToHex(buff, upperCase);
} catch (Exception e) {
e.printStackTrace();
}
return md5str;
}
public static String bytesToHex(byte[] bytes, boolean upperCase) {
StringBuffer md5str = new StringBuffer();
int digital;
for (int i = 0; i < bytes.length; i++) {
digital = bytes[i];
if (digital < 0) {
digital += 256;
}
if (digital < 16) {
md5str.append("0");
}
md5str.append(Integer.toHexString(digital));
}
if (upperCase) {
return md5str.toString().toUpperCase();
}
return md5str.toString().toLowerCase();
}
/**
* 统一处理设备唯一标识 保存的文件的地址
*
* @param context
* @return
*/
private static File getDevicesDir(Context context) {
File mCropFile = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File cropdir = new File(Environment.getExternalStorageDirectory(), CACHE_IMAGE_DIR);
if (!cropdir.exists()) {
cropdir.mkdirs();
}
mCropFile = new File(cropdir, DEVICES_FILE_NAME); // 用当前时间给取得的图片命名
} else {
File cropdir = new File(context.getFilesDir(), CACHE_IMAGE_DIR);
if (!cropdir.exists()) {
cropdir.mkdirs();
}
mCropFile = new File(cropdir, DEVICES_FILE_NAME);
}
return mCropFile;
}
}