0

I receive axes position from my bluetooth gamepad controller in dispatchGenericMotionEvent(android.view. MotionEvent) method. My method:

    @Override
public boolean dispatchGenericMotionEvent(final MotionEvent event) {
    if( mPadListener==null ||
            (event.getSource()&InputDeviceCompat.SOURCE_JOYSTICK)!=InputDeviceCompat.SOURCE_JOYSTICK ){
        return super.dispatchGenericMotionEvent(event);
    }

    int historySize = event.getHistorySize();
    for (int i = 0; i < historySize; i++) {
        // Process the event at historical position i
        Log.d("JOYSTICKMOVE",event.getHistoricalAxisValue(MotionEvent.AXIS_Y,i)+"  "+event.getHistoricalAxisValue(MotionEvent.AXIS_Z,i));
    }
    // Process current position
    Log.d("JOYSTICKMOVE",event.getAxisValue(MotionEvent.AXIS_Y)+" "+event.getAxisValue(MotionEvent.AXIS_Z));

    return true;
}

The problem is that when I release all joystick axes, I'm not getting last axes values (0,0) in my log. It stops for example in (0.23,0.11) and appropriate values appear in logcat only after next move event. What's more - situation is the same even if I press normal button (button event is catch by completely other method dispatchKeyEvent(android.view.KeyEvent) )

What's going on ?

1 Answers1

0

You get a MotionEvent.ACTION_MOVE event for the zero position, but the value you receive is not necessarily zero. You need to get the flat range of the joystick, which gives you the values at which we should consider the joystick to be at rest (ie. if we are below the flat range, then we're at the zero position). See getCenteredAxis, which corrects for the flat range (https://developer.android.com/training/game-controllers/controller-input.html):

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;
}
Quig
  • 166
  • 4