Skip to content

[已解決] Android 手機的 Mac Address 顯示 02:00:00:00:00:00

問題描述

今天我在一個網路受限的環境中,需要將手機設備的 mac address 登記在 WiFi 設定中好讓自己能夠連上網路…… 但就在我確認自己手機裝置的 mac address 時,我驚訝地發現我的位址是 02:00:00:00:00:00

一開始還笑笑地跟同事說或許我是天選之人…… 但想當然爾,我很快地就發現自己的手機無法連上 WiFi 網路。經過查詢,這才發現那串 02:00:00:00:00:00 的神秘數字,算是某種程度上被屏蔽的顯示結果。


解決方法

我們需要編輯以下 3 份檔案(如果你使用 Android Studio 開啟專案,能夠在專案資料夾中找到以下三份檔案):

  • AndroidManifest.xml
  • MainActivity.java
  • activity_main.xml

首先是給予權限,在 manifests 底下找到 AndroidManifest.xml,編輯它並在 <application> 標籤前貼上以下設定:

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>


接著撰寫指定功能(得到 mac address)的程式碼。

(注意第一行的 package com.example.test 或許會因為你對專案的命名與我不同,而導致需要改寫成你自己的 package。)

package com.example.test;
//package com.example.getmacaddress;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    String mobile_mac_address;
    TextView macaddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        macaddress = (TextView)findViewById(R.id.macaddress);
        mobile_mac_address = getMacAddress();
        Log.d("MyMacIS",mobile_mac_address);
        macaddress.setText(mobile_mac_address);
    }
    public String getMacAddress(){
        try{
            List<NetworkInterface> networkInterfaceList = Collections.list(NetworkInterface.getNetworkInterfaces());
            String stringMac = "";
            for(NetworkInterface networkInterface : networkInterfaceList)
            {
                if(networkInterface.getName().equalsIgnoreCase("wlon0"));
                {
                    for(int i = 0 ;i <networkInterface.getHardwareAddress().length; i++){
                        String stringMacByte = Integer.toHexString(networkInterface.getHardwareAddress()[i]& 0xFF);
                        if(stringMacByte.length() == 1)
                        {
                            stringMacByte = "0" +stringMacByte;
                        }
                        stringMac = stringMac + stringMacByte.toUpperCase() + ":";
                    }
                    break;
                }
            }
            return stringMac;
        }catch (SocketException e)
        {
            e.printStackTrace();
        }
        return  "0";
    }
}



最後隨意拉個介面(主要是顯示 mac address),這份檔案通常是放在 res > layout 底下。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/macaddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


接著執行程式,或許你就會看到你的 mac address 出現在你的手機裝置上。

順帶一提,我的手機裝置顯示出來一個錯誤的 mac address,但是我回到設定裡面去看,發現本來顯示為 02:00:00:00:00:00 的遮蔽欄位變成了我真正的 mac address,可以參考看看。


References


Read More

Leave a Reply