Search code examples
c#windows-phone-8windows-runtimewindows-phone-8.1windows-phone

Windows Phone 8.1 VibrationDevice doesn't stop vbration


I found a following issue of vibration device on Windows Phone 8.1 XAML application.

The code

var vibrationDevice = VibrationDevice.GetDefault();
vibrationDevice.Vibrate(TimeSpan.FromMilliseconds(50));

The first time when it's called, the vibration starts and doesn't stop until the next call. The following calls works correct - phone vibrates for short time and stops.

It reproduces on Lumia 920 at 100% calls and at Lumia 930 at 1% of calls. It reproduces on WP 8.0 and WP 8.1


Solution

  • This is a bug in the framework. What I use is to cancel it in a task after a delay which is the same as the vibration duration:

    private const int DefaultVibrationDuration = 20;
    
    if (this.EnableVibration)
    {
        VibrationDevice.GetDefault().Vibrate(TimeSpan.FromMilliseconds(DefaultVibrationDuration));
    
        // Run task to cancel vibration (this is an error in phone framework where the vibration does
        // not stop after running the first time)
        Task.Run(async () =>
        {
            await Task.Delay(DefaultVibrationDuration);
            VibrationDevice.GetDefault().Cancel();
        });
    }