简体   繁体   中英

android 6.0 not returning the exact bluetooth mac address

 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

mBluetoothAdapter.getAddress(); returning the 02:00:00:00:00:00.

Please let me know what is the issues or settings I need to get current mac address in bluetooth

i have tried the following code it was returning empty

     /**
     * Returns MAC address of the given interface name.
     * @param interfaceName eth0, wlan0 or NULL=use first interface
     * @return  mac address or empty string
     */

    public static String getMACAddress(String interfaceName) 
    {
        try 
        {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) 
            {
                if (interfaceName != null) 
                {
                    if (!intf.getName().equalsIgnoreCase(interfaceName)) continue;
                }
                byte[] mac = intf.getHardwareAddress();
                if (mac==null) return "";
                StringBuilder buf = new StringBuilder();
                for (int idx=0; idx<mac.length; idx++)
                    buf.append(String.format("%02X:", mac[idx]));
                if (buf.length()>0) buf.deleteCharAt(buf.length()-1);
                return buf.toString();
            }
        } catch (Exception ex) { } // for now eat exceptions
        return "";
        /*try 
          {
            // this is so Linux hack
            return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
        } catch (IOException ex) 
          {
            return null;
          }*/
    }

The following method will give the Bluetooth macAddress in Android 6.0

public static final String SECURE_SETTINGS_BLUETOOTH_ADDRESS = "bluetooth_address";


public static String getBluetoothMacAddress(Context mContext) {


        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();


        // BluetoothAdapter.getDefaultAdapter().DEFAULT_MAC_ADDRESS;
        // if device does not support Bluetooth
        if (mBluetoothAdapter == null) {
            Log.d(TAG, "device does not support bluetooth");
            return null;
        }

        String address = mBluetoothAdapter.getAddress();
        if (address.equals("02:00:00:00:00:00")) {

            //  System.out.println(">>>>>G fail to get mac address " + address);

            try {


                ContentResolver mContentResolver = mContext.getContentResolver();

                address = Settings.Secure.getString(mContentResolver, SECURE_SETTINGS_BLUETOOTH_ADDRESS);
                DebugReportOnLocat.ln(">>>>G >>>> mac " + address);

            } catch (Exception e) {


            }


        } else {

            // System.out.println(">>>>>G sucess to get mac address " + address);
        }
        return address;
    }

This feature was disabled in Android 6.

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.

http://developer.android.com/intl/pt-br/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id

If anyone find an workaround, please, tell me.

Get MAC-adress with this method:

/**
 * get bluetooth adapter MAC address
 * @return MAC address String
 */
public static String getBluetoothMacAddress() {

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

// if device does not support Bluetooth
if(mBluetoothAdapter==null){
    Log.d(TAG,"device does not support bluetooth");
    return null;
}

return mBluetoothAdapter.getAddress();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM