Android获取mac地址
2015-07-07 10:32
561 查看
原文地址:http://www.cnblogs.com/Amandaliu/archive/2012/12/04/2802322.html
这个是需要设备开通Wifi连接,获取到网卡的MAC地址
另,贴上,判断当前是否为wifi连接方式:
(4) 调用Linux的busybox
其他相关函数:
这个需要设备支持busybox工具。现在发现一些设备是没有安装该工具的,这时使用该方法,会报错。
(5)查询记录了MAC地址的文件“/proc/net/arp”
需要有这个文件,并且记录了相应的内容
记录了MAC地址的文件“/proc/net/arp”内容大致如下:
IP address HW type Flags HW address Mask Device
10.63.253.193 0x1 0x2 00:11:92:06:85:3f * eth0
10.63.253.194 0x1 0x2 00:11:92:06:85:3a * eth1
10.63.253.195 0x1 0x2 00:11:92:06:85:3b * eth2
esult = wifiInfo.getMacAddress(); Log.i(TAG_NETWORK,"Mac address(wifi): "+mResult); return mResult; }
这个是需要设备开通Wifi连接,获取到网卡的MAC地址
另,贴上,判断当前是否为wifi连接方式:
//判断当前是否使用wifi连接 private static boolean isWifiConnected(Context context){ ConnectivityManager cm; cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); boolean result = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ? true : false ; Log.i(TAG_NETWORK,"isWifiConnected: "+result); return result; }
(4) 调用Linux的busybox
/** * 通过callCmd("busybox ifconfig","HWaddr")获取mac地址 * @attention 需要设备装有busybox工具 * @return Mac Address */ private static String getMacFromCallCmd(){ String result = ""; result = callCmd("busybox ifconfig","HWaddr"); if(result == null || result.length() <= 0){ Log.i(TAG_NETWORK,"callCmd returns null or empty"); return null; } //对该行数据进行解析 //例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67 if(result.length()>0 && result.contains("HWaddr")==true){ String Mac = result.substring(result.indexOf("HWaddr")+6, result.length()-1); Log.i(TAG_NETWORK,"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(TAG_NETWORK,result+" result.length: "+result.length()); } Log.i(TAG_NETWORK,"Mac address(CallCmd): "+result); 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; }
这个需要设备支持busybox工具。现在发现一些设备是没有安装该工具的,这时使用该方法,会报错。
(5)查询记录了MAC地址的文件“/proc/net/arp”
需要有这个文件,并且记录了相应的内容
/** * get the Mac Address from the file /proc/net/arp * @param context * @attention the file /proc/net/arp need exit * @return Mac Address */ private static String getMacFromFile(Context context){ String mIP = Config.getIpAddress(context); if(mIP == null || mIP.length()<=0) return null; List<String> mResult = readFileLines("/proc/net/arp"); Log.d(TAG_NETWORK,"======= /proc/net/arp ========="); for(int i =0;i<mResult.size();++i) Log.d("line",mResult.get(i)); Log.d(TAG_NETWORK,"==========================="); if(mResult !=null && mResult.size()>1){ for(int j =1;j<mResult.size();++j){ List<String> mList = new ArrayList<String>(); String[] mType = mResult.get(j).split(" "); for(int i =0;i<mType.length;++i){ if(mType[i]!=null && mType[i].length()>0) mList.add(mType[i]); } if(mList!=null && mList.size()>4 && mList.get(0).equalsIgnoreCase(mIP)){ String result=""; String[] tmp = mList.get(3).split(":"); for(int i = 0;i<tmp.length;++i){ result +=tmp[i]; } result = result.toUpperCase(); Log.i(TAG_NETWORK,"Mac address(file): "+result); return result; } } } return null; }
/** * 以行为单位读取文件,常用于读面向行的格式化文件 */ private static List<String> readFileLines(String fileName) { File file = new File(fileName); BufferedReader reader = null; String tempString =""; List<String> mResult = new ArrayList<String>(); try { Log.i("result","以行为单位读取文件内容,一次读一整行:"); reader = new BufferedReader(new FileReader(file)); while((tempString = reader.readLine())!=null){ mResult.add(tempString); } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } return mResult; }
记录了MAC地址的文件“/proc/net/arp”内容大致如下:
IP address HW type Flags HW address Mask Device
10.63.253.193 0x1 0x2 00:11:92:06:85:3f * eth0
10.63.253.194 0x1 0x2 00:11:92:06:85:3a * eth1
10.63.253.195 0x1 0x2 00:11:92:06:85:3b * eth2
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories