How to Vibrate your WP programmatically?


If you are a phone application developer, you may want to integrate vibration in your application, so that, you can vibrate the phone progra... - Article authored by Kunal Chowdhury on .

If you are a phone application developer, you may want to integrate vibration in your application, so that, you can vibrate the phone programmatically to provide some message to the end user silently in some special occasion.

 

Today in this post, we will learn how to programmatically vibrate your Windows Phone device by learning the SDK API details. Continue learning and provide your feedback.

 

Most of us uses mobile phone these days. As the name suggests, you can take this mobile to anywhere you go. Let’s suppose you are in a board meeting or presenting a seminar and your phone rings. What will happen? You will get distracted. If this happens again and again, others will feel it irritating. You may say here that, you can put the phone on silent mode. But remember, in such case you may loose any important phone call.

 

This is not only phone call but it applies to different occasion where the application developer wants to notify user about some issues, state change or other stuffs. Putting the device in completely silent mode can ignore such things.

 

Board Meeting

 

That’s why, all phone devices support vibration mode. If you are a phone application developer, you may want to integrate the same in your application to notify a user silently by just a vibration. Let’s see what are the APIs that Windows Phone SDK provides to a WPDev.

 

If you are a Windows Phone application developer, you can find a class named “VibrateController” present under the namespace “Microsoft.Devices” and present in the “Microsoft.Phone.dll”. VibrateController class provides two API methods named “Start(…)” and “Stop()” by which you can control the use of Vibration in your Windows Phone application.

 

The Start(..) method takes parameter as TimeSpan and it can vary from 0 second to 5 seconds. If the value provided does not fall under the 0-5 sec(s) range, you will get an ArgumentOutOfRangeException.

 

Here is the meta data of the class:

 

namespace Microsoft.Devices
{
public class VibrateController
{
public void Start(TimeSpan duration);
public void Stop();
 
public static VibrateController Default { get; }
}
}

To get the instance of the class, you have to call the static property named “Default” as mentioned above and then you will be able to call the Start(…) and Stop() method.

 

In case you want to forcefully stop the vibration of the device in between, call the Stop() method. Here are some examples of putting the Windows Phone device in vibration mode:

 

// the below call puts the device to vibration mode for 2 seconds
Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromSeconds(2.0));
 
// the below call puts the device to vibration mode for 3.5 seconds
Microsoft.Devices.VibrateController.Default.Start(TimeSpan.FromSeconds(3.5));

 

The “VibrateController” class internally uses a security safe class called “NativeVibrateController” present under the same namespace “Microsoft.Devices”. The NativeVibrateController class does all the things related to vibration, the VibrateController class is just a wrapper to it.

 

I hope, the post will be helpful for you to understand the use the VibrateController API in your Windows Phone application. The API will not work in simulator as you will not feel any vibration there. You can only test it in a physical device. Drop a line below if you have any queries. Alternatively, connect with me on Twitter and Facebook. Also, don’t forget to subscribe to my blog’s email newsletter and RSS feed. Happy coding.

Have a question? Or, a comment? Let's Discuss it below...

dhgate

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.