nextcloud-talk-android/app
Tim Krüger 4821b02729
Add Android build switch for checking bluetooth permissions
Refactored method 'hasPermission' to 'hasNoBluetoothPermission'. The new method
respect the in SKD 31 introduced permission BLUETOOTH_CONNECT. For older SDK
versions the permission BLUETOOTH will be used.

Additionaly make methods private which need bluetooth permissions and only
called locally. For public methods which need bluetooth permissions the method
'hasNoBluetoothPermission' is called to check them.
Also add suppress lint annotation for 'MissingPermission'.

From SDK 30 to SDK 31 the bluetooth related permissions changed and it
is suggested to add the attribute 'android:maxSdkVersion="30"' to the
legacy BLUETOOTH permission in the 'AndroidManifest.xml' [1]:

> For your legacy Bluetooth-related permission declarations, set
> android:maxSdkVersion to 30. This app compatibility step helps the system
> grant your app only the Bluetooth permissions that it needs when installed
> on devices that run Android 12 or higher.

This is explicitly not done here!

During runtime (on Android 12) while starting the 'MagicBluetoothManger' the
following part in 'android.bluetooth.BluetootHeadset' constructor will be
executed and results in the previous exception:

  // Preserve legacy compatibility where apps were depending on
  // registerStateChangeCallback() performing a permissions check which
  // has been relaxed in modern platform versions
  if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.R
           && context.checkSelfPermission(android.Manifest.permission.BLUETOOTH)
                   != PackageManager.PERMISSION_GRANTED) {
     throw new SecurityException("Need BLUETOOTH permission");
 }

In the 'build.gradle' the 'targetSdkVersion 30' and 'compileSdkVersion 31' is
configured. Because the 'MagicBluetoothManager' checks for the 'targetSdkVersion'
instead of 'Build.VERSION.SDK_INT' also on a Android 12 (SDK 31) the 'BLUETOOTH'
permission is needed.

So the solution is to don't set `android:maxSdkVersion="30"' for the BLUETOOTH
permission and request the BLUETOOTH_CONNECT permission.

Resolves: #2132
See:
  [1] https://web.archive.org/web/20220416121005/https://developer.android.com/guide/topics/connectivity/bluetooth/permissions#declare-android12-or-higher

Signed-off-by: Tim Krüger <t@timkrueger.me>
2022-06-17 15:51:44 +02:00
..
src Add Android build switch for checking bluetooth permissions 2022-06-17 15:51:44 +02:00
build.gradle Weekly 14.1.0 Alpha 08 2022-06-06 03:15:13 +00:00
lint.xml
proguard-rules.pro