1e41f4b71Sopenharmony_ci# @system.vibrator (振动) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_civibrator模块提供控制马达振动的能力,主要包含灯的列表查询、打开灯、关闭灯等接口,振动器的列表查询、振动器的振动器效果查询、触发/关闭振动器等接口。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci控制类小器件指的是设备上的LED灯和振动器。其中,LED灯主要用作指示(如充电状态)、闪烁功能(如三色灯)等;振动器主要用于闹钟、开关机振动、来电振动等场景。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci> **说明:** 9e41f4b71Sopenharmony_ci> - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 10e41f4b71Sopenharmony_ci> - 从API Version 8开始,该接口不再维护,推荐使用新接口[`@ohos.vibrator`](js-apis-vibrator.md)。 11e41f4b71Sopenharmony_ci> - 该功能使用需要对应硬件支持,仅支持真机调试。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci## 导入模块 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci```ts 18e41f4b71Sopenharmony_ciimport { Vibrator } from '@kit.SensorServiceKit'; 19e41f4b71Sopenharmony_ci``` 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci## Vibrator.vibrate 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci vibrate(options?: VibrateOptions): void 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci触发设备振动。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.VIBRATE 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Sensors.MiscDevice.Lite 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci**参数:** 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 34e41f4b71Sopenharmony_ci| ------- | --------------------------------- | ---- | ---------- | 35e41f4b71Sopenharmony_ci| options | [VibrateOptions](#vibrateoptions) | 否 | 振动模式。 | 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci**示例:** 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci```ts 40e41f4b71Sopenharmony_ciimport { Vibrator, VibrateOptions } from '@kit.SensorServiceKit'; 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_cilet vibrateOptions: VibrateOptions = { 43e41f4b71Sopenharmony_ci mode: 'short', 44e41f4b71Sopenharmony_ci success: () => { 45e41f4b71Sopenharmony_ci console.info('Succeed in vibrating'); 46e41f4b71Sopenharmony_ci }, 47e41f4b71Sopenharmony_ci fail: (data: string, code: number) => { 48e41f4b71Sopenharmony_ci console.info(`Failed to vibrate. Data: ${data}, code: ${code}`); 49e41f4b71Sopenharmony_ci }, 50e41f4b71Sopenharmony_ci complete: () => { 51e41f4b71Sopenharmony_ci console.info('completed in vibrating'); 52e41f4b71Sopenharmony_ci } 53e41f4b71Sopenharmony_ci}; 54e41f4b71Sopenharmony_ciVibrator.vibrate(vibrateOptions); 55e41f4b71Sopenharmony_ci``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci## VibrateOptions 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci振动模式。 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci**需要权限:** ohos.permission.VIBRATE 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.Sensors.MiscDevice.Lite 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 66e41f4b71Sopenharmony_ci| -------- | -------- | ---- | ------------------------------------------------------------ | 67e41f4b71Sopenharmony_ci| mode | string | 否 | 振动的模式,其中long表示长振动,short表示短振动,默认值为long。 | 68e41f4b71Sopenharmony_ci| success | Function | 否 | 感应到振动数据变化后的回调函数。 | 69e41f4b71Sopenharmony_ci| fail | Function | 否 | 接口调用失败的回调函数。 | 70e41f4b71Sopenharmony_ci| complete | Function | 否 | 接口调用结束的回调函数。 |