Android Question Capturing Bluetooth Remote Media Keys

woodpecker

Member
Licensed User
Longtime User
I am trying to use the media keys on a bluetooth audio receiver to control my app using the reflection library and ACTION_UP.

I can capture media_next and media_previous fine, I can capture the play / pause button by using its key code of 126 but not using KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE, not sure why?

I cannot seem to capture anything from volume up and volume down, they just control the phone volume, how can I capture these?

Also I would like to control the app when the screen goes off, is that possible, here is my key test code:-

B4X:
Sub Activity_Create(FirstTime As Boolean)
    re.Target = Activity
    re.SetOnKeyListener("ActivityOnKey")
    re.RunMethod2("setFocusable", "True",  "java.lang.boolean")
    re.RunMethod2("setFocusableInTouchMode", "True", "java.lang.boolean")
    Activity.RequestFocus
End Sub

Private Sub ActivityOnKey(vTag As Object, keyCode As Int, KeyEvent As Object) As Boolean
    re.Target = KeyEvent
    Dim Action As Int = re.RunMethod("getAction")


    Select Action
        Case Activity.ACTION_DOWN
            Select keyCode
                Case KeyCodes.KEYCODE_MEDIA_PREVIOUS
                    'MsgboxAsync(keyCode,"MEDIA PREVIOUS DOWN")
                    Return(True) 'to cancel the event
                Case KeyCodes.KEYCODE_MEDIA_NEXT
                    'MsgboxAsync(keyCode,"MEDIA NEXT DOWN")
                    Return(True) 'to cancel the event
                Case KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE
                    'MsgboxAsync(keyCode,"MEDIA PLAY / PAUSE DOWN")
                    Return(True) 'to cancel the event
                Case KeyCodes.KEYCODE_VOLUME_UP
                    'MsgboxAsync(keyCode,"VOLUME UP down")
                    Return(True) 'to cancel the event
                Case KeyCodes.KEYCODE_VOLUME_DOWN
                    'MsgboxAsync(keyCode,"VOLUME DOWN down")
                    Return(True) 'to cancel the event
            End Select
        Case Activity.ACTION_UP
            ToastMessageShow(keyCode & KeyEvent,True)
            Select keyCode
                Case KeyCodes.KEYCODE_MEDIA_PREVIOUS
                    MsgboxAsync(keyCode,"MEDIA PREVIOUS")
                    Return(True) 'to cancel the event
                Case KeyCodes.KEYCODE_MEDIA_NEXT
                    MsgboxAsync(keyCode,"MEDIA NEXT")
                    Return(True) 'to cancel the event
                Case 126
                'Case KeyCodes.KEYCODE_MEDIA_PLAY_PAUSE 'DOESN'T WORK!
                    MsgboxAsync(keyCode,"MEDIA PLAY / PAUSE")
                    Return(True) 'to cancel the event
                Case KeyCodes.KEYCODE_VOLUME_UP
                    MsgboxAsync(keyCode,"VOLUME UP up")
                    Return(True) 'to cancel the event
                Case KeyCodes.KEYCODE_VOLUME_DOWN
                    MsgboxAsync(keyCode,"VOLUME DOWN up")
                    Return(True) 'to cancel the event
            End Select
    End Select
    Return False
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is raised when the volume keys are pressed.
B4X:
Sub Activity_KeyUp (KeyCode As Int) As Boolean
   Log(KeyCode)
   If KeyCode = KeyCodes.KEYCODE_VOLUME_DOWN Or KeyCode = KeyCodes.KEYCODE_VOLUME_UP Then
       Return True
   End If
   Return False
End Sub

While it should also consumes the key event, the volume is still changing on Android 8.
 
Upvote 0

woodpecker

Member
Licensed User
Longtime User
Hi Erel,

This seems to only work with the volume keys on the phone, it doesn't recognise a keyup on the bluetooth volume keys?
 
Upvote 0
Top