我需要使用Java获取我的Android设备的MAC地址。我已经在网上搜索过,但没有找到有用的信息。
我需要使用Java获取我的Android设备的MAC地址。我已经在网上搜索过,但没有找到有用的信息。
正如评论中已经指出的那样,可以通过WifiManager获取MAC地址。
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();
还要别忘了在你的AndroidManifest.xml
中添加适当的权限。
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
请参考Android 6.0 更改。为了提供更高的数据保护性,在此版本中,Android 删除了使用 Wi-Fi 和 Bluetooth API 的应用对设备本地硬件标识符的编程访问。现在,WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法返回一个固定值 02:00:00:00:00:00。要通过蓝牙和 Wi-Fi 扫描访问附近外部设备的硬件标识符,您的应用现在必须具有 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION 权限。在Marshmallow及以上版本中,通过WifiInfo.getMacAddress()
获取MAC地址将不起作用,因为它已被禁用并将返回常量值02:00:00:00:00:00
。
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";
}
我从http://robinhenniges.com/en/android6-get-mac-address-programmatically找到了这个解决方案,它对我很有效!希望能帮助到你!
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) {
String hex = Integer.toHexString(b & 0xFF);
if (hex.length() == 1)
hex = "0".concat(hex);
res1.append(hex.concat(":"));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
return "";
}
<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;
}
有其他方法这里
macAddress
会是null
吗? - Max HeiberContext context
吗?如果是的话,任何上下文都可以使用。https://developer.android.com/reference/android/content/Context.html#WIFI_SERVICE - ademar111190它能够与Marshmallow操作系统兼容
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<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 "";
}
}
你现在无法获取安卓设备的硬件MAC地址。WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法将返回 02:00:00:00:00:00。这个限制是在Android 6.0中引入的。
但Rob Anderson找到了一个适用于< Marshmallow的解决方案:https://dev59.com/0lwY5IYBdhLWcg3wCD6W#35830358
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String WLANMAC = wm.getConnectionInfo().getMacAddress();
您可以获取Mac地址:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();
在Manifest.xml文件中设置权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
这段代码来自Android源代码 这里。这是在系统设置应用程序中显示您的MAC地址的实际代码。
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);
}
WifiManager
(例如:WifiManager mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
)。 - fernandohur02:00:00:00:00:00
,而不是实际的WiFi MAC地址。 - Hunt
ip link | grep -A1 wlan0
可以在 Android 9 上运行,来源于 https://stackoverflow.com/questions/58463658/how-to-determine-wifi-hardware-address-in-termux。 - S D Rausty