20年疫情期间,为公司开发过一款安全防疫系统,主要用户医院,医生手持PDA样机对来访人员进行测温,扫健康码或者身份证查看健康状态,然后作人员登记录入web管理系统。
移动端
移动端即手持PDA注册界面如下
WEB管理系统
web管理系统之设备管理界面如下
经过三轮版本测试项目很快上线了,直到21年的一天,实施人员在现场部署的时候发现一台手持终端PDA注册之后,其他的手持终端不需要注册即可登录。拿到问题反馈的时候很奇怪,因为之前用的两个厂商的PDA,即用两台PDA产品测试的都没问题,为啥同一品牌的会出现这个问题呢,原来虽然生成了设备标识,不同的品牌不一样,但是对于同一个品牌,硬件部分一致,而又获取不到Serial Number导致最终生成的设备标识在同一品牌的同一产品下,标识是一样的,下面上解决方案
public static String getUniquePsuedoID(Context context) {
String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);
String serial = null;
try {
serial =getDeviceId(context);
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
} catch (Exception exception) {
serial = "serial";
}
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}
//保存文件的路径
private static final String CACHE_DEVICES_DIR = "aqfyapp-devices/devices";
//保存的文件 采用隐藏文件的形式进行保存
private static final String DEVICES_FILE_NAME = ".DEVICES";
/**
* 获取设备唯一标识符
*
* @param context
* @return
*/
public static String getDeviceId(Context context) {
//读取保存的在sd卡中的唯一标识符
String deviceId = readDeviceID(context);
//判断是否已经生成过,
if (deviceId != null && !"".equals(deviceId)) {
return deviceId;
}
//用于生成最终的唯一标识符
StringBuffer s = new StringBuffer();
try {
//获取IMES(也就是常说的DeviceId)
deviceId = getIMIEStatus(context);
s.append(deviceId);
} catch (Exception e) {
e.printStackTrace();
}
try {
//获取设备的MACAddress地址 去掉中间相隔的冒号
deviceId = getLocalMac(context).replace(":", "");
s.append(deviceId);
} catch (Exception e) {
e.printStackTrace();
}
// }
//如果以上搜没有获取相应的则自己生成相应的UUID作为相应设备唯一标识符
if (s == null || s.length() <= 0) {
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);
}
return md5;
}
/**
* 读取固定的文件中的内容,这里就是读取sd卡中保存的设备唯一标识符
*
* @param context
* @return
*/
private 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;
}
}
/**
* 获取设备的DeviceId(IMES) 这里需要相应的权限<br/>
* 需要 READ_PHONE_STATE 权限
*
* @param context
* @return
*/
private static String getIMIEStatus(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
return deviceId;
}
/**
* 获取设备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
*/
private 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
*/
private 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;
}
private 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_DEVICES_DIR);
if (!cropdir.exists()) {
cropdir.mkdirs();
}
mCropFile = new File(cropdir, DEVICES_FILE_NAME);
} else {
File cropdir = new File(context.getFilesDir(), CACHE_DEVICES_DIR);
if (!cropdir.exists()) {
cropdir.mkdirs();
}
mCropFile = new File(cropdir, DEVICES_FILE_NAME);
}
return mCropFile;
}
调用方式:
CommonUtils.getUniquePsuedoID(this)
CommonUtils即为方法的封装类