正如在评论中已经指出的那样,可以通过WifiManager接收MAC地址。

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = manager.getConnectionInfo(); String address = info.getMacAddress();

另外不要忘记将相应的权限添加到您的AndroidManifest.xml

请参阅Android 6.0更改 。

为了向用户提供更好的数据保护,从本版本开始,Android使用Wi-Fi和蓝牙API删除对设备本地硬件标识符的编程访问。 WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法现在返回一个常数值02:00:00:00:00:00。

要通过蓝牙和Wi-Fi扫描访问附近外部设备的硬件标识符,您的应用必须具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限。

通过WifiInfo.getMacAddress()获取MAC地址在Marshmallow及以上版本中不起作用,它已被禁用,并将返回恒定值02:00:00:00:00:00 。

public String getMacAddress(Context context) { WifiManager wimanager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); String macAddress = wimanager.getConnectionInfo().getMacAddress(); if (macAddress == null) { macAddress = "Device don't have mac address or wi-fi is disabled"; } return macAddress; }

在这里有其他的方法

public static String getMacAddr() { try { List all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(String.format("%02X:",b)); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { } return "02:00:00:00:00:00"; }

你可以得到mac地址:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wInfo = wifiManager.getConnectionInfo(); String mac = wInfo.getMacAddress();

在Menifest.xml中设置权限

我从; 希望有帮助!

public static String getMacAddr() { try { List all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(Integer.toHexString(b & 0xFF) + ":"); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { } return "02:00:00:00:00:00"; }

它与棉花糖一起工作

package com.keshav.fetchmacaddress; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Collections; import java.util.List; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("keshav","getMacAddr -> " +getMacAddr()); } public static String getMacAddr() { try { List all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) { return ""; } StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) { res1.append(Integer.toHexString(b & 0xFF) + ":"); } if (res1.length() > 0) { res1.deleteCharAt(res1.length() - 1); } return res1.toString(); } } catch (Exception ex) { //handle exception } return ""; } }

这里从Android来源采取。 这是在系统设置应用程序中显示您的MAC ADDRESS的实际代码。

private void refreshWifiInfo() { WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS); String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress(); wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : getActivity().getString(R.string.status_unavailable)); Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS); String ipAddress = Utils.getWifiIpAddresses(getActivity()); wifiIpAddressPref.setSummary(ipAddress == null ? getActivity().getString(R.string.status_unavailable) : ipAddress); }

你不能再获得Android设备的硬件MAC地址。 WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法将返回02:00:00:00:00:00。 这个限制是在Android 6.0中引入的。

但罗布·安德森find了一个解决scheme,为

java安卓获取mac_android开发分享以编程方式获取Android设备的MAC相关推荐

  1. 如何以编程方式在Android上截屏?

    如何通过代码而不是通过任何程序来截屏电话屏幕的选定区域? #1楼 Mualig的回答很好,但是我遇到了Ewoks描述的相同问题,但我没有得到背景知识. 因此,有时足够好,有时我会在黑色背景上出现黑色文 ...

  2. 如何通过编程方式获取alexa排名的数据

    Alexa 是以发布世界网站排名而引人注目的一个网站.其实,此网站的搜索引擎也很好用,但是"网站排名"却是它吸引眼球的最主要原因.      以网站导航起家的Alexa创建于199 ...

  3. android的padding属性,以编程方式获取android:padding属性

    从一个角度来看,如何以编程方式获取android:padding属性的值? 我目前正在使用: private static final String ANDROID_NAMESPACE = " ...

  4. android softkeyboard,如何以编程方式关闭Android Soft KeyBoard?

    如何以编程方式关闭Android Soft KeyBoard? 我目前正在使用以下代码显示软键盘 InputMethodManager imm = (InputMethodManager) getSy ...

  5. 学java 安卓还是ios开发_非计算机科班出身,有JAVA基础,问学安卓开发还是IOS开发好些?...

    至于转android还是ios还需要你自己考虑好,最好是有兴趣就好,不管是学什么,兴趣是最好的老师,学java的话转android会有一些好处,java转安卓的一些建议可以看下: 先说说Java和An ...

  6. java模拟滑动事件_java - 以编程方式在Android视图中触发滑动动作事件 - SO中文参考 - www.soinside.com...

    我正在尝试以编程方式触发视图中的滑动.还有其他问题的答案,但他们没有为我工作.我还看到一些comments表示程序化的拖动/滚动因安全原因被禁用,这是真的吗?我很想找到一个明确的答案. 我尝试了以下方 ...

  7. android+双卡imei,以编程方式在Android中为双SIM卡检索IMEI号码

    对于单个SIM,以下代码有效: TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); String ...

  8. android 获取蓝牙设备id_不需要任何权限获得Android设备的唯一ID

    这个问题来自于Is there a unique Android device ID? 我对这个问题的答案做了整理,包括将另一篇文章加入进来作为补充,可以完美解决此问题. 作者提出的问题: Andro ...

  9. eap wifi 证书_如何以编程方式在Android中安装CA证书(用于EAP WiFi配置)?

    我的目标:在Android programmitcally中创建一个EAP WiFi配置 - 包括CA证书 . 问题:如何以编程方式安装CA证书(然后在EAP WiFi配置中引用该证书)? 但是,假设 ...

最新文章

  1. python学会了能做什么-学会Python后都能做什么?介绍五种Python的实用场景
  2. WinRT知识积累1之读xml数据
  3. ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
  4. 翻译python语言命令_有道词典命令行快速翻译,Python编程的利器
  5. 坚持自主创新,凌波微步完成数千万A轮融资,加速半导体产业
  6. c# 执行js方法
  7. 57 Insert Interval
  8. 基于Office Visio 2010 图表绘制
  9. 想知道PDF转Word软件免费有哪些吗?试试这3款工具
  10. java 加密 压缩_如何用java 将文件加密压缩为zip文件.
  11. 解决React Hooks useEffect控制台报错:内存泄漏
  12. 一键查询 | 2020年最新SCI期刊影响因子报告
  13. badboy录制脚本错误问题解决
  14. Moment.js 2.22.2 源代码
  15. 万字长文:人脸识别综述(学习笔记)
  16. Mac系统原生支持NTFS格式硬盘
  17. golang加载双向认证加密的证书key文件
  18. Inventor冲压加强筋_inventor 加强筋教程
  19. SQLServer中服务器角色和数据库角色权限详解角色
  20. 迪赛智慧数——其他图表(平行坐标图):家庭未来资产配置意愿

热门文章

  1. websocket 获取连接id_nodejs做后端,用websocket写聊天室,怎么获取连接用户的ip呢?...
  2. python把数据写入excel_Python读取和写入Excel文件(转)
  3. Hourglass网络
  4. 请编写一个 C 函数,该函数给出一个字节中8二进制数中为1的个数
  5. 【计算机网络】聊一聊那些常见的网络通信的性能指标
  6. 《***测试实践指南》D03
  7. 分享-WinForm界面开发之布局控件WeifenLuo.WinFormsUI.Docking的使用
  8. Ubuntu adb devices :???????????? no permissions 解决方法
  9. 中断处理及系统调用的处理过程
  10. Python GUI编程(Tkinter)