4 回答

TA貢獻1799條經(jīng)驗 獲得超6個贊
正如評論中已經(jīng)指出的那樣,可以通過WifiManager接收MAC地址。
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();
也不要忘記在您的計算機中添加適當(dāng)?shù)臋?quán)限 AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
請參考Android 6.0更改。
為了向用戶提供更好的數(shù)據(jù)保護,從此版本開始,Android刪除使用Wi-Fi和Bluetooth API對應(yīng)用程序?qū)υO(shè)備本地硬件標(biāo)識符的編程訪問。WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法現(xiàn)在返回常數(shù)值02:00:00:00:00:00。
要通過藍(lán)牙和Wi-Fi掃描訪問附近的外部設(shè)備的硬件標(biāo)識符,您的應(yīng)用現(xiàn)在必須具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION權(quán)限。

TA貢獻1851條經(jīng)驗 獲得超4個贊
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(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";
}

TA貢獻1802條經(jīng)驗 獲得超10個贊
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
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;
}
添加回答
舉報