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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With