'Android: How to capture key events from Bluetooth headphones?

I am trying to capture KEYCODE_MEDIA_PLAY_PAUSE key events from Bluetooth headphones. I tried to override onKeyDown in the MainActivity to get the keyCode, but I only got events from the phone, never from the Bluetooth headphones. I found some similar questions, but couldn't find any solution. Thanks!

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    Timber.d("$keyCode")
    when (keyCode) {
        KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE -> {               
            Timber.d("Received")
            return true
        }
    }
    return super.onKeyDown(keyCode, event)
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source