Cosc 4735
Android Bluetooth Game Controllers
Supporting Game Controllers
• Android has built-in support for them.
– From an Activity/fragment
• dispatchGenericMotionEvent(android.view. MotionEvent)
– Called to process generic motion events such as joystick movements.
• dispatchKeyEvent(android.view.KeyEvent)
– Called to process key events such as a press or release of a gamepad or D-pad
button.
– From View:
• onGenericMotionEvent(android.view.MotionEvent)
– Called to process generic motion events such as joystick movements.
• onKeyDown(int, android.view.KeyEvent)
– Called to process a press of a physical key such as a gamepad or D-pad button.
• onKeyUp(int, android.view.KeyEvent)
– Called to process a release of a physical key such as a gamepad or D-pad
button.
Supporting Game Controllers (2)
• KeyEvent
– An object that describes directional pad (D-pad) and gamepad button events.
– Key events are accompanied by a key code that indicates the specific button
triggered, such as DPAD_DOWN or BUTTON_A.
– You can obtain the key code by calling getKeyCode() or from key event
callbacks such as onKeyDown().
• MotionEvent
– An object that describes input from joystick and shoulder trigger movements.
– Motion events are accompanied by an action code and a set of axis values.
– The action code specifies the state change that occurred such as a joystick
being moved.
– The axis values describe the position and other movement properties for a
specific physical control, such as AXIS_X or AXIS_RTRIGGER.
– You can obtain the action code by calling getAction() and the axis value by
calling getAxisValue().
Do we have Game Controllers?
• You can check to see if you have a game controller connected by
int[] deviceIds = InputDevice.getDeviceIds();
for (int deviceId : deviceIds) {
InputDevice dev = InputDevice.getDevice(deviceId);
int sources = dev.getSources();
– Verify that the device has gamepad buttons, control sticks, or both.
if ( ((sources & InputDevice.SOURCE_GAMEPAD) ==
InputDevice.SOURCE_GAMEPAD)
|| ((sources & InputDevice.SOURCE_JOYSTICK) ==
InputDevice.SOURCE_JOYSTICK)) {
//we have a game controller.
}
}
Do we have Game Controllers? (2)
• A source type of SOURCE_GAMEPAD indicates that the
input device has gamepad buttons (for example,
BUTTON_A).
– Note that this source type does not strictly indicate if the game
controller has D-pad buttons, although most gamepads typically
have directional controls.
• A source type of SOURCE_DPAD indicates that the input
device has D-pad buttons
– for example, DPAD_UP
• A source type of SOURCE_JOYSTICK indicates that the input
device has analog control sticks
– for example, a joystick that records movements along AXIS_X
and AXIS_Y
Generally, how controllers are setup
Key Events
• With the buttons, you use the dispatchKeyEvent(
android.view.KeyEvent) from the activity.
– Figure out if it's the ACTION_DOWN (ie button pressed) or
ACTION_UP (button back to original state, ie lifted).
• As a note the key codes may not match the devices button
names.
–
–
–
–
KeyEvent.KEYCODE_BUTTON_X is the left button.
KeyEvent.KEYCODE_BUTTON_A is the bottom
KeyEvent.KEYCODE_BUTTON_Y is the top
KeyEvent.KEYCODE_BUTTON_B is the right button
JoyStick/Dpad
• It's possible for a device to have both
– one to act is both joystick and dpad.
• onGenericMotionEvent(android.view.MotionE
vent motionEvent)
– Then use the getAxisValue to get the information.
Axis Data
• JoyStick
– xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_X);
– yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_Y);
• Numbers between -1.0 to 1.0 with 0.0 the joystick is in center.
– 0.0, assumes perfect calibration of the joystick.
• GamePad
– xaxis =
motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
– yaxis =
motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);
• Android reports D-pad UP and DOWN presses as AXIS_HAT_Y
events with a range from -1.0 (up), 1.0 (down), and D-pad LEFT or
RIGHT presses as AXIS_HAT_X events with a range from -1.0 (left)
and 1.0 (right). With 0.0 as center.
API and support
• Some parts are supported back to API 12
(android 3.1), but API 16 (android 4.1) is the
lowest API for some code shown here.
• The develop pages show how to get backward
compact to 3.1 and even 2.3.3 if that is
desired.
• https://developer.android.com/training/game
-controllers/compatibility.html
Multiplayer/multi controller.
• You can connect more then on controller at a
time, which allows you to have two more
players at the same time.
– You will need map each controller to each player.
• InputDevices.getDeviceIds() will give you the full list of
device ids, which can be all before "starting" a game to
get player 1 controller, etc.
• KeyEvent and MotionEvent each have a getDeviceID()
method, so when an action has taken place you can
map between the controller to the player.
References
• https://developer.android.com/training/game
-controllers/controller-input.html
• https://developer.android.com/training/game
-controllers/compatibility.html
• https://developer.android.com/training/game
-controllers/multiple-controllers.html
Q&A
© Copyright 2026 Paperzz