以太网MAC地址获得 android
16lz
2021-01-26
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
protected InetAddress getLocalInetAddress() { InetAddress ip = null; try { Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces(); while (en_netInterface.hasMoreElements()) { NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement(); Enumeration<InetAddress> en_ip = ni.getInetAddresses(); while (en_ip.hasMoreElements()) { ip = en_ip.nextElement(); if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1) break; else ip = null; } if (ip != null) { break; } } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ip; } protected String getMacAddress() /* throws UnknownHostException */{ String strMacAddr = null; 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().toUpperCase(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return strMacAddr; }
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer