/// <summary> /// Turns off the vibration. /// </summary> /// <remarks> /// You may need to turn off vibration when your app is paused. /// </remarks> public void Stop() { if (_vibrator != null) { _vibrator.Stop(); if (_vibratorTimer != null) { _vibratorTimer.Elapsed -= OnTimedEvent; _vibratorTimer.Stop(); _vibratorTimer.Dispose(); _vibratorTimer = null; } } }
/// <summary> /// Vibrates with a given pattern. /// </summary> /// <remarks> /// You may need to turn off vibration when your app is paused. /// </remarks> /// <param name="durations"> /// The duration values of the duration and intensity pairs. A duration is the number of milliseconds to vibrate. /// </param> /// <param name="intensities"> /// The intensity values of the duration and intensity pairs. /// A intensity is the amount of intensity variation (0 ~ 100). There will be no vibration for the duration of time if intensity < 0. /// </param> /// <param name="repeat">The index at which to repeat. There will be no repetition if repeat < 0.</param> public void Vibrate(int[] durations, int[] intensities, int repeat) { if (durations == null) { throw new ArgumentNullException(nameof(durations)); } if (intensities == null) { throw new ArgumentNullException(nameof(intensities)); } if (durations.Length < 1 || durations.Length <= repeat || durations.Length > intensities.Length) { throw new ArgumentOutOfRangeException(nameof(durations)); } if (intensities.Length < 1) { throw new ArgumentOutOfRangeException(nameof(intensities)); } if (_vibratorTimer != null) { Stop(); } Vibrate(durations[0], intensities[0]); if (durations.Length > 1) { _vibratorTimer = new VibratorTimer { Interval = durations[0], Durations = (int[])durations.Clone(), Intensities = (int[])intensities.Clone(), Repeat = repeat, CurrentIndex = 1, AutoReset = false }; _vibratorTimer.Elapsed += OnTimedEvent; _vibratorTimer.Start(); } }
private void OnTimedEvent(object source, ElapsedEventArgs e) { VibratorTimer timer = (VibratorTimer)source; int index = timer.CurrentIndex; if (index == timer.Durations.Length && timer.Repeat >= 0) { index = timer.Repeat; } if (index < timer.Durations.Length) { Vibrate(_vibratorTimer.Durations[index], _vibratorTimer.Intensities[index]); _vibratorTimer.Interval = _vibratorTimer.Durations[index]; _vibratorTimer.CurrentIndex = index + 1; _vibratorTimer.Start(); } else { Stop(); } }