Android 获取以太网Mac地址和IP地址

来源:互联网 发布:sql的服务器名称 编辑:程序博客网 时间:2024/05/16 00:44

MAC:

private void setEthernetMac() {BufferedReader reader = null;String ethernetMac = getResources().getString(R.string.text_default_mac);try {reader = new BufferedReader(new FileReader("sys/class/net/eth0/address"));ethernetMac = reader.readLine();Log.v(TAG, "ethernetMac: " + ethernetMac);if (ethernetMac == null || ethernetMac.trim().length() == 0) {ethernetMac = getResources().getString(R.string.text_default_mac);textMac.setTextColor(Color.parseColor("#ff0000"));} else {ethernetMac = ethernetMac+ getResources().getString(R.string.text_normal);textMac.setTextColor(Color.parseColor("#00ff00"));}textMac.setText(ethernetMac);} catch (Exception e) {Log.e(TAG, "open sys/class/net/eth0/address failed : " + e);} finally {try {if (reader != null)reader.close();} catch (IOException e) {Log.e(TAG, "close sys/class/net/eth0/address failed : " + e);}}}

IP:

private String getEthernetIp() {String mEthIpAddress;if (!isUsingStaticIp()) {EthernetManager mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE);String tempIpInfo;String iface = mEthManager.getEthernetIfaceName();tempIpInfo = SystemProperties.get("dhcp." + iface + ".ipaddress");if ((tempIpInfo != null) && (!tempIpInfo.equals(""))) {mEthIpAddress = tempIpInfo;} else {mEthIpAddress = getResources().getString(R.string.text_default_ip);}return mEthIpAddress;} else {return Settings.System.getString(getContentResolver(),Settings.System.ETHERNET_STATIC_IP);}}private boolean isUsingStaticIp() {return Settings.System.getInt(getContentResolver(),Settings.System.ETHERNET_USE_STATIC_IP, 0) == 1 ? true : false;}



0 0
原创粉丝点击