4

I'm trying to make the device vibrate in a pre-defined pattern, which is defined in the VibrationEffect class, with patterns like EFFECT_CLICK, EFFECT_POP, and others. I noticed that they are all annotated by @hide, It seems like there's no public method for me to get these patterns, VibrationEffect.get() doesn't work.

So how should I get such patterns? Or is it not possible at all? I also tried to dig into the Android source code to find these patterns, I'm particularly interested in the pre-defined Ringtone vibration patterns, but I can't seem to find them, all I can find is the interface package that defines vibration patterns. Can someone point me the right way if I'm doing this wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jack
  • 5,354
  • 2
  • 29
  • 54

2 Answers2

3

You can obtain any pattern using VibrationEffect.createPredefined(int), example:

val vibrator = context?.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator
val effect: VibrationEffect = VibrationEffect.createPredefined(VibrationEffect.EFFECT_CLICK)
vibrator?.vibrate(effect)

Note that VibrationEffect.EFFECT_CLICK can be replaced with other values that are mentioned in AOSP reference. Minimal required API is 29 (Android 10).

mroczis
  • 1,879
  • 2
  • 16
  • 18
0

I also wanted to recreate these vibration patterns, so I did some trial and error to get these numbers. They are as close to what I felt was right.

        val singleClickPattern = longArrayOf(0, 10)
        val singleClickAmplitude = intArrayOf(0, 180)

        val doubleClickPattern = longArrayOf(0, 10, 160, 10)
        val doubleClickAmplitude = intArrayOf(0, 255, 0, 255)

        val heavyClickPattern = longArrayOf(0, 13)
        val heavyClickAmplitude = intArrayOf(0, 255)

        val tickPattern = longArrayOf(0, 5)
        val tickAmplitude = intArrayOf(0, 50)

Using them like this:

vibrationEffect = VibrationEffect.createWaveform(tickPattern, tickAmplitude, -1)
vibrator.vibrate(vibrationEffect);

You can try them out yourself by looping through the predefined ones and these ones.

yeu0202
  • 21
  • 4