Using Vibrate In Android

Nilesh Deokar
ProAndroidDev
Published in
4 min readNov 13, 2017

--

True greatness consists in being great in little things. — Charles Simmons

Vibrate huh? All it takes is one simple Manifest permission and these three lines of code.

Even though vibrate() is deprecated in SDK 26 we are still going to study it in order to understand newly introduced VibrationEffect class.

Want to build own vibration pattern?

Vibrate works on play & pause pattern. So suppose you want to:

  • Vibrate a phone for 400 ms then pause for 200 ms
  • And again vibrate for 400 ms then our pattern becomes : {400, 200, 400};
  • Vibrator treats the first element of an array as a delay in ms. It would start vibrating a device after array[0] ms.
  • If you don’t want any initial delay then your array becomes :
    {0, 400, 200, 400};

By passing -1 to the vibrate() we are asking to play it just once. If we want to repeat this pattern we can just pass 0.

// 0 : Repeat this pattern from 0th element.
vibrator.vibrate(mVibratePattern, 0);

To cancel vibration at any time call : vibrator.cancel()

Repeat vibration pattern from a specific index?

Consider this example of the vibration pattern:

long[] mVibratePattern = new long[]{0,400,800,600,800,800,800,1000};

Here :

  • FirstElement 0 : Initial delay 0 ms
  • SecondElement 400 : Vibrate for 400 ms
  • ThirdElement 800 : Pause for 800 ms
  • FourthElement 600 : Vibrate for 600 ms
  • FifthElement 800 : Pause for 800 ms
  • SixthElement 800 : Vibrate for 800 ms
  • SeventElement 800 : Pause for 800 ms
  • EightElement 1000 : Vibrate for 1000 ms

Here we are increasing vibrate time by 200 ms. Suppose we want to repeat vibration from 600 onwards for which we will pass index position of 600 to vibrate() So for repeating vibration from specific index it becomes :

// 3 : Repeat this pattern from 3rd element of an array
vibrator.vibrate(mVibratePattern, 3);

Play Vibration Exactly Once

Now that you know for repeating vibration from specific index we need to pass its array position tovibrate(). Since we don’t want to repeat the pattern we can simply pass the -1 as the repeating position which would be out of bounds and hence it would stop.

// -1 : Play exactly once
vibrator.vibrate(mVibratePattern, -1);

Now let’s move to newly introduced VibrationEffect class which has three methods for creating vibrations.

In order to understand these methods in detail we will look into what exactly amplitude is.

Amplitude

Amplitude is used to describe the peak value.

Illustration of an Amplitude

In vibration negative values indicates no vibration so let’s ignore that part.

So :

  • If you want to control the strength of the vibration you need to use AMPLITUDE parameter.
  • In android vibration AMPLITUDE 0 says vibration motor is off and 255 says vibration would be performed at its full strength.

createOneShot(long vibrateTimeInMills, int amplitude)

As the name suggests these methods would simply create a vibration which would constantly vibrate the device for the time specified in vibrateTimeInMills.

Snippet for creating one shot vibration

To lower the strength of the vibration we need to play around the AMPLITUDE values by passing 50,100,150… remember 255 is highest and 0 means motor is off.

createWaveForm(long[] vibratePattern, int repeat)

This works exactly the same as the vibrate(mVibratePattern,mRepeatIndex);

createWaveFormVibrationEffect

createWaveForm(long[] vibratePattern, int[] amplitudes, int repeat)

It uses the array of AMPLITUDES to decide the strength of the vibration pattern. 0 being the lowest value and 255 being the highest value.

The above-mentioned pattern would vibrate the phone at its full strength exactly 4 times since in AMPLITUDES array it has 4 x 255 and rest are 0 which means it won’t play vibration on those occurrences.

Troubleshooting

  • Check if your app has permission to VIBRATE in manifest.
  • Check if your device has a vibrator by vibrator.hasVibrator();
  • If you are doing amplitude control: check for vibrator.hasAmplitudeControl()
  • Array size of mVibratePattern and mAmplitudes must be of the same length.

Conclusion

Although it is tempting to use vibration extensively to get user’s attention to your app, use it wisely.

Seriously there was no plan to write on Android Vibrator class but while adding vibration to the notification I observed the @deprecated annotation on the vibrate(). Which led me to the discovery of this class and the newly introduced VibrationEffect class in SDK 26. This is how @deprecated is useful in android.

Have a look at the complete code here: https://gist.github.com/nieldeokar/e05fffe4d639dfabf0d57e96cb8055e2

Feel free to discuss, critique, and share.

My LinkedIn, Twitter. Let’s connect. :)

--

--