3

I'm trying to use a vibration pattern when vibrating the phone. I'm using:

Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
                // Vibrate for 500 milliseconds
                long[] longs = { 2, 0, 0, 0, 2, 0 , 0, 0, 2 };
                v.vibrate(longs, 1);

and it just doesn't stop vibrating.

  1. If I use v.vibrate(longs, -1); it doesn't vibrate at all.
  2. If I use v.vibrate(longs, 0); it doesn't vibrate at all.
  3. If I use v.vibrate(longs, 2); or any number higher than 1, it vibrates indefinitely.
  4. If I change the long values to so they're higher or lower it makes no difference.

I've read the documentation and some tutorials and I don't think I've done anything wrong here. Why isn't it vibrating properly?

Note: I use other apps that vibrate in patterns properly so I know it's not an issue with my phone.

Community
  • 1
  • 1
RǢF
  • 806
  • 5
  • 17
  • I'm adding my own answer as I have found a solution. I don't understant it though, so if somebody could add an explanation of _why_ this code I'm about to add works as expected then that'd be great. – RǢF Jan 12 '15 at 20:38
  • I don't see the relation between the comment `// Vibrate for 500 milliseconds ` and the code. – njzk2 Jan 12 '15 at 20:41
  • @njzk2 That's irrelavent. I just haven't removed the comment after changing the code a little. – RǢF Jan 12 '15 at 20:43

3 Answers3

5

You should read the documentation for vibrate().

With 2, 0, 0, 0, 2, 0 , 0, 0, 2, you are saying: "wait for 2 ms, vibrate for 0 ms, wait for 0 ms, vibrate for 0 ms, wait for 2 ms, vibrate for 0 ms, wait for 0 ms, vibrate for 0 ms, wait for 2 ms". Obviously, this pattern never vibrates, unless you repeat the pattern (which has an odd number of intervals).

When you pass anything other than -1 as the second argument, the pattern is repeated using the second argument as the index into the pattern at which to start repeating. Since you never seem to call v.cancel(), this repetition never ends, causing endless vibration (because at some point in the repetition, you will have non-0 vibration interval).

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Thank you @cybersam, I did originally read the documentation. Don't know how I managed to miss the part where they talk about the on-off's. – RǢF Jan 12 '15 at 20:42
2

Why isn't it vibrating properly?

Well...

First, zero milliseconds is a really short time. Users will not notice a vibration that is happening for zero milliseconds, and users will not notice a gap between vibrations that is happening for zero milliseconds.

Second, two milliseconds is a really short time. Your entire pattern takes six milliseconds for each pass (with a repeat value of 0), or four milliseconds for the second and subsequent passes (with a repeat value of 1, since that skips the first element of your pattern, which is one of your three two-millisecond values). There are hundreds of these patterns per second, which is not going to be discernable by the user.

I suggest that you get rid of the zeros, and use reasonable numbers of milliseconds for the rest. This is on top of cybersam's suggestions of eventually calling cancel() and thinking through whether you want an even or odd number of elements in your pattern.

tung
  • 719
  • 2
  • 14
  • 30
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

The code from the second answer on this page is working for me. It vibrates in a pattern and then stops once the pattern is finished:

// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

// Start without a delay
// Vibrate for 100 milliseconds
// Sleep for 1000 milliseconds
long[] pattern = {0, 100, 1000};

// The '0' here means to repeat indefinitely
// '0' is actually the index at which the pattern keeps repeating from (the start)
// To repeat the pattern from any other point, you could increase the index, e.g. '1'
v.vibrate(pattern, 0);
Community
  • 1
  • 1
RǢF
  • 806
  • 5
  • 17