官方博客
获取mac地址
各种ID
获取唯一性ID,各种ID的意义与限制
一.android_id
ANDROID_ID是Android系统第一次启动时产生的一个64bit(16BYTES)数,如果设备被wipe还原后,该ID将被重置(变化)。
android.provider.Settings.Secure.getString(getContext().getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
二.序列号
android.os.Build.SERIAL
三.MAC地址
/**
* 6.0以下获取mac地址
*/
public static String getMAC(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String mac = info.getMacAddress();
return mac;
}
/**
* andorid 6.0以上获取MAC地址
* @param context
* @return
*/
public static String getMacBeyondM(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(context.WIFI_SERVICE);
if(!wifi.isWifiEnabled()){
wifi.setWifiEnabled(true);
}
String macSerial = null;
String str = "";
try {
String wifiInterfaceName = SystemPropertiesProxy.get(context, "wifi.interface");
File file = new File("/sys/class/net/" + wifiInterfaceName + "/address");
boolean exist = file.exists();
Log.i("文件存在", exist + "");
InputStream inputStream = new FileInputStream(file);
InputStreamReader ir = new InputStreamReader(inputStream);
LineNumberReader input = new LineNumberReader(ir);
for (; null != str; ) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (IOException ex) {
// 赋予默认值
ex.printStackTrace();
}
Log.i("fileMac", macSerial + "");
return macSerial;
}
四.获取唯一性ID
/**
* 获取设备唯一ID
* @param context
* @return
*/
public static String getDeviceUniqID(Context context) {
android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String unique_id ;
unique_id = tm.getDeviceId();
if (TextUtils.isEmpty(unique_id)) {
unique_id=android.os.Build.SERIAL;
}
return unique_id;
}