public class MacUtils {
/**
* 获取手机MAC地址。
*
* @param mContext
* @return MAC地址
*/
public static String getMacAddress(Context mContext) {
WifiManager wm = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wi = wm.getConnectionInfo();
return wi.getMacAddress();
}
/**
* 获取用户IP地址。
*
* @param mContext
* @return IP地址
*/
public static String getIpAddress(Context mContext) {
WifiManager wm = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wm.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
if (ipAddress == 0)
return "0";
return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
+ (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
}
}
/**
* 获取手机MAC地址。
*
* @param mContext
* @return MAC地址
*/
public static String getMacAddress(Context mContext) {
WifiManager wm = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wi = wm.getConnectionInfo();
return wi.getMacAddress();
}
/**
* 获取用户IP地址。
*
* @param mContext
* @return IP地址
*/
public static String getIpAddress(Context mContext) {
WifiManager wm = (WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wm.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
if (ipAddress == 0)
return "0";
return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."
+ (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
}
}
本文介绍如何在Android应用中通过WifiManager接口获取设备的MAC地址和IP地址,详细步骤包括初始化WifiManager、获取连接信息以及解析MAC和IP地址。
6715

被折叠的 条评论
为什么被折叠?



