Android 13 - 需要蓝牙特权许可吗?

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

仅在 Android 13 Pixel 设备的生产应用程序中观察到一些崩溃。

所有与蓝牙相关的权限都在清单中声明,附近的设备运行时权限也已到位。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="auto">

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission
        android:name="android.permission.BLUETOOTH_ADMIN"
        android:maxSdkVersion="33" />
    <uses-permission
        android:name="android.permission.BLUETOOTH_SCAN"
        android:usesPermissionFlags="neverForLocation"
        tools:ignore="UnusedAttribute" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
  
    <uses-permission
        android:name="android.permission.ACCESS_COARSE_LOCATION"
        android:maxSdkVersion="31" />
    <uses-permission
        android:name="android.permission.ACCESS_FINE_LOCATION"
        android:maxSdkVersion="31" />
</manifest>

崩溃日志:

Fatal Exception: java.lang.SecurityException: Need BLUETOOTH PRIVILEGED permission: Neither user 10370 nor current process has android.permission.BLUETOOTH_PRIVILEGED.
       at android.app.ContextImpl.enforce(ContextImpl.java:2240)
       at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:2268)
       at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:948)
       at com.android.bluetooth.Utils.enforceBluetoothPrivilegedPermission(Utils.java:411)
       at com.android.bluetooth.gatt.GattService.permissionCheck(GattService.java:474)
       at com.android.bluetooth.gatt.GattService.registerForNotification(GattService.java:3801)
       at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerForNotification(GattService.java:1085)
       at com.android.bluetooth.gatt.GattService$BluetoothGattBinder.registerForNotification(GattService.java:1073)
       at android.bluetooth.IBluetoothGatt$Stub.onTransact(IBluetoothGatt.java:812)
       at android.os.Binder.execTransactInternal(Binder.java:1285)
       at android.os.Binder.execTransact(Binder.java:1244)
android bluetooth bluetooth-lowenergy android-13
2个回答
0
投票

我被告知 android 12(API 级别 31)存在问题,android.permission.BLUETOOTH_PRIVILEGED 权限是特权的,不适用于第 3 方应用程序


0
投票

Android 12(API 31)的蓝牙权限已更改

Android 12 引入了 BLUETOOTH_SCANBLUETOOTH_ADVERTISEBLUETOOTH_CONNECT 权限, 允许应用扫描附近的设备 (NearBy) 而无需请求位置权限 (ACCESS_FINE_LOCATION)。

  1. 如果您的应用程序寻找蓝牙设备,例如 BLE 外围设备,请声明 BLUETOOTH_SCAN 权限。
  2. 如果您的应用程序使当前设备可被其他蓝牙设备发现,请声明 BLUETOOTH_ADVERTISE 权限。
  3. 如果您的应用程序与配对的蓝牙设备通信,请声明BLUETOOTH_CONNECT权限。
  4. 遗留的蓝牙相关权限声明,将android:maxSdkVersion设置为30。此应用兼容性步骤有助于系统仅授予您的应用安装在运行Android 12或更高权限的设备上时所需的蓝牙。

由于这三个蓝牙权限是运行时权限,因此必须在应用程序中明确请求用户同意才能发现蓝牙设备。 因此,存在蓝牙连接权限检查的问题。我们从这个角度(BLUETOOTH_CONNECT)入手,分析权限检查机制

    <uses-permission android:name="android.print.BLUETOOTH " />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
    <uses-permission android:name="android .permission. BLUETOOTH_DEBUG"/>
© www.soinside.com 2019 - 2024. All rights reserved.