我需要使用Java获取我的android设备的MAC地址。我已经在网上搜索过,但没有发现任何有用的信息。
我需要使用Java获取我的android设备的MAC地址。我已经在网上搜索过,但没有发现任何有用的信息。
Answers:
正如评论中已经指出的那样,可以通过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权限。
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";
}
<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
吗?
Context context
?如果是,则任何上下文都应该起作用。developer.android.com/reference/android/content/…–
我从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 "";
}
它与棉花糖一起工作
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地址:
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String mac = wInfo.getMacAddress();
在Menifest.xml中设置权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
您无法再获取Android设备的硬件MAC地址。WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法将返回02:00:00:00:00:00。此限制是在Android 6.0中引入的。
但是罗布·安德森(Rob Anderson)找到了一种解决方案,该解决方案适用于<棉花糖:https : //stackoverflow.com/a/35830358
从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);
)。
02:00:00:00:00:00
Mac地址,而不是实际的wifi上网Mac ID
使用这种简单的方法
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
String WLANMAC = wm.getConnectionInfo().getMacAddress();
此ip link | grep -A1 wlan0
命令可从如何确定Termux中的wifi硬件地址在 Android 9 上运行