[Android] Check bluetooth permissions

Robert Stone git at videolan.org
Wed Nov 20 06:47:54 UTC 2024


vlc-android | branch: master | Robert Stone <rhstone at gmail.com> | Sat Aug 10 08:51:18 2024 -0700| [27bff6827ab16e1050571ae9c11475cf0db7340e] | committer: Duncan McNamara

Check bluetooth permissions

> https://code.videolan.org/videolan/vlc-android/commit/27bff6827ab16e1050571ae9c11475cf0db7340e
---

 .../src/org/videolan/vlc/MediaSessionCallback.kt           | 14 ++++++++------
 .../vlc-android/src/org/videolan/vlc/util/Permissions.kt   |  4 ++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
index 54ee31d353..31d5bff920 100644
--- a/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
+++ b/application/vlc-android/src/org/videolan/vlc/MediaSessionCallback.kt
@@ -49,6 +49,7 @@ import org.videolan.tools.removeQuery
 import org.videolan.tools.retrieveParent
 import org.videolan.vlc.gui.helpers.MediaComparators
 import org.videolan.vlc.media.MediaSessionBrowser
+import org.videolan.vlc.util.Permissions.canCheckBluetoothDevices
 import org.videolan.vlc.util.TextUtils
 import org.videolan.vlc.util.VoiceSearchParams
 import org.videolan.vlc.util.awaitMedialibraryStarted
@@ -79,11 +80,13 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
             }
 
             // Bluetooth headset
-            val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
-            if (bluetoothAdapter != null &&
-                BluetoothAdapter.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET) &&
-                isBluetoothHeadsetHardKey(keyEvent)) {
-                return true
+            if (canCheckBluetoothDevices(playbackService.applicationContext)) {
+                BluetoothAdapter.getDefaultAdapter()?.let { bluetoothAdapter ->
+                    if (BluetoothAdapter.STATE_CONNECTED == bluetoothAdapter.getProfileConnectionState(BluetoothProfile.HEADSET)
+                        && isBluetoothHeadsetHardKey(keyEvent)) {
+                        return true
+                    }
+                }
             }
         }
 
@@ -115,7 +118,6 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
                 }
                 KeyEvent.ACTION_UP -> {
                     if (!prevActionSeek) {
-                        val enabledActions = playbackService.enabledActions
                         when (keyEvent.keyCode) {
                             KeyEvent.KEYCODE_MEDIA_NEXT -> onSkipToNext()
                             KeyEvent.KEYCODE_MEDIA_PREVIOUS -> onSkipToPrevious()
diff --git a/application/vlc-android/src/org/videolan/vlc/util/Permissions.kt b/application/vlc-android/src/org/videolan/vlc/util/Permissions.kt
index 306bf0482a..fec89eb09e 100644
--- a/application/vlc-android/src/org/videolan/vlc/util/Permissions.kt
+++ b/application/vlc-android/src/org/videolan/vlc/util/Permissions.kt
@@ -113,6 +113,10 @@ object Permissions {
      */
     fun hasAllAccess(context: Context) = !Intent(android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.fromParts(SCHEME_PACKAGE, context.packageName, null)).isCallable(context) || isExternalStorageManager()
 
+    fun canCheckBluetoothDevices(context: Context): Boolean {
+        return ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED
+    }
+
     @JvmOverloads
     fun canWriteStorage(context: Context = AppContextProvider.appContext): Boolean {
         return if (AndroidUtil.isROrLater) {



More information about the Android mailing list