88 *
99 * @providesModule Vibration
1010 * @flow
11+ * @jsdoc
1112 */
1213'use strict' ;
1314
1415var RCTVibration = require ( 'NativeModules' ) . Vibration ;
1516var Platform = require ( 'Platform' ) ;
1617
1718/**
19+ * @class
20+ * @description
1821 * The Vibration API is exposed at `Vibration.vibrate()`.
1922 * The vibration is asynchronous so this method will return immediately.
2023 *
2124 * There will be no effect on devices that do not support Vibration, eg. the simulator.
2225 *
23- * **Note for android **
26+ * **Note for Android: **
2427 * add `<uses-permission android:name="android.permission.VIBRATE"/>` to `AndroidManifest.xml`
2528 *
26- * **Android Usage:**
29+ * Since the **vibration duration in iOS is not configurable**, so there are some differences with Android.
30+ * In Android, if `pattern` is a number, it specified the vibration duration in ms. If `pattern`
31+ * is an array, those odd indices is the vibration duration, while the even one are the separation time.
2732 *
28- * [0, 500, 200, 500]
29- * V(0.5s) --wait(0.2s)--> V(0.5s)
33+ * In iOS, invoking `vibrate(duration)` will just ignore the duration and vibrate for a fixed time. While the
34+ * `pattern` array is used to define the duration between each vibration. See below example for more.
3035 *
31- * [300, 500, 200, 500]
32- * --wait(0.3s)--> V(0.5s) --wait(0.2s)--> V(0.5s)
36+ * Repeatable vibration is also supported, the vibration will repeat with defined pattern until `cancel()` is called.
3337 *
34- * **iOS Usage:**
35- * if first argument is 0, it will not be included in pattern array.
38+ * Example:
39+ * ```
40+ * const DURATION = 10000
41+ * const PATTERN = [1000, 2000, 3000]
3642 *
37- * [0, 1000, 2000, 3000]
38- * V(fixed) --wait(1s)--> V(fixed) --wait(2s)--> V(fixed) --wait(3s)--> V(fixed)
43+ * Vibration.vibrate(DURATION)
44+ * // Android: vibrate for 10s
45+ * // iOS: duration is not configurable, vibrate for fixed time (about 500ms)
46+ *
47+ * Vibration.vibrate(PATTERN)
48+ * // Android: wait 1s -> vibrate 2s -> wait 3s
49+ * // iOS: wait 1s -> vibrate -> wait 2s -> vibrate -> wait 3s -> vibrate
50+ *
51+ * Vibration.vibrate(PATTERN, true)
52+ * // Android: wait 1s -> vibrate 2s -> wait 3s -> wait 1s -> vibrate 2s -> wait 3s -> ...
53+ * // iOS: wait 1s -> vibrate -> wait 2s -> vibrate -> wait 3s -> vibrate -> wait 1s -> vibrate -> wait 2s -> vibrate -> wait 3s -> vibrate -> ...
54+ *
55+ * Vibration.cancel()
56+ * // Android: vibration stopped
57+ * // iOS: vibration stopped
58+ * ```
3959 */
4060
4161var _vibrating : boolean = false ;
@@ -74,6 +94,11 @@ function vibrateScheduler(id, pattern: Array<number>, repeat: boolean, nextIndex
7494}
7595
7696var Vibration = {
97+ /**
98+ * Trigger a vibration with specified `pattern`.
99+ * @param pattern Vibration pattern, accept a number or an array of number. Default to 400ms.
100+ * @param repeat Optional. Repeat vibration pattern until cancel(), default to false.
101+ */
77102 vibrate : function ( pattern : number | Array < number > = 400 , repeat : boolean = false ) {
78103 if ( Platform . OS === 'android' ) {
79104 if ( typeof pattern === 'number' ) {
@@ -98,6 +123,9 @@ var Vibration = {
98123 } ,
99124 /**
100125 * Stop vibration
126+ * ```
127+ * Vibration.cancel()
128+ * ```
101129 */
102130 cancel : function ( ) {
103131 if ( Platform . OS === 'ios' ) {
0 commit comments