Android's - performHapticFeedback vs Vibrator - documentation and use
Asked Answered
P

1

6

TL;DR:

Would appreciate any extra information on Android's

abstract class Vibrator vs performHapticFeedback

Preferably avoiding the use of the Vibrator class and prioritising performHapticFeedback to circumvent having to ask the user for permissions, and rely only on their system's preference.


Scenario:

I'm working with Xamarin trying to implement Haptic Feedback for Android and iOS.

Now, whereas the iOS documentation has a short explanation, which I've implemented as the following:

    void Platform.Vibrate(HapticsIntensity HapticsIntensity)
    {
      UIKit.UIImpactFeedbackGenerator ImpactFeedbackGenerator;

      switch (HapticsIntensity)
      {
        case HapticsIntensity.Light:
          ImpactFeedbackGenerator = new UIKit.UIImpactFeedbackGenerator(UIKit.UIImpactFeedbackStyle.Light);
          break;
        case HapticsIntensity.Medium:
          ImpactFeedbackGenerator = new UIKit.UIImpactFeedbackGenerator(UIKit.UIImpactFeedbackStyle.Medium);
          break;
        case HapticsIntensity.Heavy:
          ImpactFeedbackGenerator = new UIKit.UIImpactFeedbackGenerator(UIKit.UIImpactFeedbackStyle.Heavy);
          break;
        default:
          ImpactFeedbackGenerator = null;
          break;
      };

      if (ImpactFeedbackGenerator != null)
      {
        ImpactFeedbackGenerator.Prepare();
        ImpactFeedbackGenerator.ImpactOccurred();
      }
    }

The Android documentation for Haptic Feedback states that the method performHapticFeedback expects a HapticFeedbackConstant as a parameter.

public boolean performHapticFeedback (int feedbackConstant)

The available feedbackConstant's are here, but they seem to have no difference between them.

Calling:

LongPress

Engine.AndroidActivity.Window.DecorView.PerformHapticFeedback(Android.Views.FeedbackConstants.LongPress);

has the same effect as VirtualKey

Engine.AndroidActivity.Window.DecorView.PerformHapticFeedback(Android.Views.FeedbackConstants.VirtualKey);

or KeyboardTap

Engine.AndroidActivity.Window.DecorView.PerformHapticFeedback(Android.Views.FeedbackConstants.KeyboardTap);

moreover, some of the FeedbackConstants don't even result in haptic feedback.

Does anyone know where I could find any more documentation around this matter?


The reason why I ask is that I am implementing an abstract layer over Xamarin with Invention where my intention is to have my method calls like:

Vibrate(HapticsIntensity.Light);
Vibrate(HapticsIntensity.Medium);
Vibrate(HapticsIntensity.Heavy);

This works today, but where on iOS I get the tactile feedback of Light, Medium and Heavy vibration, on Android, I can't differentiate between them.

Now, I know Android has a Vibrate class (see here), which allows for granular control; however, to use this I need to add to my manifest or ask for specific permissions for my app (android.permission.VIBRATE), and that is not optimal.


Also, if I add the android.permission.VIBRATE permission to my manifest, it seems like (if the device has Haptic Feedback enabled in its settings), I don't even need to add the Vibrate() method call to my buttons` onClick; and they will already provide the tactile feedback (BZZZTT!!1!).

Prebo answered 20/5, 2019 at 3:28 Comment(0)
R
5

It totally depends upon if the device OEM has altered ASOP code and the vibration timing arrays in com.android.internal.R.array resource to enable a special haptic feedback "engine" that they are using on their device.

By default, the hardware OEMs are only required to support (in hardware) a standard on/off vibration (linear actuator, weighted rotary, etc..), not a "true" haptic feedback one which is normally based upon waveforms.

In comparison to the newer iOS devices (7|8+?), they are using the "Taptic Engine" (fancy speak for an "advanced linear actuator") for haptic feedback and only recently are Android devices "catching" up on the hardware side (new OnePlus, Pixel 3s, etc...) are starting to include more advanced haptic/vibration hardware (whether are not the OEM has done any special with that new hardware, you decide...)

So if you look at ASOP's PhoneWindowManager.java you will find that most of the HapticFeedbackConstants get lumped into a few VibrationEffects constants such as:

~~~
VibrationEffect.EFFECT_TICK
VibrationEffect.EFFECT_CLICK
VibrationEffect.EFFECT_HEAVY_CLICK
~~~

Look at the source if you want to see what the ASOP default VibrationEffects would be for a specific HapticFeedbackConstants:

If you have to provide manual-based haptic for your app for some reason, you can use the Vibrator API and provide the byte array for your on/off timing and then special case it for phone devices at offer more hardware features.

Rosenda answered 20/5, 2019 at 4:31 Comment(3)
thank you for your reply! I thought Android would use a different approach. Something OEM agnostic, such as: (assuming the device as a simple motor where only on/off are available) Light: On for 200ms Medium: On for 500ms heavy: On for 1000ms I understand I can control that through Vibrate, but I did not want to ask for the VIBRATE permission. Thanks for this, it really did set me in a better direction,Prebo
Since you mentioned a comparison to apple devices and the "taptic engine" I thought I'd share this article with some interesting relevant info. It'll explain the capabilities of Android haptics: medium.com/lofelt/…Durant
The code you refer to seems to have moved to HapticFeedbackVibrationProvider.javaPleuropneumonia

© 2022 - 2024 — McMap. All rights reserved.