How to capture key events from bluetooth headset with android

前端 未结 3 1582
夕颜
夕颜 2020-12-01 01:42

My app can be controlled by normal headset. It simply overrides \"onKeyDown\". But key events from bluetooth headset are not captured - why? Or how to capture bluetooth key

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 02:10

    Add a broadcast listener to MEDIA_BUTTON:

     
        
     
    

    You should register your broadcast receiver inside your application (not in manifest file). Otherwise Google Music player will catch your broadcast and aboard it.

    Your IntentFilter priority should be higher that other media players priorities in your phone)

    Add android.permission.BLUETOOTH permission in manifest to support Bluetooth headset

    After received you key you have to manually abort the broadcast using abortBroadcast().

    However priorities and abortBroadcast() work fine as long as each app only responds while e.g. something is played. But several users also expect a "default player" to be launched (or start playing) upon button press, like the default player, so it might happen some app with a higher priority number won't let the intent come through to your app

    In the onReceive, you can get the button event with

    KeyEvent key = (KeyEvent) 
    intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
    

    key.getKeyAction() tells you whether the button was released or pressed, key.getKeyCode() tells which button is pressed.

    If you want to handle single button cable headsets as well, also regard the key code KEYCODE_HEADSETHOOK

    Override the onKeyDown method in any activity and check for the KeyEvent.KEYCODE_MEDIA_KEYCODE_pressed_key

    e.g.

     boolean onKeyDown(int keyCode, KeyEvent event) { 
                AudibleReadyPlayer abc; 
                switch (keyCode) { 
                case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD: 
                        // code for fast forward 
                        return true; 
                case KeyEvent.KEYCODE_MEDIA_NEXT: 
                        // code for next 
                        return true; 
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: 
                        // code for play/pause 
                        return true; 
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS: 
                        // code for previous 
                        return true; 
                case KeyEvent.KEYCODE_MEDIA_REWIND: 
                        // code for rewind 
                        return true; 
                case KeyEvent.KEYCODE_MEDIA_STOP: 
                        // code for stop 
                        return true; 
                } 
                return false; 
        } 
    

    Volume key integration example Android - Volume Buttons used in my application
    This one may need permission

    
    


    Or you can try slimier implementations over the following link

    Android Developer Blog : Handling remote control buttons
    Android Tales : Add Headset button support to your Android application

提交回复
热议问题