(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>

代码:

?
1 2 3 4 5 WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress();

这个是设备开通Wifi连接,获取到网卡的MAC地址

(4) 调用Linux的busybox

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 /* ***************************************************************** * 子函数:获得本地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的网络抓包工具Tcpdump
  2. android调用系统功能
  3. Android调用js传过来的值,Android触发调起的js
  4. (Android)搭建NDK开发环境 (一)
  5. Android的Camera系统结构
  6. android命令行播放mp3
  7. Android(安卓)4.0.3 CTS 测试
  8. Android有用代码(一)
  9. Android教程之Android(安卓)SDK1.5模拟器使用命令

随机推荐

  1. Android开发(一)Android搭建、HelloWorld
  2. mono for android 百度map binding项目
  3. android 利用ksoap2方式连接webservice
  4. android EditText inputType 及 android:
  5. android文件存储
  6. android layout_gravity 和gravity
  7. Android实战【可可爱爱一零一动植物志】(
  8. 安卓017ListView & GridView & ScrollView
  9. android google地图定位开发,且可以自由移
  10. Android黑科技动态加载(四)之动态启动插