手机识别码

简介

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 ControlMedium 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";
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

snpmyn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值