1、获取Mac地址:
在AndroidManifest.xml文件中添加权限:
获取:
public String getLocalMacAddress() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}
2、获取IP地址:
在AndroidManifest.xml文件中添加权限:
获取:
public String getLocalIpAddress() {
try {
String ipv4;
List nilist =
Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni: nilist){
List ialist = Collections.list(ni.getInetAddresses());
for (InetAddress address: ialist){
if (!address.isLoopbackAddress() &&
InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress())){
return ipv4;
}
}
}
} catch (SocketException ex) {
Log.e("exception", ex.toString());
}
return null;
}