日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2024/9/19 Android 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。