您可以從IPv6本地地址獲取MAC地址。例如,IPv6地址「fe80 :: 1034:56ff:fe78:9abc」對應於MAC地址「12-34-56-78-9a-bc」。請參閱下面的代碼。獲取WiFi IPv6地址只需要android.permission.INTERNET。
請參閱維基百科頁面IPv6 Address,特別是關於「本地地址」fe80 ::/64的註釋和關於「修改的EUI-64」的章節。
/**
* Gets an EUI-48 MAC address from an IPv6 link-local address.
* E.g., the IPv6 address "fe80::1034:56ff:fe78:9abc"
* corresponds to the MAC address "12-34-56-78-9a-bc".
* <p/>
* See the note about "local addresses" fe80::/64 and the section about "Modified EUI-64" in
* the Wikipedia article "IPv6 address" at https://en.wikipedia.org/wiki/IPv6_address
*
* @param ipv6 An Inet6Address object.
* @return The EUI-48 MAC address as a byte array, null on error.
*/
private static byte[] getMacAddressFromIpv6(final Inet6Address ipv6)
{
byte[] eui48mac = null;
if (ipv6 != null) {
/*
* Make sure that this is an fe80::/64 link-local address.
*/
final byte[] ipv6Bytes = ipv6.getAddress();
if ((ipv6Bytes != null) &&
(ipv6Bytes.length == 16) &&
(ipv6Bytes[0] == (byte) 0xfe) &&
(ipv6Bytes[1] == (byte) 0x80) &&
(ipv6Bytes[11] == (byte) 0xff) &&
(ipv6Bytes[12] == (byte) 0xfe)) {
/*
* Allocate a byte array for storing the EUI-48 MAC address, then fill it
* from the appropriate bytes of the IPv6 address. Invert the 7th bit
* of the first byte and discard the "ff:fe" portion of the modified
* EUI-64 MAC address.
*/
eui48mac = new byte[6];
eui48mac[0] = (byte) (ipv6Bytes[8]^0x2);
eui48mac[1] = ipv6Bytes[9];
eui48mac[2] = ipv6Bytes[10];
eui48mac[3] = ipv6Bytes[13];
eui48mac[4] = ipv6Bytes[14];
eui48mac[5] = ipv6Bytes[15];
}
}
return eui48mac;
}
堆棧溢出用於編程問題。你的問題是什麼?如果您嘗試提供某種常見問題解答條目,請[請按照網站上的說明](https://stackoverflow.com/help/self-answer)提出問題,然後提供您自己的問題答案。 – CommonsWare
看來,Mac地址是隨機的,即使你可以抓住它! https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id –
[在Android 6.0中獲取MAC地址]的可能重複(http://stackoverflow.com/questions-33159224/getting-mac-address-in-android-6-0) – mhdjazmati