Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 656
Jack Avatar asked Sep 14 '25 17:09

Jack


1 Answers

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

like image 142
mroczis Avatar answered Sep 16 '25 07:09

mroczis