Search code examples
androidexceptionvibration

Vibrator.vibrate() throws ArrayIndexOutOfBoundsException


I use the following snippet to vibrate the phone in a specific pattern, but it throws and ArrayIndexOutOfBoundsException.

vibrator.vibrate(new long[] { selectedDuration, CONSTANT_DELAY }, REPEAT); 

But

vibrator.vibrate(VIBRATE_DURATION);

works fine. Any pointers?


Solution

  • The docs say:

    If you want to repeat, pass the index into the pattern at which to start the repeat.

    Means REPEAT is only allowed to be 0 or 1 in your case.

    This is the implementation:

    public void vibrate(long[] pattern, int repeat)
    {
        // catch this here because the server will do nothing.  pattern may
        // not be null, let that be checked, because the server will drop it
        // anyway
        if (repeat < pattern.length) {
            try {
                mService.vibratePattern(pattern, repeat, mToken);
            } catch (RemoteException e) {
            }
        } else {
            throw new ArrayIndexOutOfBoundsException();
        }
    }