为什么Android隐藏蓝牙Mac地址?

时间:2018-07-05 20:13:27

标签: java android android-bluetooth

我正试图通过这种方式获取蓝牙mac地址:

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

String address = mBluetoothAdapter.getAddress();

但是它总是返回:

02:00:00:00:00:00

为什么?这是一种安全政策吗?

谢谢。

贾科莫

1 个答案:

答案 0 :(得分:0)

也许您应该查看With targetSdkLevel of 27答案。

答案摘录:

 private String getBluetoothMac(final Context context) {

    String result = null;
    if (context.checkCallingOrSelfPermission(Manifest.permission.BLUETOOTH)
            == PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Hardware ID are restricted in Android 6+
            // https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id
            // Getting bluetooth mac via reflection for devices with Android 6+
            result = android.provider.Settings.Secure.getString(context.getContentResolver(),
                    "bluetooth_address");
        } else {
            BluetoothAdapter bta = BluetoothAdapter.getDefaultAdapter();
            result = bta != null ? bta.getAddress() : "";
        }
    }
    return result;
}