Android Bluetooth Game Controllers Support and Setup Guide

Slide Note
Embed
Share

This guide explores the built-in support for game controllers in Android devices, detailing how to process joystick movements and key events. It covers KeyEvent and MotionEvent objects, checking for connected game controllers, and understanding different source types like SOURCE_GAMEPAD and SOURCE_JOYSTICK. Additionally, it provides insights into setting up controllers effectively.


Uploaded on Sep 11, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Cosc 4735 Android Bluetooth Game Controllers

  2. 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.

  3. 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().

  4. 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. } }

  5. 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

  6. Generally, how controllers are setup

  7. 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 Note, I've found that some devices don't seem to present the right codes for A,B,X and Y.

  8. JoyStick/Dpad It's possible for a device to have both one to act is both joystick and dpad. onGenericMotionEvent(android.view.MotionEvent motionEvent) Then use the getAxisValue to get the information.

  9. 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.

  10. 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

  11. 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.

  12. 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

  13. QA &

Related


More Related Content