鸿蒙开发设备管理:【@ohos.vibrator (振动)】

振动

说明:
开发前请熟悉鸿蒙开发指导文档gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

复制代码
import vibrator from '@ohos.vibrator';

vibrator.vibrate

vibrate(duration: number): Promise

按照指定持续时间触发马达振动。

权限列表:ohos.permission.VIBRATE,该权限为系统权限

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
duration number 指示马达振动的持续时间。

返回值:

类型 说明
Promise 指示触发振动是否成功。

示例:

复制代码
vibrator.vibrate(1000).then(()=>{
    console.log("Promise returned to indicate a successful vibration.");
}, (error)=>{
    console.log("error.code"+error.code+"error.message"+error.message);
});

vibrator.vibrate

vibrate(duration: number, callback?: AsyncCallback): void

按照指定持续时间触发马达振动。

权限列表:ohos.permission.VIBRATE,该权限为系统权限

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
duration number 指示马达振动的持续时间。
callback AsyncCallback 马达执行振动的回调函数,指示触发振动是否成功。

示例:

复制代码
vibrator.vibrate(1000,function(error){
    if(error){
        console.log("error.code"+error.code+"error.message"+error.message);
    }else{
        console.log("Callback returned to indicate a successful vibration.");
    }
})

vibrator.vibrate

vibrate(effectId: EffectId): Promise

按照指定振动效果触发马达振动。

权限列表:ohos.permission.VIBRATE,该权限为系统权限

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effectId [EffectId] 指示马达振动效果的字符串。

返回值:

类型 说明
Promise 指示触发振动是否成功。

示例:

复制代码
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(()=>{
    console.log("Promise returned to indicate a successful vibration.");
}, (error)=>{
    console.log("error.code"+error.code+"error.message"+error.message);
});

vibrator.vibrate

vibrate(effectId: EffectId, callback?: AsyncCallback): void

按照指定振动效果触发马达振动。

权限列表:ohos.permission.VIBRATE,该权限为系统权限

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effectId [EffectId] 指示马达振动效果的字符串。
callback AsyncCallback 马达执行振动的回调函数,指示触发振动是否成功。

示例:

复制代码
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, function(error){
    if(error){
        console.log("error.code"+error.code+"error.message"+error.message);
    }else{
        console.log("Callback returned to indicate a successful vibration.");
    }
})

vibrator.stop

stop(stopMode: VibratorStopMode): Promise

按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败。

权限列表:ohos.permission.VIBRATE,该权限为系统权限

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
stopMode [VibratorStopMode] 指示马达要停止指定的振动模式。

返回值:

类型 说明
Promise 指示停止振动是否成功。

示例:

复制代码
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(()=>{
    console.log("Promise returned to indicate a successful vibration.");
}, (error)=>{
    console.log("error.code"+error.code+"error.message"+error.message);
});

vibrator.stop

stop(stopMode: VibratorStopMode, callback?: AsyncCallback): void;

按照要停止指定的振动模式来停止马达的振动。如果要停止的振动模式与触发马达振动时的模式不相同,则调用本接口会失败。

权限列表:ohos.permission.VIBRATE,该权限为系统权限

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
stopMode [VibratorStopMode] 指示马达要停止指定的振动模式。
callback AsyncCallback 马达停止振动的回调函数,指示停止振动是否成功。

示例:

复制代码
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, function(error){
    if(error){
        console.log("error.code"+error.code+"error.message"+error.message);
    }else{
        console.log("Callback returned to indicate successful.");
    }
})

EffectId

表示马达振动效果的字符串。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice

名称 默认值 说明 HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿
EFFECT_CLOCK_TIMER "haptic.clock.timer" 调整定时器时振动器的振动效果。

VibratorStopMode

表示马达要停止指定的振动模式。

系统能力:以下各项对应的系统能力均为SystemCapability.Sensors.MiscDevice

名称 默认值 说明
VIBRATOR_STOP_MODE_TIME "time" 停止模式为duration模式的振动。即触发振动时参数类型为number,参数本身为指示振动持续时间的触发方式。
VIBRATOR_STOP_MODE_PRESET "preset" 停止模式为预置EffectId的振动。即触发振动时参数类型为EffectId,参数本身为指示马达振动效果的字符串的触发方式。
相关推荐
特立独行的猫a3 小时前
鸿蒙 PC 命令行工具迁移实战 · 内部课件(详细配套版)
华为·harmonyos·移植·鸿蒙pc
廖松洋(Alina)3 小时前
04极速划词页面实现-鸿蒙PC端Electron开发
华为·electron·开源·harmonyos·鸿蒙
轻口味3 小时前
HarmonyOS 6.1 全栈实战录 - 07 极速连接:Remote Communication Kit (RCP) 实战与认证挑战深度解析
华为·harmonyos
程序员鱼皮4 小时前
再见百度,我用 1 小时,开发了个 AI 搜索引擎!Codex + GPT 5.5 + DeepSeek V4 真香~
计算机·ai·程序员·编程·ai编程
Nightwatchman4 小时前
用 AI 写了两年代码,我终于看清它为什么会"骗人"
程序员·ai编程
想你依然心痛4 小时前
HarmonyOS 6(API 23)实战:基于悬浮导航、沉浸光感与Face AR & Body AR的“灵犀康养“——PC端沉浸式AR智能康复训练系统
华为·ar·harmonyos·悬浮导航·沉浸光感
廖松洋(Alina)4 小时前
03主入口页面与导航结构-鸿蒙PC端Electron开发
前端·javascript·华为·electron·开源·harmonyos·鸿蒙
廖松洋(Alina)4 小时前
09词根分解与水印展示-鸿蒙PC端Electron开发
前端·javascript·华为·electron·开源·harmonyos·鸿蒙
音视频牛哥4 小时前
大牛直播SDK(SmartMediaKit)鸿蒙NEXT同屏RTMP推流与轻量级RTSP服务集成实践指南
华为·harmonyos·大牛直播sdk·鸿蒙next无纸化同屏·鸿蒙next rtmp推流·鸿蒙next rtsp服务器·鸿蒙next无纸化会议
xmdy58664 小时前
Flutter + 开源鸿蒙实战|城市智慧停车管理系统 Day4 停车订单生成+多状态管理+在线缴费+我的订单+会员中心+个人中心完善
flutter·开源·harmonyos