# Vibrator Development ## When to Use You can set different vibration effects as needed, for example, customizing vibration effects with different intensities and durations for buttons on the device, and customizing one-shot or periodic vibration effects with different intensities and durations for alarm clocks and incoming calls. ## Available APIs | Module | API | Description | | -------- | -------- | -------- | | ohos.vibrator | vibrate(duration: number): Promise<void> | Triggers vibration with the specified duration. This API uses a promise to return the result. | | ohos.vibrator | vibrate(duration: number, callback?: AsyncCallback<void>): void | Triggers vibration with the specified duration. This API uses a callback to return the result. | | ohos.vibrator | vibrate(effectId: EffectId): Promise<void> | Triggers vibration with the specified effect. This API uses a promise to return the result. | | ohos.vibrator | vibrate(effectId: EffectId, callback?: AsyncCallback<void>): void | Triggers vibration with the specified effect. This API uses a callback to return the result. | | ohos.vibrator | stop(stopMode: VibratorStopMode): Promise<void> | Stops vibration. This API uses a promise to return the result. | | ohos.vibrator | stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void | Stops vibration. This API uses a callback to return the result. | ## How to Develop 1. Declare the permissions required for controlling vibrators on the hardware device in the **config.json** file. ``` "reqPermissions":[ { "name":"ohos.permission.ACCELEROMETER", "reason"":"", "usedScene":{ "ability""[ ".MainAbility" ], "when":"inuse" } }, { "name":"ohos.permission.VIBRATE", "reason"":"", "usedScene":{ "ability""[ ".MainAbility" ], "when":"inuse" } }, { "name":"ohos.permission.ACTIVITY_MOTION", "reason"":"", "usedScene":{ "ability""[ ".MainAbility" ], "when":"inuse" } }, ] ``` 2. Trigger the device to vibrate. ``` import vibrator from "@ohos.vibrator" vibrator.vibrate(duration: number).then((error)=>{ if(error){// The call fails, and error.code and error.message are printed. console.log("Promise return failed.error.code"+error.code+"error.message"+error.message); }else{// The call succeeded. The device starts to vibrate. console.log("Promise returned to indicate a successful vibration.") }; }) ``` 3. Stop the vibration. ``` import vibrator from "@ohos.vibrator" vibrator.stop(stopMode: VibratorStopMode).then((error)=>{ if(error){// The call fails, and error.code and error.message are printed. console.log("Promise return failed. error.code"+error.code+"error.message"+error.message); }else{// The call succeeded. The device stops vibration. Console.log("Promise returned to indicate a successful stop."); }; }) ```