缺少BluetoothAdapter.isEnabled.BLUETOOTH所需的权限

时间:2016-10-22 23:14:25

标签: android bluetooth android-permissions

我将蓝牙添加到我的应用程序但遇到了以下问题。当我做代码时:

BluetoothAdapter bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
    Toast.makeText(getApplicationContext(),"Device doesnt Support Bluetooth",Toast.LENGTH_SHORT).show();
}

if(!bluetoothAdapter.isEnabled()) {
    Intent enableAdapter = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableAdapter, 0);
}

错误发生在以下行:

if(!bluetoothAdapter.isEnabled())

错误:

  

缺少BluetoothAdapter.isEnabled所需的权限:android.permissions.BLUETOOTH

我在android清单文件中添加了以下权限:

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

但我仍然遇到同样的问题。你知道我做错了吗?

3 个答案:

答案 0 :(得分:1)

  1. 由于您使用的是权限,因此您只需要<uses-permission>个标记。您不需要<permissions>代码,因为这不是您的自定义权限。
  2. 也许您已将<uses-permission>标记放在清单中的错误元素中,但我不能用您提供的信息说出任何其他内容
  3. 由于你的bluetoothAdapter可以为null,要么添加到第二个IF空指针检查,要么在第一个IF中添加RETURN
  4. 拥有<uses-feature>代码
  5. 也很不错

答案 1 :(得分:0)

我们假设您有一个按钮然后点击它,蓝牙需要打开。以下是如何为Android 6.0及更高版本执行此操作的快速示例。

首先,在Activity / Fragment

中声明此变量
public static final int PERMISSION_ASK = 1001;

现在,让我们说这个按钮将启用蓝牙。 setOnClickListener

myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(isBluetoothPermissionGranted()) {
                // app already has required permissions
                // do some task here
            } else {
                // app does not have permission yet.
                // ask for permissions
                askForBluetoothPermissions();
            }
        } else {
            // Android version below 6.0, no need to check or ask for permission
            // do some task here
        }
    }
});

检查应用程序是否已具有所需权限:

@RequiresApi(api = Build.VERSION_CODES.M)
private boolean isBluetoothPermissionGranted() {
    boolean granted = false;
    int bluetoothGranted = checkSelfPermission(Manifest.permission.BLUETOOTH);
    int bluetoothAdminGranted = checkSelfPermission(Manifest.permission.BLUETOOTH_ADMIN);

    if(bluetoothGranted == PackageManager.PERMISSION_GRANTED &&
            bluetoothAdminGranted == PackageManager.PERMISSION_GRANTED) {
        granted = true;
    }

    return granted;
}

如果未获得所需的许可,请按照以下方式提出要求:

@RequiresApi(api = Build.VERSION_CODES.M)
private void askForBluetoothPermissions() {
    String[] permissions = new String[] {
            Manifest.permission.BLUETOOTH,
            Manifest.permission.BLUETOOTH_ADMIN
    };
    requestPermissions(permissions, PERMISSION_ASK);
}

最后,在这里,您将如何确认用户是否授予您许可:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_ASK:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                // all requested permissions were granted
                // perform your task here
            } else {
                // permissions not granted
                // DO NOT PERFORM THE TASK, it will fail/crash
            }
            break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            break;
    }
}

希望这有帮助

答案 2 :(得分:0)

对于Android 6.0及更高版本,您必须将以下内容添加到清单文件

y <- t(replicate(30,{x <- runif(3); y <- c(x, 100 - sum(x)); sort(y/sum(y) * 100)}))