android 6.0 没有返回准确的蓝牙 mac 地址

android 6.0 not returning the exact bluetooth mac address

 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

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

请告诉我在蓝牙

中获取当前mac地址需要什么问题或设置

我试过下面的代码它返回空

     /**
     * 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;
          }*/
    }

使用此方法获取 MAC-地址:

/**
 * 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();

}

此功能在 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

如果有人找到解决方法,请告诉我。

以下方法将在Android 6.0

中给出蓝牙macAddress
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;
    }