读取网络MAC地址

//读取以太网MAC在目录cat /sys/class/net/eht0/address

//读取wifi或当前网络连接MAC地址:cat /sys/class/net/wlan0/address

public static String getMac() {

String macSerial = null;
try {
Process pp = Runtime.getRuntime().exec(
"cat /sys/class/net/eht0/address");
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String str = input.readLine();
if (str != null) {
macSerial = str.trim();// 去空格
}


} catch (IOException ex) {
// 赋予默认值
macSerial = "00:00:00:00";
ex.printStackTrace();
}
return macSerial.replace(":", "");

}

// 也可以通过linux命令来获取

/*
* public static String getMacAddress() { String result = ""; String Mac =
* ""; result = callCmd("busybox ifconfig", "HWaddr");

* // 如果返回的result == null,则说明网络不可取 if (result == null) { return
* "网络出错,请检查网络"; }

* // 对该行数据进行解析 // 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67 if
* (result.length() > 0 && result.contains("HWaddr") == true) { Mac =
* result.substring(result.indexOf("HWaddr") + 6, result.length() - 1);
* Log.i("test", "Mac:" + Mac + " Mac.length: " + Mac.length());

* if (Mac.length() > 1) { Mac = Mac.replaceAll(" ", ""); result = "";
* String[] tmp = Mac.split(":"); for (int i = 0; i < tmp.length; ++i) {
* result += tmp[i]; } } Log.i("test", result + " result.length: " +
* result.length()); } return result; }

* public static String callCmd(String cmd, String filter) { String result =
* ""; String line = ""; try { Process proc =
* Runtime.getRuntime().exec(cmd); InputStreamReader is = new
* InputStreamReader(proc.getInputStream()); BufferedReader br = new
* BufferedReader(is);

* // 执行命令cmd,只取结果中含有filter的这一行 while ((line = br.readLine()) != null &&
* line.contains(filter) == false) { // result += line; Log.i("test",
* "line: " + line); }

* result = line; Log.i("test", "result: " + result); } catch (Exception e)
* { e.printStackTrace(); } return result; }
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值