Android关于获取到本机ip和mac地址
2016-10-15 14:45
281 查看
public static String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr .hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { ex.printStackTrace(); } return null; } // 得到本机ip地址 public static String getLocalHostIp(Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcpInfo = wifiManager.getDhcpInfo(); Log.e(TAG, "getLocalHostIp: ip Int = " + dhcpInfo.ipAddress); return FormatString(dhcpInfo.ipAddress); } //int高位放在数组低位,int低位放在数组高位 public static byte[] intToByteArray(int value) { byte[] b = new byte[4]; for (int i = 0; i < 4; i++) { int offset = (b.length - 1 - i) * 8; b[i] = (byte) ((value >>> offset) & 0xFF); } return b; } public static String FormatString(int value) { String strValue = ""; byte[] ary = intToByteArray(value); for (int i = ary.length - 1; i >= 0; i--) { Log.e(TAG, "FormatString: ary[" + i + "] = " + ary[i] + " (ary[" + i + "] & 0xFF) = " + (ary[i] & 0xFF)); strValue += (ary[i] & 0xFF); if (i > 0) { strValue += "."; } } return strValue; } // 得到本机Mac地址 public static String getLocalMac(Context context) { WifiManager wifiMng = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfor = wifiMng.getConnectionInfo(); return wifiInfor.getMacAddress(); }
相关文章推荐
- Android中Wifi未开启情况下获取Mac地址和IP
- Android获取Ethernet、WIFI的ip和mac地址
- Android 在wifi下获取IP和MAC地址
- Android -- 获取IP和MAC地址
- Android中Wifi未开启情况下获取Mac地址和IP
- Android获取设备型号和系统版本号、mac地址、IP、屏幕的分辨率
- android获取Mac地址和IP 地址
- Android系统下获取本地IP和mac地址的方法
- 关于android 获取mac地址
- 关于获取设备的唯一设备ID,MAC地址 IOS/Android问题
- Android 获取Mac地址和IP
- android-关于socket编程(附上如何获取安卓机的ip)
- Android中Wifi未开启情况下获取Mac地址和IP
- Android_获取手机IP和MAC地址
- (android)通过wifiManager获取关于wifi的ip,dns....
- Android中Wifi未开启情况下获取Mac地址和IP
- 获取机器IP和MAC地址
- 获取本机IP和mac地址
- 关于android如何获取屏幕分辨率的例子
- android 关于获取焦点问题