[英]get wifi mac address of my device?
我想访问设备的wifi mac地址并将其发布到服务器,我使用以下方法:
public void accessAdress(){
//Detect Bluetooth Address
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String address = wifiInfo.getMacAddress();
Toast.makeText(this, ""+address, Toast.LENGTH_SHORT).show();
}
但是它给了我这样的东西:02:00:00:00:00:00。 我应该怎么做才能访问我的真实wifi mac地址? (我的设备的android版本是7.0)
从Android 6.0开始模糊了Mac地址
为了向用户提供更好的数据保护,从此版本开始,Android删除使用Wi-Fi和Bluetooth API对应用程序对设备本地硬件标识符的编程访问。 WifiInfo.getMacAddress()和BluetoothAdapter.getAddress()方法现在返回常数值02:00:00:00:00:00。
要通过蓝牙和Wi-Fi扫描访问附近的外部设备的硬件标识符,您的应用现在必须具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限。
请参阅文档 。
人们已经用这种围绕工作 :
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 "";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.