Home » Android » java – controlling vibration intensity in android phones? is it possible?

java – controlling vibration intensity in android phones? is it possible?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am developing a game. In which, i want do set different vibration intensities for different events. I just want know if its really possible to control the vibration intensity and duration. Any advice or reference links, could be very helpful. Thanks in advance.

How to&Answers:

I think it depends on what you mean by intensity. You can control the pattern and length of the vibration, but I don’t think you can make it vibrate “stronger”.

http://developer.android.com/reference/android/os/Vibrator.html

Answer:

I’ve made a simple trick to somehow reduce the intensity of vibration. My idea is to interleave vibration intervals with silent intervals. If you have one millisecond of vibration and then one second of silence and so on it seems like it’s one constant vibration but weaker than normal. You can try to increase the silence intervals to make the vibration even weaker. Here goes the code example:

int strong_vibration = 30; //vibrate with a full power for 30 secs
int interval = 1000;
int dot = 1; //one millisecond of vibration
int short_gap = 1; //one millisecond of break - could be more to weaken the vibration
long[] pattern = {
        0,  // Start immediately
        strong_vibration, 
        interval,
        // 15 vibrations and 15 gaps = 30millis
        dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, dot, short_gap, //yeah I know it doesn't look good, but it's just an example. you can write some code to generate such pattern. 
    };

Answer:

PWM can be used to produce a vibration pattern of various pulse widths, resulting in lower average voltage to the vibrator motor (and thus weaker vibration output).

I’ve posted a simple proof of concept method here. This method will generate a pattern with the specified intensity and duration. The transition in that method isn’t quite linear, so I have posted a bounty to hopefully get some alternate suggestions. Will update when I have an even better algorithm.

Answer:

This could help you but It only works for API level 26 or above.

public void vibrate(View view) {
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

        long[] wave_time = {0, 100, 0, 100, 0, 100, 0, 100, 0, 100};
        int[] wave_ampl = {0, 50, 0, 100, 0, 150, 0, 200, 0, 255};

        VibrationEffect vibrationEffect = null;
        vibrationEffect = VibrationEffect.createWaveform(wave_time, wave_ampl, -1);
        vibrator.vibrate(vibrationEffect);
    }

}

Here wave_time array represents two types of times:

  1. Time in which it should be idle (0th index, 2nd index, .etc)
  2. Time In which it should vibrate (1st index, 3rd index, .etc)

wave_ampl array represents the strength of the vibration w.r.t wave_time array.

Explanation:

Phone waits for 0 ms (0th index of wave_time) and starts vibrating for 100 ms (1st index of wave_time) with intensity of 50 (1st index of wave_ampl).


Similarly for phone vibrates for 100 ms (3rd index of wave_time) with intensity of 100 (3rd index of wave_ampl).


The maximum intensity is 255 in android.

Reference