You can set different vibration effects for your device, for example, you can set vibration effects with different intensities and durations for buttons on the device.
For details about the APIs, see @ohos.vibrator (Vibrator).
| Modules | Name | Description |
|---|---|---|
| ohos.vibrator | startVibration(effect: VibrateTime, attribute: VibrateAttribute): Promise<void> | Starts vibration with the specified effect and attribute. This API uses a promise to return the result. |
| ohos.vibrator | startVibration(effect: VibrateTime, attribute: VibrateAttribute, callback: AsyncCallback<void>): void | Starts vibration with the specified effect and attribute. This API uses an asynchronous callback to return the result. |
| ohos.vibrator | stopVibration(): Promise<void> | Stops vibration in all modes. This API uses a promise to return the result. |
| ohos.vibrator | stopVibration(callback: AsyncCallback<void>): void | Stops vibration in all modes. This API uses an asynchronous callback to return the result. |
Before using the vibrator on a device, request the ohos.permission.VIBRATE permission.
Configure the vibrator to vibrate with the specified duration and attribute.
import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';
try {
// Start vibration.
vibrator.startVibration({
type: 'time',
duration: 1000,
}, {
id: 0,
usage: 'alarm'
}, (error: BusinessError) => {
if (error) {
console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in starting vibration');
});
} catch (err) {
let e: BusinessError = err as BusinessError;
console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
} import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';
try {
// Stop vibration in all modes.
vibrator.stopVibration((error: BusinessError) => {
if (error) {
console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
return;
}
console.info('Succeed in stopping vibration');
})
} catch (error) {
let e: BusinessError = error as BusinessError;
console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}