How to vibrate device n number of times through programming in android?
Asked Answered
T

9

12

can anyone tell me how to vibrate same patter 5 times like this my pattern

long[] pattern = { 0, 200, 500 };

i want this pattern to repeat 5 times

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
Task answered 4/5, 2011 at 7:50 Comment(1)
Ok, does this code work? Have you tried it?Crandall
T
23

I found the solution, it was very simple:

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , -1);
Task answered 12/5, 2011 at 10:39 Comment(3)
Looks like a hack ^^Oneill
great hack nice :)Entomologize
Why is it a hack? There is a pattern parameter and there is a number of repeating parameter. vibrate(...) method documentation says To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating. Everything done according to documentation.Keek
D
5

From: Android Vibrator#vibrate(long[], int)

To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating.

You have to init index 0

long[] pattern = { 0, 100, 500, 100, 500, 100, 500, 100, 500, 100, 500};
vibrator.vibrate(pattern , 0);
Devault answered 7/9, 2017 at 21:6 Comment(0)
S
4

the following works for me:

if(vibration_enabled) {
    final Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    if(v.hasVibrator()) {
        final long[] pattern = {0, 1000, 1000, 1000, 1000};
        new Thread(){
            @Override
            public void run() {
                for(int i = 0; i < 5; i++){ //repeat the pattern 5 times
                    v.vibrate(pattern, -1);
                    try {
                       Thread.sleep(4000); //the time, the complete pattern needs
                    } catch (InterruptedException e) {
                        e.printStackTrace();  
                    }
                }
            }
        }.start();
    }
}

The vibrate method only starts the vibration, but doesn't wait until its executed.

Sixfooter answered 9/9, 2013 at 22:36 Comment(3)
Looks like another hack. What is the second argument of the vibrate() method for?Oneill
Documentation for the vibrate-method , second arg: repeat count, -1 means dont repeatSixfooter
Bad practice! No need to run loops to achieve repetition of vibratorFaxen
D
3

Your code should do the trick. Just make sure you have <uses-permission android:name="android.permission.VIBRATE"/> in the AndroidManifest.xml file.

Dissimilitude answered 4/5, 2011 at 7:57 Comment(2)
its already working but i dont know how to vibrate it n number of times like if select 5 on runtime then it should vibrate 5 times...Task
for(int i = 0; i < n; i++) vibrator.vibrate(pattern, -1);Dissimilitude
B
2

Besides the above given solutions, i have created my own vibration pattern where i can control the duration size between vibrations. startVibration() creates a continous regular vibration pattern for one minute.

stopVibration() - Terminates the vibration or pauses the counterTimer thus pausing the vibration pattern.

private time = 0;
private countDownTimer;

private void startVibration() {
    time = (int) System.currentTimeMillis();

    countDownTimer = new CountDownTimer(60000, 1000) {

        public void onTick(long millisUntilFinished) {

            time = (int) (millisUntilFinished / 1000);
            int[] timeLapse = {58, 55, 52, 49, 46, 43, 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 7, 4, 1};
            for (int k = 0; k < timeLapse.length; k++) {
                if (time == timeLapse[k]) {
                    ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(1000);
                }
            }
        }

        public void onFinish() {
        }
    }.start();
}

private void stopVibration() {
    if (countDownTimer != null) {
        countDownTimer.cancel();
    }
}
Blowhard answered 3/7, 2017 at 8:28 Comment(0)
D
0

In the method public void vibrate (long[] pattern, int repeat), the long[] pattern follows the rule : long[] pattern = {pauseTime1, vibrationTime1, pauseTime2, vibrationTime2, pauseTime3, vibrationTime3, ...} so as a result you have odd number of values, it won't works. You must have to end the pattern with a vibrationTime. An even number of value do the job (at least 4 values).

long[] pattern = {0, 500, 200, 500}
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(pattern , 5);
Definitive answered 7/12, 2018 at 9:22 Comment(0)
E
0

Here's how I did it. I dynamically generate my timing array based on the number of times the caller wants it to vibrate. In the loop, start at 1 so to avoid 0 % 2 causing an extra useless delay at the end.

private void vibrate(int times)
{
    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        ArrayList<Long> timings = new ArrayList();

        timings.add(0L);

        for(int i = 1; i <= times; i ++)
        {

            if(i%2==0)
                timings.add(0L);

            timings.add(250L);


        }

        long[] arrTimings = new long[timings.size()];

        for(int j = 0; j < timings.size(); j++)
        {
            arrTimings[j] = timings.get(j);
        }

        vibrator.vibrate(VibrationEffect.createWaveform(arrTimings, -1));
    }
}
Edan answered 13/2, 2019 at 21:7 Comment(0)
W
0
    long vibrationDuration = Arrays.stream(pattern).sum();
    new CountDownTimer(vibrationDuration*(repeat+1), vibrationDuration) {

        @Override
        public void onTick(long millisUntilFinished) {
            v.cancel();
            if (Build.VERSION.SDK_INT >= 26) {
                VibrationEffect vibrationEffect = VibrationEffect.createWaveform(pattern, -1);
                v.vibrate(vibrationEffect);
            } else {
                v.vibrate(pattern, -1);
            }
        }

        @Override
        public void onFinish() {
            v.cancel();
        }
    }.start();
Wearable answered 23/8, 2019 at 10:19 Comment(1)
Brackets and parens aren't balanced here, are them?Pacorro
H
0

You can apply one trick, just build pattern dynamic based on number of repetition you want.

private long[] createVibrationPattern(long[] oneShotPattern, int repeat) {
    long[] repeatPattern = new long[oneShotPattern.length * repeat];
    System.arraycopy(oneShotPattern, 0, repeatPattern, 0, oneShotPattern.length);
    for (int count = 1; count < repeat; count++) {
        repeatPattern[oneShotPattern.length * count] = 500; // Delay in ms, change whatever you want for each repition
        System.arraycopy(oneShotPattern, 1, repeatPattern, oneShotPattern.length * count + 1, oneShotPattern.length - 1);
    }
    return repeatPattern;
}

Then do the call like below

long[] pattern = { 0, 200, 500 };
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(createVibrationPattern(pattern , 2);

OUTPUT Pattern would become as 0, 200, 500, 500, 0, 200, 500

Herschelherself answered 17/7, 2020 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.