android设备无线、有线网络下获取MAC地址
2017-05-16 15:32
941 查看
public static String getLocalMacIdFromIp(){ String strMacAddr = ""; try { InetAddress ip = getLocalInetAddress(); byte[] b = NetworkInterface.getByInetAddress(ip) .getHardwareAddress(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < b.length; i++) { if (i != 0) { buffer.append(':'); } String str = Integer.toHexString(b[i]&0xFF); buffer.append(str.length() == 1 ? 0 + str : str); } strMacAddr = buffer.toString().toLowerCase(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return strMacAddr; } /** * 获取设备本地IP */ protected static InetAddress getLocalInetAddress() { InetAddress ip = null; try { //列举 Enumeration en_netInterface = NetworkInterface.getNetworkInterfaces(); while (en_netInterface.hasMoreElements()) {//是否还有元素 NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一个元素 Enumeration en_ip = ni.getInetAddresses();//得到一个ip地址的列举 while (en_ip.hasMoreElements()) { ip = (InetAddress) en_ip.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) break; else ip = null; } if (ip != null) { break; } } } catch (SocketException e) { e.printStackTrace(); } return ip; }
亲测有效
相关文章推荐
- 获取android 设备的cpu gpu信息 mac地址
- 关于android设备wifi-mac地址的获取
- android 获取设备的IP和Mac地址
- android TV 获取有线mac和无线mac
- android 设备唯一码的获取,Cpu号,Mac地址
- android 获取设备IP和Mac地址
- Android中获取设备IP、MAC地址
- android 设备唯一码的获取,Cpu号,Mac地址
- 获取局域网内所有网络设备MAC地址
- android 获取设备信息的IP地址和Mac地址—亲测无误!!
- android 设备唯一码的获取,Cpu号,Mac地址
- android 设备唯一码的获取,Cpu号,Mac地址
- android 设备唯一码的获取,Cpu号,Mac地址
- android 获取设备IP和Mac地址
- 获取 android 设备 mac 地址的方法
- android设备获取当前所用网络类型和获取手机ip地址
- Linux下如何获取网络设备MAC地址
- Android: 用jni 获取MAC地址
- Android 获取无线蓝牙MAC信息代码
- android:获取本机Mac地址及IP地址方法