1

I'm integrating haptic feedback in my android app which works fine in my Google Pixel 4a phone but its not working at all in Samsung devices. I'm using below snippet for calling it after getting a result from an API call:

activity?.window?.decorView?.performHapticFeedback(HapticFeedbackConstants.CONFIRM, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING)

I have defined permission for Vibrate also in AndroidManifest.xml as:

  <uses-permission android:name="android.permission.VIBRATE" />

Please let me know if it's not possible without any click or touch listener.

Anshul Tyagi
  • 2,076
  • 4
  • 34
  • 65
  • please have a look https://stackoverflow.com/a/13152567/2219208 – Lakhwinder Singh Mar 08 '22 at 07:46
  • @LakhwinderSingh I have already mentioned that I'm calling it after getting a result from an API call not on `ClickListener` or `TouchListener` – Anshul Tyagi Mar 08 '22 at 08:04
  • have you added this to button android:hapticFeedbackEnabled="true" – Lakhwinder Singh Mar 08 '22 at 08:45
  • I'm trying to `performHapticFeedback` on opening a new screen. So there is no user interaction. – Anshul Tyagi Mar 08 '22 at 08:52
  • Have you found a solution for this? If not, I would suggest using `Vibrator` instead of `performHapticFeedback` since no touch is being performed. This answer could help: https://stackoverflow.com/a/17066803/5746918. According to [this issue](https://github.com/flutter/flutter/issues/73987) haptic feedback is not enabled in Samsung devices by default and that will cause the device to not vibrate if you use `performHapticFeedback`. Have you tried this code after enabling touch interactions on said Samsung devices manually? – Ishita Sinha Sep 06 '22 at 07:25
  • Yes @IshitaSinha I had to use vibration with different frequencies. – Anshul Tyagi Sep 06 '22 at 07:53
  • 1
    In that case, please post an answer and accept it so we all can benefit from your solution. :) – Ishita Sinha Sep 06 '22 at 08:06
  • 1
    Yes @IshitaSinha. I have posted the answer. – Anshul Tyagi Sep 07 '22 at 05:24

1 Answers1

3

I didn't find the solution with haptic feedback for Samsung devices so I went with the android's default vibration. Find below the snippet:

    if (Build.VERSION.SDK_INT >= 26) {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(VibrationEffect.createOneShot(250, VibrationEffect.EFFECT_DOUBLE_CLICK))
    } else {
        (context.getSystemService(VIBRATOR_SERVICE) as Vibrator).vibrate(250)
    }
Anshul Tyagi
  • 2,076
  • 4
  • 34
  • 65