4

There are some crashes observed in production app only for Android 13 devices.

All the bluetooth related permission are declared in manifest and nearby device runtime permissions are also in place.

<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>

Crash Logs:

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)
Pratik Popat
  • 2,891
  • 20
  • 31
  • Are you trying to turn on bluetooth hardware directly? Have you seen this [post](https://stackoverflow.com/q/19974264/12749998)? Looks relatable. – Kozmotronik May 02 '23 at 10:31
  • there is a bluetooth on check.. if bluetooth is off then not going for scan/connect – Pratik Popat May 02 '23 at 10:36
  • 3
    Bluetooth on checking should not be a concern. You might be touching some BLE API in your code, where 3rd party apps have no privilege on it. Those API's can be touched only by system and system wide apps. Do you mind putting all relevant code where you call BLE APIs? – Kozmotronik May 02 '23 at 10:54
  • have you solved this problem? I have exactly the same problem as you,and haven't find any solution. – Leeqi7 May 23 '23 at 03:24
  • @Leeqi7 not found any solution till now – Pratik Popat May 23 '23 at 12:16
  • @PratikPopat If you find any solution, could you please tell me? Thank you very much! – Leeqi7 May 24 '23 at 01:49

3 Answers3

2

Registering for notifications are in some cases restricted only to apps that have the BLUETOOTH_PRIVILEGED permission, which generally only is available to system apps.

It is possible that you are trying to register notifications for example on a HID service, which are banned for general app access. Or at least the Android system believes (by looking in its GATT cache) that the characteristic is a sensitive one.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • I'm not registering notification for HID service. we have custom BLE device specific services and for those I am registering for notification – Pratik Popat May 12 '23 at 05:20
  • 1
    By following the stack trace, it appears this code decides what are the restricted characteristics/services: https://android.googlesource.com/platform/packages/modules/Bluetooth/+/9e0e2fb3e323616d151c7a9504501183aad15f7a/android/app/src/com/android/bluetooth/gatt/GattService.java#2442. Here we can see that HID, FIDO, Android Remote TV, LE audio services are restricted. Hence it appears your system believes the calling app is interacting with those services. I can see no other explanation, since line 4021 in the same file seems to be called according to your stack trace. – Emil May 12 '23 at 08:17
0

ive been told that there has been issues with android 12 (API level 31), the android.permission.BLUETOOTH_PRIVILEGED permission is priviledged and isnt available for 3rd party apps

0

The Bluetooth permission of Android 12 (API 31) has been changed.

Android 12 introduces BLUETOOTH_SCAN , BLUETOOTH_ADVERTISE and BLUETOOTH_CONNECT permissions, Allows the app to scan for nearby devices (NearBy) without requesting location permission (ACCESS_FINE_LOCATION).

  1. If your application looks for Bluetooth devices, such as BLE peripherals, please declare the BLUETOOTH_SCAN permission.
  2. If your application makes the current device discoverable by other bluetooth devices, declare the BLUETOOTH_ADVERTISE permission.
  3. If your application communicates with a paired Bluetooth device, please declare the BLUETOOTH_CONNECT permission.
  4. For legacy Bluetooth-related permission declarations, set android:maxSdkVersion to 30. This app compatibility step helps the system grant your app only the Bluetooth required when installed on a device running Android 12 or higher authority.

Since these three Bluetooth permissions are runtime permissions, user consent must be explicitly requested in the app before it can discover Bluetooth devices. Therefore, there is a problem of Bluetooth connection permission checking. We start from this perspective (BLUETOOTH_CONNECT) and analyze the permission checking mechanism.

    <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"/>
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50