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.