[英]get wifi mac address of my device?
我想訪問設備的wifi mac地址並將其發布到服務器,我使用以下方法:
public void accessAdress(){
//Detect Bluetooth Address
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String address = wifiInfo.getMacAddress();
Toast.makeText(this, ""+address, Toast.LENGTH_SHORT).show();
}
但是它給了我這樣的東西:02:00:00:00:00:00。 我應該怎么做才能訪問我的真實wifi mac地址? (我的設備的android版本是7.0)
從Android 6.0開始模糊了Mac地址
為了向用戶提供更好的數據保護,從此版本開始,Android刪除使用Wi-Fi和Bluetooth API對應用程序對設備本地硬件標識符的編程訪問。 WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法現在返回常數值02:00:00:00:00:00。
要通過藍牙和Wi-Fi掃描訪問附近的外部設備的硬件標識符,您的應用現在必須具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION權限。
請參閱文檔 。
人們已經用這種圍繞工作 :
public static String getMacAddr() {
try {
List<NetworkInterface> 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 "";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.