正如在评论中已经指出的那样,可以通过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,为