Vibrator - MatheusAlvesA/react-native-androide GitHub Wiki

Using vibration API

The react-native project already have a vibration API, but that one give you a some more features.

You will need to import the module from the library.

import { Vibrator } from 'react-native-androide';

Note 1: This module requires a especial permission: VIBRATE. To grant the required permission, add this line: <uses-permission android:name="android.permission.VIBRATE" /> to the Manifest File located in android\app\src\main\AndroidManifest.xml. If this permission are not guaranteed almost all promises will be rejected.

Note 2: All the functions of this module are async and return Promises. So you can use await to see de results or use .then().

Vibrating

All the vibrations effects are executed by the function Vibrator.vibrate();, but that function can be called with differents type and number of arguments.

Vibrator.vibrate(int time, int intensity);

Calling the vibrate function with two integers arguments(time and intensity) the device will vibrate for time milliseconds and force proportional to intensity. intensity must be a number between 1 and 100, if a different number is provided the default intensity will be used and if intensity has be not provided 100 will be used.

Example:

let time = 1000;
let intensity = 100;
let intensityReproduced = await Vibrator.vibrate(time, intensity);
// Device will vibrate for one second with the maximum intensity

intensityReproduced stands for if the intensity choosed was used or not(default was used insted).

Vibrator.vibrate(Array<number> pattern);

Calling with a array of numbers you can make a vibration effect where the odd indices are the vibration duration, while the even ones are the separation time. Intensity are not suported.

Example:

Vibrator.vibrate([1000, 2000, 3000]);
// Wait 1s -> vibrate 2s -> wait 3s

Vibrator.vibrate([0, 2000, 3000, 4000]);
// Wait 0s -> vibrate 2s -> wait 3s -> vibrate 4s

Stopping

To stop a current vibration call Vibrator.stop()

Useful Functions

The functions on this session does not requires any permission and the promises will never reject.

  • Vibrator.hasVibrator();

This function returns a promise that when resolved give you a boolean that stands for if the device has a vibrator enabled.

  • Vibrator.hasIntensityControl();

This important function returns a promise that when resolved give you a boolean that stands for if the system allow yout to choose what intensity the vibrator will vibrate. If the system does not allow intensity control all the functions still working but the chosen intensity will have no effect.

⚠️ **GitHub.com Fallback** ⚠️