Skip to content

Instantly share code, notes, and snippets.

@mr-wind
Created September 21, 2017 08:32
Show Gist options
  • Save mr-wind/c17bd87d66028a574194ddfd3d1b089f to your computer and use it in GitHub Desktop.
Save mr-wind/c17bd87d66028a574194ddfd3d1b089f to your computer and use it in GitHub Desktop.
查Ip地址和mac地址的方法
/**
* 获取手机的Mac地址,在Wifi未开启或者未连接的情况下也能获取手机Mac地址
*/
public static String getMacAddress(Context context) {
String macAddress = null;
WifiInfo wifiInfo = getWifiInfo(context);
if (wifiInfo != null) {
macAddress = wifiInfo.getMacAddress();
}
return macAddress;
}
/**
* 获取手机的Ip地址
*/
public static String getIpAddress(Context context) {
String IpAddress = null;
WifiInfo wifiInfo = getWifiInfo(context);
if (wifiInfo != null) {
IpAddress = intToIpAddress(wifiInfo.getIpAddress());
}
return IpAddress;
}
/**
* 获取WifiInfo
*/
public static WifiInfo getWifiInfo(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = null;
if (null != wifiManager) {
info = wifiManager.getConnectionInfo();
}
return info;
}
public static String intToIpAddress(long ipInt) {
StringBuffer sb = new StringBuffer();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt >> 8) & 0xFF).append(".");
sb.append((ipInt >> 16) & 0xFF).append(".");
sb.append((ipInt >> 24) & 0xFF);
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment