下面这个工具类提供了一些方法,可以获取设备AndroidID、IMEI、序列号、以及由他们组成的唯一识别码、网卡的MAC地址、蓝牙MAC地址。代码如下:
DeviceInfoUtil:
package com.home;
import java.util.UUID;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
public class DeviceInfoUtil {
/**
* 得到设备AndroidID,需要设备添加 Google账户
*
* @param context
* @return
*/
public static String getAndroidID(Context context) {
String androidID = Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
return androidID;
}
/**
* 得到设备IMEI值
*
* @param context
* @return
*/
public static String getIMEI(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getDeviceId();
}
/**
* 得到设备序列号
*
* @param context
* @return
*/
public static String getSimSerialNumber(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
return tm.getSimSerialNumber();
}
/**
* 得到设备唯一识别码
*
* @param context
* @return
*/
public static String getUniqueNumber(Context context) {
String androidID = getAndroidID(context);
String imei = getIMEI(context);
String simSerialNumber = getSimSerialNumber(context);
UUID uuid = new UUID(androidID.hashCode(),
((long) imei.hashCode() <<32) | simSerialNumber.hashCode());
return uuid.toString();
}
/**
* 获取设备本身网卡的MAC地址
*
* &#64;param context
* &#64;return
*/
public static String getWLANMACAddress(Context context) {
String macAddress &#61; "";
WifiManager wm &#61; (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo info &#61; wm.getConnectionInfo();
if (info !&#61; null) {
macAddress &#61; info.getMacAddress();
} else {
macAddress &#61; "No Wifi Device";
}
return macAddress;
}
/**
* 获取蓝牙MAC地址
*
* &#64;param context
* &#64;return
*/
public static String getBluetoothMACAddress(Context context) {
String btMacAddress &#61; "";
BluetoothAdapter ba &#61; BluetoothAdapter.getDefaultAdapter();
if (ba !&#61; null) {
if (!ba.isEnabled()) {
btMacAddress &#61; "Bluetooth not open";
// 启动蓝牙
Intent in &#61; new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
context.startActivity(in);
} else {
btMacAddress &#61; ba.getAddress();
}
} else {
btMacAddress &#61; "No Bluetooth Device";
}
return btMacAddress;
}
}
权限&#xff1a;