Skip to content

Vibration doesn't respecting the pattern #130

@Alfatist

Description

@Alfatist

Hello guys! I have the following code. In summary, The Vibration.vibrate pattern receives [4900, 100, 4900, 100, 4900, 100, 4900, 100] and intensity, a custom interpolation (it doesn't matter to the issue).

void vibrate() {
    late List<int> patternWaitVibrateMs;
    Duration selectedDuration = const Duration(seconds: 20);

    
    patternWaitVibrateMs = _createListWith4900and100Value(selectedDuration);

    List<int> intensitiesList =
      _generateGradientList(patternWaitVibrateMs.length);

    print(patternWaitVibrateMs); // [4900, 100, 4900, 100, 4900, 100, 4900, 100]
    Vibration.vibrate(
       pattern: patternWaitVibrateMs, intensities: intensitiesList);
  }

List<int> _createListWith100and4900Value(Duration timesToRepeat) {
  List<int> result = [];
  int resultWanted = timesToRepeat.inMilliseconds;
  int sum = 0;
  for (var i = 1; i <= timesToRepeat.inMilliseconds; i++) {
    if (i % 2 == 0) {
      result.add(100);
      sum += 100;
    } else {
      result.add(4900);
      sum += 4900;
    }
    if (sum >= resultWanted) break;
  }
  return result;
}

List<int> _generateGradientList(int lengthPatternWaitVibrate) {
  if (lengthPatternWaitVibrate < 2) {
    throw CustomError("List length must be at least 2");
  }
  List<int> listReturn = List.generate(lengthPatternWaitVibrate, (index) {
    double fraction =
        index / (lengthPatternWaitVibrate - 1); // Fração entre 0 e 1
    return (255 - fraction * (255 - 1)).round(); // Interpolation linear
  });
  return listReturn;
}

It's expected to wait 4900ms and then vibrate 100ms. It keeps vibrating at an unknown pattern, but notable not waiting 4900ms

Platform: Android 13; Samsung S20 FE
print(await Vibration.hasCustomVibrationsSupport()); returns true inside an async func.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions