Search code examples
androidandroid-sourceandroid-vibration

How to get pre-defined vibration patterns?


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?


Solution

  • 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).