把已经得到的信息的方法附上,都是从别人那里整理的,验证过木有问题。
下面附上获取mac地址,以太网下获取IP的方法以及wifi下获取网络信息的方法。
获取Mac地址:
public static String getMacAddress(){
try {
return loadFileAsString(“/sys/class/net/eth0/address”)
.toUpperCase().substring(0, 17);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
以太网下获取IP:
public static String getLocalIpAddress() {
try {
for (Enumeration en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(“WifiPreference IpAddress”, ex.toString());
}
return null;
}
wifi下获取相关网络信息:
wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
d = wm.getDhcpInfo();
//将地址码转换成字符串类型
public String FormatString(int value){
String strValue=””;
byte[] ary =intToByteArray(value);
for(int i=ary.length-1;i>=0;i–){
strValue+=(ary & 0xFF);
if(i>0){
strValue+=”.”;
}
}
return strValue;
}
public byte[] intToByteArray(int value){
byte[] b = new byte[4];
for (int i = 0; i
int offset = (b.length – 1 – i) * 8;
b = (byte) ((value >>> offset) & 0xFF);
}
return b;
}
IP:FormatString(d.ipAddress).toString();
默认网关:FormatString(d.gateway).toString();
子网掩码:FormatString(d.netmask).toString();