We must take advantage and use the functionalities of our devices, through vibration we have access to inform actions / processes to our users through a physical reaction. Today we will learn in a very simple and quick way how to send vibrations from .NET MAUI!
First of all… What do I need?
Let’s start by adding some platform settings. To implement them, follow the instructions added below:
We have two ways to do it, let’s see:
Go to Platform
Android
AndroidManifes.cs and add the following Permission:
<uses-permission android:name="android.permission.VIBRATE" /> |
Or just go to your AssemblyInfo.cs and add the following:
[assembly: UsesPermission(Android.Manifest.Permission.Vibrate)] |
For iOS/macOS and Windows you don’t need an additional configuration.
Let’s start!
The Vibration can be requested for a certain time or the default value of 500 milliseconds, let’s see how to do it:
Sending Vibration
Through the .Default property we can access the Vibration method which allows us to send vibrations to our device, it accepts a TimeSpan as a parameter that represents a time interval.
private void SendVibrationButton_Clicked(object sender, EventArgs e) => | |
Vibration.Default.Vibrate(TimeSpan.FromSeconds(3)); |
Canceling Vibration
But also, we have the Cancel method that allows us to stop the vibration emitted.
private void CancelVibration_Clicked(object sender, EventArgs e) => | |
Vibration.Default.Cancel(); |
Platform differences
- Only vibrates for 500 milliseconds.
- Once started, vibration cannot be cancelled.
- It only vibrates when the device is set to “Vibrate when ringing”.
For Android and Windows you don’t have platform diferences.
Spanish post: https://es.askxammy.com/vibracion-en-net-maui-/
3 thoughts on “Vibrations in .NET MAUI
”