How to get half a millisecond vibration on ios in react native with expo?

Viewed 2281

How to get half a millisecond vibration on ios in react native with expo? Similar to the vibration you get when typing on a keypad of a phone. I seem to get it on android with this Vibration.vibrate([0, 0, 0, 30]) but it does not replicate the same on ios

here is the code below

<TouchableOpacity onPress={() => Vibration.vibrate([0, 0, 0, 30])} style={{ marginTop: 100, alignItems: 'center' }}>
        <Text style={{ fontSize: 20 }}>Vibrate</Text>
</TouchableOpacity>
2 Answers

vibration in react-native is platform specific.

import { Vibration, Platform } from "react-native";

// vibrate is platform specific. default is 400ms
  const vibrate = () => {
    if (Platform.OS === "ios") {
      // this logic works in android too. you could omit the else statement
      const interval = setInterval(() => Vibration.vibrate(), 1000);
      // it will vibrate for 5 seconds
      setTimeout(() => clearInterval(interval), 5000);
    } else {
    
      Vibration.vibrate(5000);
    }
  };

Then use it as callback:

<TouchableOpacity onPress={vibrate} style={{ marginTop: 100, alignItems: 'center' }}>
        <Text style={{ fontSize: 20 }}>Vibrate</Text>
</TouchableOpacity>
Related