Skip to content

Latest commit

 

History

History
87 lines (52 loc) · 2.97 KB

File metadata and controls

87 lines (52 loc) · 2.97 KB
title description ms.date no-loc
Vibration
Learn how to use the .NET MAUI IVibration interface, which lets you start and stop the vibrate functionality for a desired amount of time.
02/02/2023
Microsoft.Maui
Microsoft.Maui.Devices

Vibration

Browse sample. Browse the sample

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) xref:Microsoft.Maui.Devices.IVibration interface. This interface lets you start and stop the vibrate functionality for a desired amount of time.

The default implementation of the IVibration interface is available through the xref:Microsoft.Maui.Devices.Vibration.Default?displayProperty=nameWithType property. Both the IVibration interface and Vibration class are contained in the Microsoft.Maui.Devices namespace.

Get started

To access the Vibration functionality, the following platform specific setup is required.

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 using directives:

    [assembly: UsesPermission(Android.Manifest.Permission.Vibrate)]

    - or -

  • Update the Android Manifest:

    Open the Platforms/Android/AndroidManifest.xml file and add the following in the manifest node:

    <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.

No setup is required.

No setup is required.


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 seven seconds using the xref:Microsoft.Maui.Devices.IVibration.Vibrate(System.TimeSpan):

:::code language="csharp" source="../snippets/shared_1/DeviceDetailsPage.xaml.cs" id="vibrate":::

Platform differences

This section describes the platform-specific differences with the vibration API.

No platform differences.

  • Only vibrates when device is set to "Vibrate on ring".
  • Always vibrates for 500 milliseconds.
  • Not possible to cancel vibration.

No platform differences.