package com.hdl.sdk.sourceos.utils;
|
|
import static android.content.Context.WIFI_SERVICE;
|
|
import android.annotation.SuppressLint;
|
import android.content.ContentResolver;
|
import android.content.Context;
|
import android.content.pm.PackageInfo;
|
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.text.TextUtils;
|
|
import com.hdl.sdk.common.utils.SPUtils;
|
import com.hdl.sdk.sourceos.OsManager;
|
import com.hdl.sdk.sourceos.Rk3566Manager;
|
|
import java.lang.reflect.Method;
|
import java.net.InetAddress;
|
import java.net.NetworkInterface;
|
import java.net.SocketException;
|
import java.util.Enumeration;
|
import java.util.UUID;
|
|
/**
|
* Created by Tong on 2021/11/8.
|
* ACCESS_WIFI_STATE
|
*/
|
public class DeviceUtils {
|
|
public static final String ANDROID_DEVICE_ID_KEY = "android_device_id_key";
|
|
/**
|
* @return 获取设备ID,Android10 只能用uuid生成了
|
*/
|
public static String getUniqueDeviceId() {
|
String deviceId = SPUtils.getString(ANDROID_DEVICE_ID_KEY);
|
if (!TextUtils.isEmpty(deviceId)) {
|
return deviceId;
|
}
|
deviceId = getDeviceId();
|
SPUtils.put(ANDROID_DEVICE_ID_KEY, deviceId);
|
return deviceId;
|
}
|
|
private static String getDeviceId() {
|
String deviceId;
|
try {
|
|
deviceId = getOsMacAddress();
|
if (!TextUtils.isEmpty(deviceId)) {
|
return deviceId;
|
}
|
|
deviceId = getMacAddress();
|
if (!TextUtils.isEmpty(deviceId)) {
|
return deviceId;
|
}
|
|
/*deviceId = getIMEI();
|
if (!TextUtils.isEmpty(deviceId)) {
|
return deviceId;
|
}*/
|
|
deviceId = getAndroidId();
|
if (!TextUtils.isEmpty(deviceId)) {
|
return deviceId;
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
|
deviceId = getUUid("", "");
|
return deviceId;
|
}
|
|
/**
|
* 需要系统权限
|
* READ_PRIVILEGED_PHONE_STATE
|
*/
|
/*private static String getIMEI(Context context) {
|
try {
|
TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
return tm.getImei();
|
} else {
|
return tm.getDeviceId();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}*/
|
private static String getAndroidId() {
|
try {
|
ContentResolver contentResolver = Rk3566Manager.getInstance().getContext().getApplicationContext().getContentResolver();
|
return Settings.System.getString(contentResolver, Settings.Secure.ANDROID_ID);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
public static String getUUid(String prefix, String id) {
|
if (id.equals("")) {
|
return prefix + UUID.randomUUID().toString().replace("-", "");
|
}
|
return prefix + UUID.nameUUIDFromBytes(id.getBytes()).toString().replace("-", "");
|
}
|
|
public static InetAddress getInetAddress() {
|
try {
|
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
|
while (nis.hasMoreElements()) {
|
NetworkInterface ni = nis.nextElement();
|
// To prevent phone of xiaomi return "10.0.2.15"
|
if (!ni.isUp()) continue;
|
Enumeration<InetAddress> addresses = ni.getInetAddresses();
|
while (addresses.hasMoreElements()) {
|
InetAddress inetAddress = addresses.nextElement();
|
if (!inetAddress.isLoopbackAddress()) {
|
String hostAddress = inetAddress.getHostAddress();
|
if (hostAddress.indexOf(':') < 0) return inetAddress;
|
}
|
}
|
}
|
} catch (SocketException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
public static String getMacAddressByWifiInfo() {
|
try {
|
final WifiManager wifi = (WifiManager) Rk3566Manager.getInstance().getContext().getApplicationContext()
|
.getSystemService(WIFI_SERVICE);
|
if (wifi != null) {
|
final WifiInfo info = wifi.getConnectionInfo();
|
if (info != null) {
|
@SuppressLint({"HardwareIds", "MissingPermission"})
|
String macAddress = info.getMacAddress();
|
if (!TextUtils.isEmpty(macAddress)) {
|
return macAddress;
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "02:00:00:00:00:00";
|
}
|
|
/**
|
* 从有线网卡获取mac
|
*
|
* @return 有线网关mac
|
*/
|
private static String getMacAddressByEthInterface() {
|
try {
|
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
|
while (nis.hasMoreElements()) {
|
NetworkInterface ni = nis.nextElement();
|
if (ni == null || !ni.getName().equalsIgnoreCase("eth0")) continue;
|
byte[] macBytes = ni.getHardwareAddress();
|
if (macBytes != null && macBytes.length > 0) {
|
StringBuilder sb = new StringBuilder();
|
for (byte b : macBytes) {
|
sb.append(String.format("%02x:", b));
|
}
|
return sb.substring(0, sb.length() - 1);
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "02:00:00:00:00:00";
|
}
|
|
|
public static String getMacAddressByNetworkInterface() {
|
try {
|
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
|
while (nis.hasMoreElements()) {
|
NetworkInterface ni = nis.nextElement();
|
if (ni == null || !ni.getName().equalsIgnoreCase("wlan0")) continue;
|
byte[] macBytes = ni.getHardwareAddress();
|
if (macBytes != null && macBytes.length > 0) {
|
StringBuilder sb = new StringBuilder();
|
for (byte b : macBytes) {
|
sb.append(String.format("%02x:", b));
|
}
|
return sb.substring(0, sb.length() - 1);
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "02:00:00:00:00:00";
|
}
|
|
private static String getMacAddressByInetAddress() {
|
try {
|
InetAddress inetAddress = getInetAddress();
|
if (inetAddress != null) {
|
NetworkInterface ni = NetworkInterface.getByInetAddress(inetAddress);
|
if (ni != null) {
|
byte[] macBytes = ni.getHardwareAddress();
|
if (macBytes != null && macBytes.length > 0) {
|
StringBuilder sb = new StringBuilder();
|
for (byte b : macBytes) {
|
sb.append(String.format("%02x:", b));
|
}
|
return sb.substring(0, sb.length() - 1);
|
}
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return "02:00:00:00:00:00";
|
}
|
|
|
private static boolean isAddressNotInExcepts(final String address, final String... excepts) {
|
if (TextUtils.isEmpty(address)) {
|
return false;
|
}
|
if ("02:00:00:00:00:00".equals(address)) {
|
return false;
|
}
|
if (excepts == null || excepts.length == 0) {
|
return true;
|
}
|
for (String filter : excepts) {
|
if (filter != null && filter.equals(address)) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
public static String getMacAddress(final String... excepts) {
|
|
String macAddress = getMacAddressByEthInterface();
|
if (isAddressNotInExcepts(macAddress, excepts)) {
|
return macAddress;
|
}
|
|
macAddress = getMacAddressByNetworkInterface();
|
if (isAddressNotInExcepts(macAddress, excepts)) {
|
return macAddress;
|
}
|
macAddress = getMacAddressByInetAddress();
|
if (isAddressNotInExcepts(macAddress, excepts)) {
|
return macAddress;
|
}
|
macAddress = getMacAddressByWifiInfo();
|
if (isAddressNotInExcepts(macAddress, excepts)) {
|
return macAddress;
|
}
|
return "";
|
}
|
|
/**
|
* 获取设备的固件系统版本
|
*
|
* @return
|
*/
|
public static String getBuilderNumberDisplay() {
|
return Build.DISPLAY;
|
}
|
|
/**
|
* 设备名
|
**/
|
public static String getDeviceName() {
|
return Build.DEVICE;
|
}
|
|
/**
|
* 获取手机Android 版本
|
*
|
* @return
|
*/
|
public static String getDeviceAndroidVersion() {
|
return Build.VERSION.RELEASE;
|
}
|
|
// /**
|
// * 获取设备序列号
|
// *
|
// * @return the serial of device
|
// */
|
// @SuppressLint("HardwareIds")
|
// public static String getDeviceSerial() {
|
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
// try {
|
// return Build.getSerial();
|
// } catch (SecurityException e) {
|
// e.printStackTrace();
|
// return "";
|
// }
|
// }
|
// return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? Build.getSerial() : Build.SERIAL;
|
// }
|
|
/**
|
* 获取手机序列号
|
*
|
* @return 手机序列号
|
*/
|
@SuppressLint({"NewApi", "MissingPermission"})
|
public static String getSerialNumber() {
|
String serial = "";
|
try {
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {//8.0+
|
serial = Build.getSerial();
|
} else {//8.0-
|
Class<?> c = Class.forName("android.os.SystemProperties");
|
Method get = c.getMethod("get", String.class);
|
serial = (String) get.invoke(c, "ro.serialno");
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return serial;
|
}
|
|
/**
|
* 获取APP版本
|
*
|
* @param context
|
* @return
|
*/
|
public static String getAppVersionName(Context context) {
|
String versionName = "0";
|
try {
|
PackageManager packageManager = context.getPackageManager();
|
PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
|
versionName = packageInfo.versionName;
|
if (TextUtils.isEmpty(versionName)) {
|
versionName = "0";
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return versionName;
|
}
|
|
/**
|
* 从系统api中获取mac
|
*
|
* @return
|
*/
|
private static String getOsMacAddress() {
|
String macAddress = OsManager.getEthMacAddress();
|
if (isAddressNotInExcepts(macAddress)) {
|
return macAddress;
|
}
|
return null;
|
}
|
|
|
/**
|
* 获取唯一码
|
*/
|
public static String getUniqueCode() {
|
return getUniqueDeviceId().replaceAll(":", "");
|
}
|
|
|
}
|