我正在使用安卓和Api level 8,我想要获取我的以太网接口(eth0)的地址。
在API8级上,NetworkInterface类没有getHardwareAddress()函数。WifiManager也无法正常工作,因为这不是无线接口。
提前感谢!
发布于 2011-09-07 21:36:34
假设您的以太网接口是eth0,请尝试打开并读取文件/sys/class/net/eth0/address。
发布于 2011-09-09 22:19:23
这是我基于Joel F答案的解决方案。希望它能帮助一些人!
/*
* Load file content to String
*/
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();
}
/*
* Get the STB MacAddress
*/
public String getMacAddress(){
try {
return loadFileAsString("/sys/class/net/eth0/address")
.toUpperCase().substring(0, 17);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}发布于 2016-03-31 16:09:20
使用java的这种方式来修复它;也许可以帮助您。
NetworkInterface netf = NetworkInterface.getByName("eth0");
byte[] array = netf.getHardwareAddress();
StringBuilder stringBuilder = new StringBuilder("");
String str = "";
for (int i = 0; i < array.length; i++) {
int v = array[i] & 0xFF;
String hv = Integer.toHexString(v).toUpperCase();
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv).append("-");
}
str = stringBuilder.substring(0, stringBuilder.length()- 1);https://stackoverflow.com/questions/7332931
复制相似问题