獲取android手機(jī)mac地址

android系統(tǒng)6.0之后,對mac地址的獲取添加了權(quán)限,按照普通的方法獲取如下:

WifiManager  wifiMan = (WifiManager)context.getSystemService(Context.WIFI_SERVICE) ;
WifiInfo wifiInf = wifiMan.getConnectionInfo();
return wifiInf.getMacAddress();

但是這種情況,獲取到的mac地址都是:02:00:00:00:00:00,看來就需要另一種方法來獲取mac地址:

private static final String marshmallowMacAddress = "02:00:00:00:00:00";
  private static final String fileAddressMac = "/sys/class/net/wlan0/address";

  public static String getAdresseMAC(Context context) {
    WifiManager wifiMan = (WifiManager)context.getSystemService(Context.WIFI_SERVICE) ;
    WifiInfo wifiInf = wifiMan.getConnectionInfo();

    if(wifiInf !=null && marshmallowMacAddress.equals(wifiInf.getMacAddress())){
      String result = null;
      try {
        result= getAdressMacByInterface();
        if (result != null){
          return result;
        } else {
          result = getAddressMacByFile(wifiMan);
          return result;
        }
      } catch (IOException e) {
        Log.e("MobileAccess", "Erreur lecture propriete Adresse MAC");
      } catch (Exception e) {
        Log.e("MobileAcces", "Erreur lecture propriete Adresse MAC ");
      }
    } else{
      if (wifiInf != null && wifiInf.getMacAddress() != null) {
        return wifiInf.getMacAddress();
      } else {
        return "";
      }
    }
    return marshmallowMacAddress;
  }

接下來,來分析一下其中兩個(gè)重要的方法
1.getAdressMacByInterface()

 private static String getAdressMacByInterface(){
    try {
      List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
      for (NetworkInterface nif : all) {
        if (nif.getName().equalsIgnoreCase("wlan0")) {
          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("MobileAcces", "Erreur lecture propriete Adresse MAC ");
    }
    return null;
  }

2.getAddressMacByFile(wifiMan)

  private static String getAddressMacByFile(WifiManager wifiMan) throws Exception {
    String ret;
    int wifiState = wifiMan.getWifiState();

    wifiMan.setWifiEnabled(true);
    File fl = new File(fileAddressMac);
    FileInputStream fin = new FileInputStream(fl);
    ret = crunchifyGetStringFromStream(fin);
    fin.close();

    boolean enabled = WifiManager.WIFI_STATE_ENABLED == wifiState;
    wifiMan.setWifiEnabled(enabled);
    return ret;
  }

  private static String crunchifyGetStringFromStream(InputStream crunchifyStream) throws IOException {
    if (crunchifyStream != null) {
      Writer crunchifyWriter = new StringWriter();

      char[] crunchifyBuffer = new char[2048];
      try {
        Reader crunchifyReader = new BufferedReader(new InputStreamReader(crunchifyStream, "UTF-8"));
        int counter;
        while ((counter = crunchifyReader.read(crunchifyBuffer)) != -1) {
          crunchifyWriter.write(crunchifyBuffer, 0, counter);
        }
      } finally {
        crunchifyStream.close();
      }
      return crunchifyWriter.toString();
    } else {
      return "No Contents";
    }
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評論 25 709
  • 國家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說閱讀 12,410評論 6 13
  • -夢 昨天我夢見了她,或許應(yīng)該感恩,因?yàn)樵谖医酉聛碛邢薜纳?,或許連夢到她都是奢侈,都會(huì)越來越少,夢里的她還是依...
    oo上海閱讀 260評論 0 0
  • 姻緣天注定在中國唐朝有一個(gè)非常有名的故事,足以代表。 杜陵的韋固自小失去雙親,想早點(diǎn)結(jié)婚,但多次求婚都不成。唐太宗...
    運(yùn)安閣閣主閱讀 528評論 0 0
  • 本文小計(jì)321字,需耗費(fèi)你約1分29秒才能看完,我用時(shí)光為你篩選真愛,而那漏走的將成為回憶。 念你到時(shí)光蒼老 我變...
    輝輝大詩閱讀 188評論 0 0

友情鏈接更多精彩內(nèi)容