How to make your Windows Phone Vibrate using C#

There are many reasons why you might want to programmatically vibrate the phone during apps or games.  You might want to alert the user to an error condition or you want the user to actually feel that he has touched/clicked a button on the Phone.

The Windows Phone SDK makes it easy for developers to control the vibrate function easily, using the VibrateController class. The VibrateController class is defined in the namespace Microsoft.Devices.

The following example shows how to vibrate the phone using C#:

using Microsoft.Devices;

privatevoidbutton1_Click(``objectsender, RoutedEventArgs e)
{
VibrateController vibrate = VibrateController.Default;
vibrate.Start(TimeSpan.FromMilliseconds(1000));
}

That’s all that you need to do! You can change the vibrate duration — this example uses 1000 milliseconds.
Of course, common-sense best practices: don’t overdo the use of vibration, and give the user the option to disable.

You can use the methods Start and Stop of VibrateController to switch on/off the Vibrate controller.

vibrate.Stop();

Technorati Tags: C#,Vibrate,Windows Phone,VibrateController