I am new to Android development. There is a need to provide support for game controllers for Android c version 4.3 and higher. I found the information on developer.android.com, but it's hard to figure out with it, the downloaded example has a lot of unnecessary, for example, interfaces for backward compatibility with Android 3.1 and so on. Is there a simple beginners guide on this topic? Could you share links and more. Any useful information on this topic.

PS Now I'm trying to connect a gamepad from PS4, but as I understand it, they work according to standards, and having provided support for one, the rest should work in principle.

    1 answer 1

    You must override 2 methods:

    public boolean onGenericMotionEvent(MotionEvent event){} public boolean onKeyDown(int keyCode, KeyEvent event){} 

    Using the first one, you can get values ​​from the sticks, and with the help of the second one you can get the id of the pressed button.

    An example of using onKeyDown:

     @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((event.getSource() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) { switch(keyCode){ case 109: // рСакция Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ с ΠΊΠΎΠ΄ΠΎΠΌ 109 break; case 108: //рСакция Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ с ΠΊΠΎΠ΄ΠΎΠΌ 108 break; } return true; } return super.onKeyDown(keyCode, event); } 

    Using onGenericMotionEvent is a bit more complicated, I will not describe everything in detail, you can read about it here . Sample code from Android Developer, the output is from [-1: 1] along the x and y axes. The final values ​​are obtained in the processJoystickInput method

      public boolean onGenericMotionEvent(MotionEvent event) { // Check that the event came from a game controller if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK && event.getAction() == MotionEvent.ACTION_MOVE) { // Process all historical movement samples in the batch final int historySize = event.getHistorySize(); // Process the movements starting from the // earliest historical position in the batch for (int i = 0; i < historySize; i++) { // Process the event at historical position i processJoystickInput(event, i); } // Process the current movement sample in the batch (position -1) processJoystickInput(event, -1); return true; } return super.onGenericMotionEvent(event); } private static float getCenteredAxis(MotionEvent event, InputDevice device, int axis, int historyPos) { final InputDevice.MotionRange range = device.getMotionRange(axis, event.getSource()); // A joystick at rest does not always report an absolute position of // (0,0). Use the getFlat() method to determine the range of values // bounding the joystick axis center. if (range != null) { final float flat = range.getFlat(); final float value = historyPos < 0 ? event.getAxisValue(axis): event.getHistoricalAxisValue(axis, historyPos); // Ignore axis values that are within the 'flat' region of the // joystick axis center. if (Math.abs(value) > flat) { return value; } } return 0; } private void processJoystickInput(MotionEvent event, int historyPos) { InputDevice mInputDevice = event.getDevice(); // Calculate the horizontal distance to move by // using the input value from one of these physical controls: // the left control stick, hat axis, or the right control stick. float x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_X, historyPos); if (x == 0) { x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_X, historyPos); } if (x == 0) { x = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Z, historyPos); } // Calculate the vertical distance to move by // using the input value from one of these physical controls: // the left control stick, hat switch, or the right control stick. float y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_Y, historyPos); if (y == 0) { y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_HAT_Y, historyPos); } if (y == 0) { y = getCenteredAxis(event, mInputDevice, MotionEvent.AXIS_RZ, historyPos); } // x ΠΈ y Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ осСй Ρ… ΠΈ y соотвСтсвСнно. }