java安卓获取mac_android开发分享以编程方式获取Android设备的MAC
正如在評論中已經指出的那樣,可以通過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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 社保卡丢失了该如何操作
- 下一篇: 指数基金周末有收益吗