标签:android style blog class code java tar ext javascript color get
由于WiFi 的Mac address 是一个被动咨询。一般在开机后,不会主动上到的系统里。要等待WiFi硬件启动后,才会把有关Mac address资料记载入系统去。 所以一般android系统获取Mac address时候会出现以下几种情况:
1、在WiFi打开的时候可以获取到Mac地址
2、关闭WiFi,不重启系统,也可以获取到Mac address
3、系统重启后,不打开WiFi,获取不到Mac address
4、系统重启后,不开开WiFi,打开2G、3G网络,获取不到Mac address
要获取Mac address :1、等待WiFi启动
2、在程序中主动启动WiFi
第二种情况代码如下:
1 public String macAddress = null; 2 3 public void getWifiMacAddress(Context context) { 4 final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 5 if (wifi == null) 6 return; 7 WifiInfo info = wifi.getConnectionInfo(); 8 this.macAddress = info.getMacAddress(); 9 10 if (this.macAddress == null && !wifi.isWifiEnabled()) { 11 new Thread() { 12 @Override 13 public void run() { 14 wifi.setWifiEnabled(true); 15 for (int i = 0; i < 10; i++) { 16 WifiInfo _info = wifi.getConnectionInfo(); 17 macAddress = _info.getMacAddress(); 18 if (macAddress != null) 19 break; 20 try { 21 Thread.sleep(500); 22 } catch (InterruptedException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } 26 } 27 wifi.setWifiEnabled(false); 28 } 29 }.start(); 30 31 System.out.println("getmac:" + macAddress); 32 } 33 }
AndroidManifest.xml 中设置一下权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
android 获取MAC地址,码迷,mamicode.com
标签:android style blog class code java tar ext javascript color get
原文地址:http://www.cnblogs.com/zaitianya1314/p/3702575.html