java.lang.SecurityException:AttributionSource 需要 android.permission.BLUETOOTH_CONNECT 权限

问题描述 投票:0回答:4

请理解,因为这是我第一次提问,所以这个问题可能有点晕。另外,请理解语法可能会很奇怪,因为我英语不好并且使用翻译器。

我是一个在Android工作室使用java制作应用程序的人。

尝试使用蓝牙时出现连续错误。错误太多,我可以轻松找到解决方案,但它不适用于我。

错误日志

Process: org.techtown.push.bluetoothtest2, PID: 10852
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org.techtown.push.bluetoothtest2/org.techtown.push.bluetoothtest2.MainActivity}: java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10158, packageName = org.techtown.push.bluetoothtest2, attributionTag = null, token = android.os.BinderProxy@f43a396, next = null }: AdapterService getBondedDevices
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10158, packageName = org.techtown.push.bluetoothtest2, attributionTag = null, token = android.os.BinderProxy@f43a396, next = null }: AdapterService getBondedDevices
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2425)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.os.Parcel.readException(Parcel.java:2334)
        at android.bluetooth.IBluetooth$Stub$Proxy.getBondedDevices(IBluetooth.java:3077)
        at android.bluetooth.BluetoothAdapter.getBondedDevices(BluetoothAdapter.java:2491)
        at org.techtown.push.bluetoothtest2.MainActivity.showPairedDevicesListDialog(MainActivity.java:300)
        at org.techtown.push.bluetoothtest2.MainActivity.onCreate(MainActivity.java:80)
        at android.app.Activity.performCreate(Activity.java:8051)
        at android.app.Activity.performCreate(Activity.java:8031)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:201) 
        at android.os.Looper.loop(Looper.java:288) 
        at android.app.ActivityThread.main(ActivityThread.java:7839) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 

我认为中间这句话是错误的原因。

Caused by: java.lang.SecurityException: Need android.permission.BLUETOOTH_CONNECT permission for AttributionSource { uid = 10158, packageName = org.techtown.push.bluetoothtest2, attributionTag = null, token = android.os.BinderProxy@f43a396, next = null }: AdapterService getBondedDevices

但是我已经应用了解决该问题的方法。这是我的 Manifest.xml。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.techtown.push.bluetoothtest2">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-feature android:name="android.hardware.bluetooth" android:required="false"/>
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Bluetoothtest2">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我应用它是因为我发现我需要“android.permission.BLUETOOTH_CONNECT 权限”。然而,错误仍然不断发生。

我是不是误会了什么?还是我在mainactivty.java中犯了错误?

供您参考,我做项目时是这样的。

最小 SDK:API 16:Android 4.1(Jelly Bean)

如果你写下的文件没有问题,请告诉我再检查一下Java代码。 谢谢。

java android android-studio bluetooth
4个回答
18
投票

自 Android 6.0 起,并非所有权限都仅仅因为放置在清单中而被授予。您必须使用 BLUETOOTH_CONNECT 处理

运行时权限
流(如何在此处请求)

BLUETOOTH_ADVERTISE、BLUETOOTH_CONNECT 和 BLUETOOTH_SCAN 权限是运行时权限。因此,您必须在应用程序中明确请求用户批准,然后才能查找蓝牙设备、使其他设备可发现设备或与已配对的蓝牙设备通信。


16
投票

您应该在 Manifest 和 Activity.java 中声明 BLUETOOTH_SCAN 和权限

例如:

  1. AndroiManifest.xml
<uses-permission android:name="android.permission.BLUETOOTH"
        android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
        android:maxSdkVersion="30" />
    <!-- Needed only if your app looks for Bluetooth devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED"
        tools:ignore="ProtectedPermissions" />
  1. MainActivity.java
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.BLUETOOTH_PRIVILEGED
    };
    private static String[] PERMISSIONS_LOCATION = {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT,
            Manifest.permission.BLUETOOTH_PRIVILEGED
    };

    private void checkPermissions(){
        int permission1 = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int permission2 = ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN);
        if (permission1 != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            ActivityCompat.requestPermissions(
                    this,
                    PERMISSIONS_STORAGE,
                    1
            );
        } else if (permission2 != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(
                    this,
                    PERMISSIONS_LOCATION,
                    1
            );
        }
    }

0
投票

就我而言,我必须请求

Manifest.permission.ACCESS_FINE_LOCATION
的许可才能解决此问题。


0
投票

step-1:在AndroidManifest.xml中声明BLUETOOTH_SCAN

Step-2:在 Activity.XML 中声明

第 3 步:检查权限(BLUETOOTH_CONNECT、BLUETOOTH_ADVERTISE、ACCESS_FINE_LOCATION)

供参考https://developer.android.com/develop/connectivity/bluetooth/bt-permissions

© www.soinside.com 2019 - 2024. All rights reserved.