1

我正在尝试获取我的 android 设备的 MAC 地址而不依赖于myWifiInfo.getMacAddress()

以下是我使用的代码:

       try{
            InetAddress inet = InetAddress.getLocalHost();
            NetworkInterface ni = NetworkInterface.getByInetAddress(inet);
            byte[] address = ni.getHardwareAddress();
            }
        catch(Exception e){
            Log.d("MyActivity",e.toString());
        }

我得到以下异常:

08-01 06:10:56.239: WARN/System.err(23164): at   java.net.NetworkInterface.rethrowAsSocketException(NetworkInterface.java:212)
08-01 06:10:56.239: WARN/System.err(23164): at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:178)
08-01 06:10:56.239: WARN/System.err(23164): at java.net.NetworkInterface.getByName(NetworkInterface.java:118)
08-01 06:10:56.239: WARN/System.err(23164): at java.net.NetworkInterface.getNetworkInterfacesList(NetworkInterface.java:270)
08-01 06:10:56.239: WARN/System.err(23164): at java.net.NetworkInterface.getByInetAddress(NetworkInterface.java:228)
08-01 06:10:56.239: WARN/System.err(23164): at com.example.MyActivity$MyAsyncTask.doInBackground(MyActivity.java:82)
08-01 06:10:56.247: WARN/System.err(23164): at com.example.MyActivity$MyAsyncTask.doInBackground(MyActivity.java:43)
08-01 06:10:56.247: WARN/System.err(23164): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-01 06:10:56.247: WARN/System.err(23164): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-01 06:10:56.247: WARN/System.err(23164): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-01 06:10:56.247: WARN/System.err(23164): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
08-01 06:10:56.247: WARN/System.err(23164): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-01 06:10:56.247: WARN/System.err(23164): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-01 06:10:56.247: WARN/System.err(23164): at java.lang.Thread.run(Thread.java:856)
08-01 06:10:56.247: WARN/System.err(23164): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
08-01 06:10:56.247: WARN/System.err(23164): at libcore.io.Posix.socket(Native Method)
08-01 06:10:56.247: WARN/System.err(23164): at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
08-01 06:10:56.247: WARN/System.err(23164): at java.net.NetworkInterface.collectIpv4Address(NetworkInterface.java:163)                       
08-01 06:10:56.247: WARN/System.err(23164): ... 12 more

我已给予许可

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

异常是从上面代码中的以下行引发的:

NetworkInterface ni = NetworkInterface.getByInetAddress(inet);

当我记录 inet 地址时,我得到的结果与localhost/127.0.0.1

有人可以指出这个问题的原因和解决方案吗?

任何帮助深表感谢

4

3 回答 3

1

您也可以尝试按照蓝牙 API 代码获取 MAC 地址,

private BluetoothAdapter btAdapther; 

mBtAdapter = BluetoothAdapter.getDefaultAdapter();
String deviceMacAddress = mBtAdapter.getAddress();

AndroidManifest.xml 中的权限

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
于 2012-08-01T06:46:27.250 回答
0

如果您的设备具有以太网硬件,您可以读取/sys/class/net/eth0/address为文本文件,并且该文件包含接口。ethernet-MAC-addresseth0

示例代码可以帮助您:

public String getMac()
{
    StringBuffer fileData = new StringBuffer(1000);
    try {
        BufferedReader reader = new BufferedReader(new FileReader("/sys/class/net/eth0/address"));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
于 2012-08-06T06:17:06.780 回答
0
public String getMacAddressFromEtcFile(){
        try {
            return loadFileAsString("/sys/class/net/eth0/address").toUpperCase().substring(0, 17);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


 public static String loadFileAsString(String filePath) throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead = 0;
        while((numRead = reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    }
于 2012-08-06T06:27:03.913 回答