我正在尝试获取我的 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
有人可以指出这个问题的原因和解决方案吗?
任何帮助深表感谢