mac from address android bluetooth mac-address android-6.0-marshmallow

from - mac id android



Obtener la dirección de mac local de Bluetooth en Marshmallow (8)

Como resultado, terminé no obteniendo la dirección MAC de Android. El dispositivo Bluetooth terminó proporcionando la dirección MAC del dispositivo Android, que se almacenó y luego se usó cuando fue necesario. Sí, parece un poco raro, pero en el proyecto en el que estaba, el software del dispositivo Bluetooth también se estaba desarrollando y esta resultó ser la mejor manera de lidiar con la situación.

Pre Marshmallow mi aplicación obtendría su dirección MAC del dispositivo a través de BluetoothAdapter.getDefaultAdapter().getAddress().

Ahora con Marshmallow, Android regresará 02:00:00:00:00:00 .

Vi un enlace (lo siento, no estoy seguro de dónde está ahora) que dice que necesita agregar el permiso adicional

<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"/>

para poder conseguirlo. Sin embargo, no está funcionando para mí.

¿Se necesita algún permiso adicional para obtener la dirección mac?

No estoy seguro de que sea pertinente aquí, pero el manifiesto también incluye

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Entonces, ¿hay alguna manera de obtener la dirección mac de bluetooth local?


El acceso a la dirección mac se ha eliminado deliberadamente:

Para proporcionar a los usuarios una mayor protección de datos, a partir de esta versión, Android elimina el acceso programático al identificador de hardware local del dispositivo para las aplicaciones que utilizan las API de Wi-Fi y Bluetooth.

(De los cambios de Android 6.0 )


Funcionó muy bien

private String getBluetoothMacAddress() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); String bluetoothMacAddress = ""; try { Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService"); mServiceField.setAccessible(true); Object btManagerService = mServiceField.get(bluetoothAdapter); if (btManagerService != null) { bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService); } } catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignore) { } return bluetoothMacAddress; }


Obtener la dirección MAC a través de la reflexión puede verse así:

private static String getBtAddressViaReflection() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); Object bluetoothManagerService = new Mirror().on(bluetoothAdapter).get().field("mService"); if (bluetoothManagerService == null) { Log.w(TAG, "couldn''t find bluetoothManagerService"); return null; } Object address = new Mirror().on(bluetoothManagerService).invoke().method("getAddress").withoutArgs(); if (address != null && address instanceof String) { Log.w(TAG, "using reflection to get the BT MAC address: " + address); return (String) address; } else { return null; } }

utilizando una biblioteca de reflexión (net.vidageek: mirror) pero obtendrás la idea.


Por favor, use el siguiente código para obtener la dirección mac del bluetooth. Déjame saber si hay algún problema.

private String getBluetoothMacAddress() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); String bluetoothMacAddress = ""; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){ try { Field mServiceField = bluetoothAdapter.getClass().getDeclaredField("mService"); mServiceField.setAccessible(true); Object btManagerService = mServiceField.get(bluetoothAdapter); if (btManagerService != null) { bluetoothMacAddress = (String) btManagerService.getClass().getMethod("getAddress").invoke(btManagerService); } } catch (NoSuchFieldException e) { } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } } else { bluetoothMacAddress = bluetoothAdapter.getAddress(); } return bluetoothMacAddress; }


Primero deben agregarse los siguientes permisos a Manifest;

<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />

Entonces,

public static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS = "bluetooth_address"; String macAddress = Settings.Secure.getString(getContentResolver(), SECURE_SETTINGS_BLUETOOTH_ADDRESS);

Después de eso, la solicitud debe firmarse con la clave OEM / Sistema. Probado y verificado en Android 8.1.0.


Puede acceder a la dirección de Mac desde el archivo "/ sys / class / net /" + networkInterfaceName + "/ address" , donde networkInterfaceName puede ser wlan0 o eth1.Pero su permiso puede estar protegido contra lectura, por lo que es posible que no funcione en algunos dispositivos . También estoy adjuntando la parte del código que obtuve de SO.

public static String getWifiMacAddress() { try { String interfaceName = "wlan0"; List<NetworkInterface> interfaces = Collections .list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) { if (!intf.getName().equalsIgnoreCase(interfaceName)) { continue; } byte[] mac = intf.getHardwareAddress(); if (mac == null) { return ""; } StringBuilder buf = new StringBuilder(); for (byte aMac : mac) { buf.append(String.format("%02X:", aMac)); } if (buf.length() > 0) { buf.deleteCharAt(buf.length() - 1); } return buf.toString(); } } catch (Exception exp) { exp.printStackTrace(); } return ""; }


zmarties tiene razón, pero aún puede obtener la dirección mac a través de la reflexión o la configuración. Seguridad:

String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");