Vibration
Get started
To access the Vibration functionality, the following platform specific setup is required.
Android
The VIBRATE permission is required, and must be configured in the Android project. This permission can be added in the following ways:
-
Add the assembly-based permission:
Open the Platforms/Android/MainApplication.cs file and add the following assembly attributes after
usingdirectives:[assembly: UsesPermission(Android.Manifest.Permission.Vibrate)]
Add this permission to the native MAUI client project for either Local or Remote hosting.
-
- or -
-
Update the Android Manifest:
Open the Platforms/Android/AndroidManifest.xml file and add the following in the
manifestnode:<uses-permission android:name="android.permission.VIBRATE" />- or -
-
Update the Android Manifest in the manifest editor:
In Visual Studio double-click on the Platforms/Android/AndroidManifest.xml file to open the Android manifest editor. Then, under Required permissions check the VIBRATE permission. This will automatically update the AndroidManifest.xml file.
Vibrate the device
The vibration functionality can be requested for a set amount of time or the default of 500 milliseconds. The following code example randomly vibrates the device between one and five seconds.
private void VibrateStartButton_Clicked(object sender, EventArgs e)
{
int millisecondsToVibrate = new Random().Next(1000, 5001);
Device.Vibration.Vibrate(millisecondsToVibrate);
}
private void VibrateStopButton_Clicked(object sender, EventArgs e) =>
Device.Vibration.Cancel();
The DeviceVibration contract accepts milliseconds in the range 0 to 5000. Check Device.Vibration.IsSupported before offering the action.
Platform differences
This section describes the platform-specific differences with the vibration API.
iOS / Mac Catalyst
- Only vibrates when device is set to "Vibrate on ring".
- Always vibrates for 500 milliseconds.
- Not possible to cancel vibration.