最近项目中用到获取手机IMEI,IMSI以及MAC,在此记录一下,方便你我他。。。
那么什么时候会用到这些东西呢?
LZ 个人认为,如果项目当中需要对当前使用用户设备做唯一标识时(证明这个是你用的)可以使用这种方式。当然了这也是LZ个人的一点理解,欢迎大家打脸~
那么首先简单普及一下这三个分别都是什么鬼。。。
1. IMEI
IMEI(International Mobile Equipment Identity)是国际移动设备身份码的缩写,国际移动装备辨识码,是由15位数字组成的”电子串号”,它与每台移动电话机一一对应,而且该码是全世界唯一的。每一只移动电话机在组装完成后都将被赋予一个全球唯一的一组号码,这个号码从生产到交付使用都将被制造生产的厂商所记录。
PS:通俗来讲就是标识你当前设备(手机)全世界唯一,类似于个人身份证,这个肯定唯一啦~
2. IMSI
国际移动用户识别码(IMSI:International Mobile Subscriber Identification
Number)是区别移动用户的标志,储存在SIM卡中,可用于区别移动用户的有效信息。其总长度不超过15位,同样使用0~9的数字。其中MCC是移动用户所属国家代号,占3位数字,中国的MCC规定为460;MNC是移动网号码,由两位或者三位数字组成,中国移动的移动网络编码(MNC)为00;用于识别移动用户所归属的移动通信网;MSIN是移动用户识别码,用以识别某一移动通信网中的移动用户
PS:通俗来讲就是标识你当前SIM卡(手机卡)唯一,同样类似于个人身份证,肯定唯一啦~
3. MAC
MAC(Media Access Control或者Medium Access Control)地址,意译为媒体访问控制,或称为物理地址、硬件地址,用来定义网络设备的位置。在OSI模型中,第三层网络层负责 IP地址,第二层数据链路层则负责 MAC地址。因此一个主机会有一个MAC地址,而每个网络位置会有一个专属于它的IP地址
PS:通俗来讲就是标识你当前使用我这个软件(功能)时的地址,方便在你干坏事的时候警察叔叔抓你~
最主要的是:在平板设备上,无法通过imei标示设备,我们会将mac地址作为用户的唯一标识
好啦,下面贴出获取这三项的代码。。。
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
/**
* 获取手机信息工具类
*
* @author HLQ
* @createtime 2016-12-7下午2:06:03
* @remarks
*/
public class MobileInfoUtil {
/**
* 获取手机IMEI
*
* @param context
* @return
*/
public static final String getIMEI(Context context) {
try {
//实例化TelephonyManager对象
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//获取IMEI号
String imei = telephonyManager.getDeviceId();
//在次做个验证,也不是什么时候都能获取到的啊
if (imei == null) {
imei = "";
}
return imei;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 获取手机IMSI
*/
public static String getIMSI(Context context){
try {
TelephonyManager telephonyManager=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//获取IMSI号
String imsi=telephonyManager.getSubscriberId();
if(null==imsi){
imsi="";
}
return imsi;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
2017年6月10日00:26:48 删除之前获取MAC地址方法,重新更新一下:
哎,有时候这活干的真是心累,没辙~按着自己的方向继续前行吧~
今天,突然给我发个链接,说什么Android 6.0获取MAC地址,返回的都是02:00:00:00:00:00。
突然一蒙圈,然后回过头看看以前提交的数据,日了狗,还真是!!!
经过排查,发现了下面一些结论,了解了解:
从Android 6.0之后,android 移除了通过 WiFi 和蓝牙 API 来在应用程序中可编程的访问本地硬件标示符。现在 WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法都将返回 02:00:00:00:00:00 。
2018年1月17日14:04:09更新:
产品,后台反馈,说部分手机或存在获取不到Mac的情况。再仔细询问后,发现获取不到的有如下特征:
- 当前网络环境为4G
然而经过LZ在公司打死搜索Android机型测试后发现如下几个奇葩无奈的事儿:
部分设备在WIFI、4G以及无网络的情况可以正常获取Mac;
较多设备仅仅支持在WIFI情况下可以获取。
针对以上问题,LZ有如下猜(xiang)测(fa):
既然在WIFI状态下正常获取,那么如果我4G情况下打开WIFI,可以获取到么?
- 1
针对以上想法,LZ专门做了一个测试,测试结果,很nice~
果然,4G情况下打开WIFI即可获取到正常的MAC!
下面附上本次更新代码:
首先,需要如下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
- 1
- 2
- 3
- 4
其次LZ简单搞了一个工具类:
package com.hlq.mac;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
/**
* author : HLQ
* e-mail : 925954424@qq.com
* time : 2018/01/17
* desc : 获取mac 兼容6.0获取 以及4g环境下获取失败
* version: 1.0
*/
public class MacUtils {
/**
* 获取失败默认返回值
*/
public static final String ERROR_MAC_STR = "02:00:00:00:00:00";
// Wifi 管理器
private static WifiManager mWifiManager;
/**
* 实例化WifiManager对象
*
* @param context 当前上下文对象
* @return
*/
private static WifiManager getInstant(Context context) {
if (mWifiManager == null) {
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
}
return mWifiManager;
}
/**
* 开启wifi
*/
public static void getStartWifiEnabled() {
// 判断当前wifi状态是否为开启状态
if (!mWifiManager.isWifiEnabled()) {
// 打开wifi 有些设备需要授权
mWifiManager.setWifiEnabled(true);
}
}
/**
* 获取手机设备MAC地址
* MAC地址:物理地址、硬件地址,用来定义网络设备的位置
* modify by heliquan at 2018年1月17日
*
* @param context
* @return
*/
public static String getMobileMAC(Context context) {
mWifiManager = getInstant(context);
// 如果当前设备系统大于等于6.0 使用下面的方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getAndroidHighVersionMac();
} else { // 当前设备在6.0以下
return getAndroidLowVersionMac(mWifiManager);
}
}
/**
* Android 6.0 设备兼容获取mac
* 兼容原因:从Android 6.0之后,Android 移除了通过WiFi和蓝牙API来在应用程序中可编程的访问本地硬件标示符。
* 现在WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法都将返回:02:00:00:00:00:00
*
* @return
*/
public static String getAndroidHighVersionMac() {
String str = "";
String macSerial = "";
try {
// 由于Android底层基于Linux系统 可以根据shell获取
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/wlan0/address ");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for (; null != str; ) {
str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (macSerial == null || "".equals(macSerial)) {
try {
return loadFileAsString("/sys/class/net/eth0/address")
.toUpperCase().substring(0, 17);
} catch (Exception e) {
e.printStackTrace();
macSerial = getAndroidVersion7MAC();
}
}
return macSerial;
}
/**
* Android 6.0 以下设备获取mac地址 获取失败默认返回:02:00:00:00:00:00
*
* @param wifiManager
* @return
*/
@NonNull
private static String getAndroidLowVersionMac(WifiManager wifiManager) {
try {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String mac = wifiInfo.getMacAddress();
if (TextUtils.isEmpty(mac)) {
return ERROR_MAC_STR;
} else {
return mac;
}
} catch (Exception e) {
e.printStackTrace();
Log.e("mac", "get android low version mac error:" + e.getMessage());
return ERROR_MAC_STR;
}
}
/**
* 兼容7.0获取不到的问题
*
* @return
*/
public static String getAndroidVersion7MAC() {
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
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) {
Log.e("mac", "get android version 7.0 mac error:" + e.getMessage());
}
return ERROR_MAC_STR;
}
public static String loadFileAsString(String fileName) throws Exception {
FileReader reader = new FileReader(fileName);
String text = loadReaderAsString(reader);
reader.close();
return text;
}
public static String loadReaderAsString(Reader reader) throws Exception {
StringBuilder builder = new StringBuilder();
char[] buffer = new char[4096];
int readLength = reader.read(buffer);
while (readLength >= 0) {
builder.append(buffer, 0, readLength);
readLength = reader.read(buffer);
}
return builder.toString();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
这里需要注意的是,最好在程序启动的时候进行首次获取,当然不是非必需,看各位具体使用场景吧。LZ这里放置在BaseApplication之中,如果首次获取到的是默认提供的02:00:00:00:00:00,则需要开启WIFI,当然这里有很多优化的地方,时间紧迫,先这么来~
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
String strMa = MacUtils.getMobileMAC(getApplicationContext());
if (MacUtils.ERROR_MAC_STR.equals(strMa)) {
Toast.makeText(this, "请授予开启wifi权限 以保证正常获取mac", Toast.LENGTH_SHORT).show();
MacUtils.getStartWifiEnabled();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
之后直接获取即可:
public void getMACInfoLG(View view) {
((Button) view).setText("获取到MAC地址为:" + MacUtils.getMobileMAC(this));
}
- 1
- 2
- 3
Q:为什么要首次运行来一次获取呢?
A:因为在目前的测试得出的结论来看,在WIFI下几乎手机都能获取到,如果首次获取不到,也就是默认为02:00:00:00:00:00的时候,打开WIFI(因为不是一打开就能立刻获取到),稍等片刻即可获取。
下面附上效果图:
补充
还有老铁给LZ提供了另外一种获取MAC地址的方式,但是在实际测试中,有的设备还是需要打开WIFI,下面附上源码:
/**
* 根据IP地址获取MAC地址
*
* @return
*/
private static String getLocalMacAddressFromIp() {
String strMacAddr = null;
try {
//获得IpD地址
InetAddress ip = getLocalInetAddress();
byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; i++) {
if (i != 0) {
buffer.append(':');
}
String str = Integer.toHexString(b[i] & 0xFF);
buffer.append(str.length() == 1 ? 0 + str : str);
}
strMacAddr = buffer.toString().toUpperCase();
} catch (Exception e) {
}
return strMacAddr;
}
/**
* 获取移动设备本地IP
*
* @return
*/
private static InetAddress getLocalInetAddress() {
InetAddress ip = null;
try {
//列举
Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
while (en_netInterface.hasMoreElements()) {//是否还有元素
NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一个元素
Enumeration<InetAddress> en_ip = ni.getInetAddresses();//得到一个ip地址的列举
while (en_ip.hasMoreElements()) {
ip = en_ip.nextElement();
if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
break;
else
ip = null;
}
if (ip != null) {
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
结束
感谢各位老铁相助~
发布文章有缺陷发现后一定及时更新,以免误导~
感谢小伙伴~、
文章来源网络:点击这里查看源文.