android 6.0及以下获取wifi mac地址
16lz
2021-01-23
Android 获取wifi mac地址的方法
Android 6.0以下获取mac地址的方法如下:
WifiManager mWifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (mWifi.isWifiEnabled()) { WifiInfo wifiInfo = mWifi.getConnectionInfo(); macAddress = wifiInfo != null ? wifiInfo.getMacAddress() : ""; }
但是在6.0及以上设置了权限,上面的方法获取的wifi mac 为默认值 “02:00:00:00:00:02”,可以用如下的方法获取
String macAddress = null; StringBuffer buf = new StringBuffer(); NetworkInterface networkInterface = null; try { networkInterface = NetworkInterface.getByName("wlan0"); if (networkInterface == null) { return "02:00:00:00:00:02"; } byte[] addr = networkInterface.getHardwareAddress(); for (byte b : addr) { buf.append(String.format("%02X:", b)); } if (buf.length() > 0) { buf.deleteCharAt(buf.length() - 1); } macAddress = buf.toString(); } catch (SocketException e) { e.printStackTrace(); return "02:00:00:00:00:02"; }
更多相关文章
- android Environment 常用方法(获取存储目录)
- Android下如何获取Mac地址
- 取WiFi MAC地址
- 为usb网卡设置ip地址之一
- Android 获取控件宽高的3种方法
- Android中创建与几种解析xml的方法!
- Android当方法总数超过64K时(Android Studio)
- android ViewPager 使用方法
- android_m2repository_rxx.zip下载地址以及MD5