Bluetooth Device Button Press Should Trigger Onclick Listener in the app

拈花ヽ惹草 提交于 2019-12-10 13:22:47

问题


I'm trying to make an app which triggers an on click listener in the app when a button is pressed on the paired bluetooth device. After googling for several hours I think I am unable to send keycode of the button of the bluetooth device to broadcast receiver where if the keycode matchs then i can call the on click listener or maybe my approach and understanding is wrong so Could anyone please guide me or point me towards the right approach? Thanks in advance

Bluetooth device: Bluetooth Selfie Remote AB shutter 3

I want something like this http://www.barbatricks.com/en/android-en/remap-ab-shutter-3-selfie-remote/

I have tried the following links for reference but no success

How to capture key events from bluetooth headset with android

BroadcastReceiver for ACTION_MEDIA_BUTTON not working

How to detect bluetooth call/media button press in android app

http://blog.phonedeveloper.com/2015/04/how-to-receive-bluetooth-broadcast.html


回答1:


The selfie remote shows up in Android as a Bluetooth keyboard, right? Or as a HID (Human Interface Device) in general.

If that's the case then add to the Activity's onCreate();

takeKeyEvents(true);

This is explained in the documentation:

Request that key events come to this activity. Use this if your activity has no views with focus, but the activity still wants a chance to process key events.

Override the onKeyUp() (in your Activity) and assign some actions to the keys that you wish to use:

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {

    switch (keyCode) {

        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            Log.d(this.getClass().getName(), "KEYCODE_MEDIA_PLAY_PAUSE");
            // Do something...

            return true;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            Log.d(this.getClass().getName(), "KEYCODE_MEDIA_PREVIOUS");
            // Do something...

            return true;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            Log.d(this.getClass().getName(), "KEYCODE_MEDIA_NEXT");
            // Do something...

            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            Log.d(this.getClass().getName(), "KEYCODE_VOLUME_DOWN");
            // Do something...

            return true;
        case KeyEvent.KEYCODE_VOLUME_UP:
            Log.d(this.getClass().getName(), "KEYCODE_VOLUME_UP");
            // Do something...

            return true;
        case KeyEvent.KEYCODE_ENTER:
            Log.d(this.getClass().getName(), "KEYCODE_ENTER");
            // Do something...

            return true;
        default:
            return super.onKeyUp(keyCode, event);
    }
}

The onKeyUp() method is explained:

Called when a key was released and not handled by any of the views inside of the activity. So, for example, key presses while the cursor is inside a TextView will not trigger the event (unless it is a navigation to another object) because TextView handles its own key presses.

The default implementation handles KEYCODE_BACK to stop the activity and go back.

Just let the system handle any keys that you don't want to capture. That's done by the default block.

Just check what's the keycode coming from the remote and remove the unnecessary cases. Those are just some candidates for keycodes that a remote might send.

And of course anything that applies to handling keyboards in general will apply to the remote as well. (Assuming it's a HID. But they usually are. Bluetooth headsets with buttons are a completely different story then.)

This will allow you to use the remote in your own app. I don't see why BroadcastReceivers or onClickListeners should be involved, but maybe I missed the point.

If you want something that runs in the background and sends key events to other applications / remaps the remote's key presses to other key codes to trigger system services then that's also a different story.




回答2:


I have created many apps with BT .. Here is my simple proposal for you:

1- to start with BluetoothChat sample : link

2- Define your own ID in ids.xml i.e.:

<resources>
    <id name="keyboard" />
</resources>

3- Keep all common buttons instance in map based on that ID

4- Create bridge as communication channel between devices so you can send data in bi-direction

5- Use that bridge to broadcast events from/to devices

6- When receive event data, just get that button by ID and call:

mView.performClick();

Thats it.




回答3:


Well, Android - Volume Buttons used in my application & Markus Kauppinen's explanation solved my problem. The below code in the MainActivity can help you trigger a function within your app when a button on a AB Shutter 3 selfie remote is clicked. Buttons of the remote are detected in my app as Android button ENTER Keycode 66 & iOS button VOLUME_UP Keycode 24

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    takeKeyEvents(true);
    }

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        switch (keyCode) {

            case KeyEvent.KEYCODE_VOLUME_UP:
                if (action == KeyEvent.ACTION_DOWN) {
                    Log.i("VOL_UP_pressed", String.valueOf(event.getKeyCode()));
                    Toast.makeText(getApplication(), "IOS button clicked", Toast.LENGTH_SHORT).show();
                }                                  
                return true;

            case KeyEvent.KEYCODE_ENTER:
                if(action==KeyEvent.ACTION_DOWN){
                    Log.i("ENTER_pressed", String.valueOf(event.getKeyCode()));
                    Toast.makeText(getApplication(), "ANDROID button clicked", Toast.LENGTH_SHORT).show();
                }
            default:
                return super.dispatchKeyEvent(event);
        }
    }

BUT PLEASE NOTE: It will detect KeyEvents only within your app, not while the app is not running. My next step is to find how to start the app and an on clicklistener within that app when a button is pressed on the paired AB Shutter 3 selfie remote Anyone with suggestions, guidance or any sort of help? Thanks in advance



来源:https://stackoverflow.com/questions/37186555/bluetooth-device-button-press-should-trigger-onclick-listener-in-the-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!