TSW*_*985 80 java android mac-address
我需要使用Java获取我的Android设备的MAC地址.我在网上搜索过,但是我找不到任何有用的东西.
Kon*_*che 106
正如评论中已经指出的那样,MAC地址可以通过WifiManager接收.
WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();
Run Code Online (Sandbox Code Playgroud)
另外,不要忘记将适当的权限添加到您的 AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Run Code Online (Sandbox Code Playgroud)
请参阅Android 6.0更改.
为了向用户提供更好的数据保护,从此版本开始,Android会删除使用Wi-Fi和蓝牙API对应用程序的设备本地硬件标识符的编程访问.WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法现在返回一个常量值02:00:00:00:00:00.
要通过蓝牙和Wi-Fi扫描访问附近外部设备的硬件标识符,您的应用现在必须具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限.
min*_*pif 33
获取MAC地址WifiInfo.getMacAddress()将无法在Marshmallow及更高版本上运行,它已被禁用并将返回常量值02:00:00:00:00:00.
pm *_*bey 18
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";
}
Run Code Online (Sandbox Code Playgroud)
ade*_*190 11
<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;
}
Run Code Online (Sandbox Code Playgroud)
在这里有其他方式
Tiz*_*tta 10
我从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 "";
}
Run Code Online (Sandbox Code Playgroud)
它与棉花糖一起工作
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 "";
}
}
Run Code Online (Sandbox Code Playgroud)
您无法再获取 android 设备的硬件 MAC 地址。WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress() 方法将返回 02:00:00:00:00:00。此限制是在 Android 6.0 中引入的。
但是 Rob Anderson 找到了一个适用于 < Marshmallow 的解决方案:https : //stackoverflow.com/a/35830358
| 归档时间: |
|
| 查看次数: |
137994 次 |
| 最近记录: |