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

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

This approach only applies to Wisej.NET Hybrid Local applications.

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

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.

private void VibrateStartButton_Clicked(object sender, EventArgs e)
{
    int millisecondsToVibrate = Random.Shared.Next(1000, 7000);
    
    Device.Vibration.Vibrate(vibrationLength);
}

private void VibrateStopButton_Clicked(object sender, EventArgs e) =>
    Device.Vibration.Cancel();

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.

Last updated