简体   繁体   English

Android android.permission.BLUETOOTH_CONNECT 错误

[英]Android android.permission.BLUETOOTH_CONNECT error

I'm new to android studio and y have a problem that I can't solve.我是 android studio 的新手,你有一个我无法解决的问题。 I'm trying to enable Bluetooth, so I have done this :我正在尝试启用蓝牙,所以我这样做了:

AndroidManifest.xml : AndroidManifest.xml:

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

MainActivity.java : MainActivity.java :

private void enableBLT(){

    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter == null) {
        dialogNoBlt();
    }
    if (!adapter.isEnabled()) {

    if (ContextCompat.checkSelfPermission(
            MainActivity.this, Manifest.permission.BLUETOOTH_CONNECT) ==
            PackageManager.PERMISSION_GRANTED) {

            Intent enableBltIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBltIntent, REQUEST_ENABLE_BT);

        } else {
            Log.d("blt-", "no permission");
        }
    } else {
        Log.d("blt-", "Not enabled");

    }
}

But I still have an error "requires android.permission.BLUETOOTH_CONNECT", and my app just crash.但我仍然有一个错误“需要 android.permission.BLUETOOTH_CONNECT”,我的应用程序崩溃了。 Can you help me please ?你能帮我吗 ?

You have to request Manifest.permission.BLUETOOTH_CONNECT and Manifest.permission.BLUETOOTH_SCAN permissions at runtime as well.您还必须在运行时请求Manifest.permission.BLUETOOTH_CONNECTManifest.permission.BLUETOOTH_SCAN权限。 Just declaring in manifest is not sufficient because they are under dangerous permissions category.仅在清单中声明是不够的,因为它们属于危险权限类别。

docs for requesting permission 请求许可的文档

Refer the following link of answer for a direct code sample请参阅以下答案链接以获取直接代码示例

requesting multiple permissions at runtime在运行时请求多个权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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