我试图在不依赖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
有人可以指出这个问题的原因和相同的解决方案吗?
非常感谢任何帮助
答案 0 :(得分: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" />
答案 1 :(得分:0)
如果您的设备具有以太网硬件,则可以将/sys/class/net/eth0/address
作为文本文件阅读,此文件包含ethernet-MAC-address
eth0
接口。
示例代码可以帮助您:
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;
}
答案 2 :(得分: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();
}