在Android机顶盒上 怎样获取有线网卡MAC地址?
我现在采用的办法是直接读取/sys/class/net/eth0/address文件中的数据,但是这个方法的缺点就是当用户关闭了网卡时候,eth0的目录就不存在了,程序也...
我现在采用的办法是直接读取/sys/class/net/eth0/address 文件中的数据,但是这个方法的缺点就是当用户关闭了网卡时候,eth0的目录就不存在了,程序也就不能读取mac地址了! 我想Android既然采取的是Linux内核,是否有方法可以在用户不打开网络的情况下得到eth0的MAC地址呢? 求大神指导!
展开
5个回答
展开全部
在Android机顶盒上 获取有线网卡MAC地址方法:
(1) 调用android 的API: NetworkInterface. getHardwareAddress ()
该API的level为9,只有android 2.3以上才有该接口
(2) 调用java 的方法: nbtstat/arp
一般android不支持这两个命令
(3) 调用Android的API: WifiManager
权限:
1 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses- permission>
代码:
12345 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress();
这个是设备开通Wifi连接,获取到网卡的MAC地址
(4) 调用Linux的busybox
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657/* ***************************************************************** * 子函数:获得本地MAC地址 ***************************************************************** */ public 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 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; }
这个需要设备支持busybox工具
(5)查询记录了MAC地址的文件“/proc/net/arp”
需要有这个文件,并且记录了相应的内容
(1) 调用android 的API: NetworkInterface. getHardwareAddress ()
该API的level为9,只有android 2.3以上才有该接口
(2) 调用java 的方法: nbtstat/arp
一般android不支持这两个命令
(3) 调用Android的API: WifiManager
权限:
1 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses- permission>
代码:
12345 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress();
这个是设备开通Wifi连接,获取到网卡的MAC地址
(4) 调用Linux的busybox
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657/* ***************************************************************** * 子函数:获得本地MAC地址 ***************************************************************** */ public 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 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; }
这个需要设备支持busybox工具
(5)查询记录了MAC地址的文件“/proc/net/arp”
需要有这个文件,并且记录了相应的内容
2013-07-30
展开全部
关掉wifi 然后[mw_shl_code=java,true] public String getEth0MacAddress() /* 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; }[/mw_shl_code]
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
获取方法:
打开机顶盒,找到“设置”。
打开“设置”后,选择“关于”。
在这里的“wifi地址”就是MAC地址。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-30
展开全部
没人回复啊!自己顶起来!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先关闭wifi,在运行以下java代码:
获取当前连接网络的网卡的mac地址
private static String parseByte(byte b) {
String s = "00" + Integer.toHexString(b)+":";
return s.substring(s.length() - 3);
}
/**
* 获取当前系统连接网络的网卡的mac地址
* @return
*/
@SuppressLint("NewApi")
public static final String getMac() {
byte[] mac = null;
StringBuffer sb = new StringBuffer();
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
InetAddress ip = address.nextElement();
if (ip.isAnyLocalAddress() || !(ip instanceof Inet4Address) || ip.isLoopbackAddress())
continue;
if (ip.isSiteLocalAddress())
mac = ni.getHardwareAddress();
else if (!ip.isLinkLocalAddress()) {
mac = ni.getHardwareAddress();
break;
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
if(mac != null){
for(int i=0 ;i<mac.length ;i++){
sb.append(parseByte(mac[i]));
}
return sb.substring(0, sb.length()-1);
}else{
return UpdateService.mDefaultMacAddress;
}
}
}
获取当前连接网络的网卡的mac地址
private static String parseByte(byte b) {
String s = "00" + Integer.toHexString(b)+":";
return s.substring(s.length() - 3);
}
/**
* 获取当前系统连接网络的网卡的mac地址
* @return
*/
@SuppressLint("NewApi")
public static final String getMac() {
byte[] mac = null;
StringBuffer sb = new StringBuffer();
try {
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> address = ni.getInetAddresses();
while (address.hasMoreElements()) {
InetAddress ip = address.nextElement();
if (ip.isAnyLocalAddress() || !(ip instanceof Inet4Address) || ip.isLoopbackAddress())
continue;
if (ip.isSiteLocalAddress())
mac = ni.getHardwareAddress();
else if (!ip.isLinkLocalAddress()) {
mac = ni.getHardwareAddress();
break;
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
if(mac != null){
for(int i=0 ;i<mac.length ;i++){
sb.append(parseByte(mac[i]));
}
return sb.substring(0, sb.length()-1);
}else{
return UpdateService.mDefaultMacAddress;
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询