简介
IMEI
IMEI(International Mobile Equipment Identity),国际移动设备身份码缩写,国际移动装备辨识码,15位数。它与每台移动电话机一一对应且全世界唯一。每移动电话机组装后都被赋予一全球唯一号码,该号码从生产到交付使用都将被制造生产厂商记录。
IMSI
国际移动用户识别码(IMSI:International Mobile Subscriber Identification
Number),区别移动用户有效信息,储存于SIM卡。总长不超15位,同样用0~9数字。其中MCC是移动用户所属国家代号,占3位,中国MCC规定460;MNC是移动网号码,两或三位数组成,中国移动的移动网络编码(MNC)00,识别移动用户所属移动通信网;MSIN是移动用户识别码,识别某移动通信网中移动用户。
MAC
MAC(Media Access Control或Medium Access Control)地址,意译媒体访问控制,或称物理地址、硬件地址,定义网络设备位置。OSI模型第三层网络层负责IP地址,第二层数据链路层负责MAC地址。一主机有一MAC地址,而每网络位有一专属它IP地址
注意:平板无法通IMEI标示设备,会将MAC地址作为用户唯一标识。
代码
MAC
package util;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
/**
* Created on 2018/6/20.
*
* @desc 手机Mac地址
*/
public class MacUtils {
private static final String DEFAULT_MAC = "02:00:00:00:00:00";
private static final String FILE_MAC = "/sys/class/net/wlan0/address";
/**
* Mac(默02:00:00:00:00:00)
*
* @param context 上下文
* @return mac
*/
@SuppressLint("HardwareIds")
public static String getMacByInterfaceOrFile(Context context) {
WifiManager wifiMan = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan != null ? wifiMan.getConnectionInfo() : null;
if (wifiInf != null && DEFAULT_MAC.equals(wifiInf.getMacAddress())) {
String result;
try {
result = getMacByInterface();
if (result != null) {
return result;
} else {
result = getMacByFile(wifiMan);
return result;
}
} catch (IOException e) {
LogUtils.e("Error getting mac");
} catch (Exception e) {
LogUtils.e("Error getting mac");
}
} else {
if (wifiInf != null && wifiInf.getMacAddress() != null) {
return wifiInf.getMacAddress();
} else {
return "";
}
}
return DEFAULT_MAC;
}
private static String getMacByInterface() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if ("wlan0".equalsIgnoreCase(nif.getName())) {
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:", b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
}
} catch (Exception e) {
LogUtils.e("Error getting mac");
}
return null;
}
private static String getMacByFile(WifiManager wifiMan) throws Exception {
String s;
int wifiState = wifiMan.getWifiState();
wifiMan.setWifiEnabled(true);
File file = new File(FILE_MAC);
FileInputStream fileInputStream = new FileInputStream(file);
s = getStringFromStream(fileInputStream);
fileInputStream.close();
boolean enabled = WifiManager.WIFI_STATE_ENABLED == wifiState;
wifiMan.setWifiEnabled(enabled);
return s;
}
private static String getStringFromStream(InputStream inputStream) throws IOException {
if (inputStream != null) {
Writer writer = new StringWriter();
char[] chars = new char[2048];
try {
Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
int counter;
while ((counter = reader.read(chars)) != -1) {
writer.write(chars, 0, counter);
}
} finally {
inputStream.close();
}
return writer.toString();
} else {
return "No Contents";
}
}
}