It’s really easy to vibrate a phone from your code. You can do it whenever you like, just don’t overdo it.
VibrateController vibrate = VibrateController.Default; vibrate.Start(TimeSpan.FromMilliseconds(1000));
That’s all you have to do if you want to vibrate the phone for a second. But, what if you want do vibrate several times, with pauses? For example, vibrate for 100 ms, then pause 100 ms, then vibrate again for 200 ms etc? Send yourself a SMS – they don’t do a single vibration, that’s not fun or fancy.
The first idea that comes to mind is to use DispatcherTimer. Vibrate once, start a timer. When it ticks, vibrate again. But that quite annoying, especially if you want to do a short vibration three or four times.
So, here is an idea:
public static void Vibrate(params double[] intervals) { System.Threading.ThreadStart vibrateJob = new System.Threading.ThreadStart(() => _vibrate(intervals)); System.Threading.Thread vibrateThread = new System.Threading.Thread(vibrateJob); vibrateThread.Start(); } private static void _vibrate(params double[] intervals) { VibrateController vc = VibrateController.Default; bool vibrateOn = false; foreach (double interval in intervals) { TimeSpan i = TimeSpan.FromSeconds(interval); if (!vibrateOn) vc.Start(i); Thread.Sleep(i); vibrateOn = !vibrateOn; } }
I’m putting a vibration in a separate thread and using Thread.Sleep to produce a pause. Usage syntax is pretty straightforward: when calling a method, use as much as parameters you want – even ones will define a vibration time (in seconds) and the odd ones will be transformed to pauses. For example, if you want to vibrate for 0.2 secs, than have a pause of 0.1 secs, and then vibrate once more for 0.3 secs, call the method like this:
Vibrate(0.2, 0.1, 0.3);
A few notes about vibrating:
- the shortest vibration / pause is 0.1 seconds
- the longest is 5 seconds, but please don’t use it
- there is no sense in having more than 4 vibration in one sequence, 3 is optimal
- 0.1 seconds in nice for taps
- 0.2 and 0.3 seconds are good choices for an alert
Login