android机顶盒获取有线mac

直接上代码:

public static String getLocalEthernetMacAddress() {
String mac=null;
try {
Enumeration localEnumeration=NetworkInterface.getNetworkInterfaces();

while (localEnumeration.hasMoreElements()) {
NetworkInterface localNetworkInterface=(NetworkInterface) localEnumeration.nextElement();
String interfaceName=localNetworkInterface.getDisplayName();

if (interfaceName==null) {
continue;
}

if (interfaceName.equals("eth0")) {
// MACAddr = convertMac(localNetworkInterface
// .getHardwareAddress());
mac=convertToMac(localNetworkInterface.getHardwareAddress());
if (mac!=null&&mac.startsWith("0:")) {
mac="0"+mac;
}
break;
}

// byte[] address =
// localNetworkInterface.getHardwareAddress();
// Log.i(TAG, "mac=" + address.toString());
// for (int i = 0; (address != null && i < address.length);
// i++)
// {
// Log.i("Debug", String.format(" : %x", address[i]));
// }
}
} catch (SocketException e) {
e.printStackTrace();
}
return mac;
}

private static String convertToMac(byte[] mac) {
StringBuilder sb=new StringBuilder();
for (int i=0; i<mac.length; i++) {
byte b=mac[i];
int value=0;
if (b>=0&&b<=16) {
value=b;
sb.append("0"+Integer.toHexString(value));
} else if (b>16) {
value=b;
sb.append(Integer.toHexString(value));
} else {
value=256+b;
sb.append(Integer.toHexString(value));
}
if (i!=mac.length-1) {
sb.append(":");
}
}
return sb.toString();
}

无线的:
private static String getWifiMacAddr(Context context, String macAddr) {
WifiManager wifi=(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info=wifi.getConnectionInfo();
if (null!=info) {
String addr=info.getMacAddress();
if (null!=addr) {
KeelLog.d("getWifiMacAddr:"+addr);
macAddr=addr;
}
}
return macAddr;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值