Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting MAC address in Android 6.0

I'm developing an app that gets the MAC address of the device, but since Android 6.0 my code doesn't work, giving me an incorrect value.

Here's my code...

public String ObtenMAC() {     WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);     WifiInfo info = manager.getConnectionInfo();      return(info.getMacAddress().toUpperCase()); } 

Instead of the real MAC address, it returns an strange code: 02:00:00:00:00:00.

like image 871
Mazinger Avatar asked Oct 15 '15 21:10

Mazinger


People also ask

How do I find my device MAC address?

From Home, tap Menu > Settings > About Phone/Device. Tap either Status or Hardware Information. Scroll down to WiFi MAC address.

How do I find the MAC address on Android 9?

Android devices with Android 9 Open settings (*) of your Android mobile device. Select “System“ and tap the button “About phone“. You can find the MAC address of your mobile device under “Wi-Fi MAC address“.


2 Answers

Please refer to Android 6.0 Changes.

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.

like image 50
Siu Avatar answered Oct 05 '22 06:10

Siu


Use below code to get Mac address in Android 6.0

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 ""; } 
like image 33
Chintan Rathod Avatar answered Oct 05 '22 07:10

Chintan Rathod