@ohos.vibrator (振动)


@ohos.vibrator (振动)

vibrator模块提供控制马达振动启、停的能力。

说明:

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

导入模块

import vibrator from '@ohos.vibrator';
1

vibrator.startVibration9+

startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback: AsyncCallback<void>): void

根据指定振动效果和振动属性触发马达振动。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effect VibrateEffect 马达振动效果。
attribute VibrateAttribute 马达振动属性。
callback AsyncCallback<void> 回调函数,当马达振动成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见 ohos.vibrator错误码

错误码ID 错误信息
14600101 Device operation failed.

示例:

import vibrator from '@ohos.vibrator';
try {
    vibrator.startVibration({
        type: 'time',
        duration: 1000,
    }, {
        id: 0,
        usage: 'alarm'
    }, (error) => {
        if (error) {
            console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
            return;
        }
        console.log('Callback returned to indicate a successful vibration.');
    });
} catch (err) {
    console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

vibrator.startVibration9+

startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void>

根据指定振动效果和振动属性触发马达振动。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effect VibrateEffect 马达振动效果。
attribute VibrateAttribute 马达振动属性。

返回值:

类型 说明
Promise<void> Promise对象。

错误码

以下错误码的详细介绍请参见 ohos.vibrator错误码

错误码ID 错误信息
14600101 Device operation failed.

示例:

import vibrator from '@ohos.vibrator';
try {
  vibrator.startVibration({
      type: 'time',
      duration: 1000
  }, {
      id: 0,
      usage: 'alarm'
  }).then(() => {
      console.log('Promise returned to indicate a successful vibration');
  }, (error) => {
      console.error('error.code' + error.code + 'error.message' + error.message);
  });
} catch (err) {
  console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

vibrator.stopVibration9+

stopVibration(stopMode: VibratorStopMode, callback: AsyncCallback<void>): void

按照指定模式停止马达的振动。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
stopMode VibratorStopMode 指定的停止振动模式。
callback AsyncCallback<void> 回调函数。当马达停止振动成功,err为undefined,否则为错误对象。

示例:

import vibrator from '@ohos.vibrator';
try {
  // 按照固定时长振动
  vibrator.startVibration({
      type: 'time',
      duration: 1000,
  }, {
      id: 0,
      usage: 'alarm'
  }, (error) => {
      if (error) {
          console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
          return;
      }
      console.log('Callback returned to indicate a successful vibration.');
  });
} catch (err) {
  console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}

try {
  // 按照VIBRATOR_STOP_MODE_TIME模式停止振动
  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, function (error) {
      if (error) {
          console.log('error.code' + error.code + 'error.message' + error.message);
          return;
      }
      console.log('Callback returned to indicate successful.');
  })
} catch (err) {
  console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

vibrator.stopVibration9+

stopVibration(stopMode: VibratorStopMode): Promise<void>

按照指定模式停止马达的振动。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

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

返回值:

类型 说明
Promise<void> Promise对象。

示例:

import vibrator from '@ohos.vibrator';
try {
  // 按照固定时长振动
  vibrator.startVibration({
      type: 'time',
      duration: 1000
  }, {
      id: 0,
      usage: 'alarm'
  }).then(() => {
      console.log('Promise returned to indicate a successful vibration');
  }, (error) => {
      console.error('error.code' + error.code + 'error.message' + error.message);
  });
} catch (err) {
  console.error('errCode: ' + err.code + ' ,msg: ' + err.message);
}

try {
  // 按照VIBRATOR_STOP_MODE_TIME模式停止振动
  vibrator.stopVibration(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);
  });
} catch (err) {
  console.info('errCode: ' + err.code + ' ,msg: ' + err.message);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

vibrator.stopVibration10+

stopVibration(callback: AsyncCallback<void>): void

停止所有模式的马达振动。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
callback AsyncCallback<void> 回调函数。当马达停止振动成功,err为undefined,否则为错误对象。

示例:

import vibrator from '@ohos.vibrator';
try {
  // 按照固定时长振动
  vibrator.startVibration({
      type: 'time',
      duration: 1000,
  }, {
      id: 0,
      usage: 'alarm'
  }, (error) => {
      if (error) {
          console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message);
          return;
      }
      console.log('Callback returned to indicate a successful vibration.');
  });
} catch (error) {
  console.error('errCode: ' + error.code + ' ,msg: ' + error.message);
}

try {
  // 停止所有模式的马达振动
  vibrator.stopVibration(function (error) {
      if (error) {
          console.log('error.code' + error.code + 'error.message' + error.message);
          return;
      }
      console.log('Callback returned to indicate successful.');
  })
} catch (error) {
  console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

vibrator.stopVibration10+

stopVibration(): Promise<void>

停止所有模式的马达振动。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

返回值:

类型 说明
Promise<void> Promise对象。

示例:

import vibrator from '@ohos.vibrator';
try {
  // 按照固定时长振动
  vibrator.startVibration({
      type: 'time',
      duration: 1000
  }, {
      id: 0,
      usage: 'alarm'
  }).then(() => {
      console.log('Promise returned to indicate a successful vibration');
  }, (error) => {
      console.error('error.code' + error.code + 'error.message' + error.message);
  });
} catch (error) {
  console.error('errCode: ' + error.code + ' ,msg: ' + error.message);
}

try {
  // 停止所有模式的马达振动
  vibrator.stopVibration().then(() => {
      console.log('Promise returned to indicate a successful vibration.');
  }, (error) => {
      console.log('error.code' + error.code + 'error.message' + error.message);
  });
} catch (error) {
  console.info('errCode: ' + error.code + ' ,msg: ' + error.message);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

vibrator.isSupportEffect10+

isSupportEffect(effectId: string, callback: AsyncCallback<boolean>): void

查询是否支持传入的参数effectId。

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effectId string 振动效果id
callback AsyncCallback<boolean> 回调函数。当返回true则表示支持该effectId,否则不支持。

示例:

import vibrator from '@ohos.vibrator';
try {
  // 查询是否支持'haptic.clock.timer'
  vibrator.isSupportEffect('haptic.clock.timer', function (err, state) {
      if (err) {
          console.error('isSupportEffect failed, error:' + JSON.stringify(err));
          return;
      }
      console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
      if (state) {
          try {
              vibrator.startVibration({ // 使用startVibration需要添加ohos.permission.VIBRATE权限
                  type: 'preset',
                  effectId: 'haptic.clock.timer',
                  count: 1,
              }, {
                  usage: 'unknown'
              }, (error) => {
                  if(error) {
                      console.error('haptic.clock.timer vibrator error:'  + JSON.stringify(error));
                  } else {
                      console.log('haptic.clock.timer vibrator success');
                  }
              });
          } catch (error) {
              console.error('Exception in, error:' + JSON.stringify(error));
          }
      }
  })
} catch (error) {
  console.error('Exception in, error:' + JSON.stringify(error));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

vibrator.isSupportEffect10+

isSupportEffect(effectId: string): Promise<boolean>

查询是否支持传入的参数effectId。

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effectId string 振动效果id。

返回值:

类型 说明
Promise<boolean> Promise对象。当返回true则表示支持该effectId,否则不支持。

示例:

import vibrator from '@ohos.vibrator';
try {
  // 查询是否支持'haptic.clock.timer'
  vibrator.isSupportEffect('haptic.clock.timer').then((state) => {
      console.log('The effectId is ' + (state ? 'supported' : 'unsupported'));
      if (state) {
          try {
              vibrator.startVibration({
                  type: 'preset',
                  effectId: 'haptic.clock.timer',
                  count: 1,
              }, {
                  usage: 'unknown'
              }).then(()=>{
                  console.log('Promise returned to indicate a successful vibration');
              }).catch((error)=>{
                  console.error('Promise returned to indicate a failed vibration:' + JSON.stringify(error));
              });
          } catch (error) {
              console.error('exception in, error:' + JSON.stringify(error));
          }
      }
  }, (error) => {
      console.error('isSupportEffect failed, error:' + JSON.stringify(error));
  })
} catch (error) {
  console.error('Exception in, error:' + JSON.stringify(error));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

EffectId

预置的振动效果。

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

名称 说明
EFFECT_CLOCK_TIMER "haptic.clock.timer" 描述用户调整计时器时的振动效果。

VibratorStopMode

停止的振动模式。

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

名称 说明
VIBRATOR_STOP_MODE_TIME "time" 停止模式为duration模式的振动。
VIBRATOR_STOP_MODE_PRESET "preset" 停止模式为预置EffectId的振动。

VibrateEffect9+

马达振动效果。

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

类型 说明
VibrateTime 按照指定持续时间触发马达振动。
VibratePreset 按照预置振动类型触发马达振动。

VibrateTime9+

马达振动时长。

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

名称 说明
type "time" 按照指定持续时间触发马达振动。
duration - 马达持续振动时长, 单位ms。

VibratePreset9+

马达预置振动类型。

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

名称 说明
type "preset" 按照预置振动效果触发马达振动。
effectId - 预置的振动效果ID。
count - 重复振动的次数。

VibrateAttribute9+

马达振动属性。

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

名称 说明
id 0 振动器id。
usage - 马达振动的使用场景。

Usage9+

振动使用场景。

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

名称 类型 说明
unknown string 没有明确使用场景,最低优先级。
alarm string 用于警报场景。
ring string 用于铃声场景。
notification string 用于通知场景。
communication string 用于通信场景。
touch string 用于触摸场景。
media string 用于多媒体场景。
physicalFeedback string 用于物理反馈场景。
simulateReality string 用于模拟现实场景。

vibrator.vibrate(deprecated)

vibrate(duration: number): Promise<void>

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

从API version 9 开始不再维护,建议使用 vibrator.startVibration 代替。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
duration number 马达振动时长, 单位ms。

返回值:

类型 说明
Promise<void> 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);
});
1
2
3
4
5

vibrator.vibrate(deprecated)

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

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

从API version 9 开始不再维护,建议使用 vibrator.startVibration 代替。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
duration number 马达振动时长, 单位ms。
callback AsyncCallback<void> 回调函数。当马达振动成功,err为undefined,否则为错误对象。

示例:

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.');
  }
})
1
2
3
4
5
6
7

vibrator.vibrate(deprecated)

vibrate(effectId: EffectId): Promise<void>

按照预置振动效果触发马达振动。

从API version 9 开始不再维护,建议使用 vibrator.startVibration 代替。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effectId EffectId 预置的振动效果ID。

返回值:

类型 说明
Promise<void> 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);
});
1
2
3
4
5

vibrator.vibrate(deprecated)

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

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

从API version 9 开始不再维护,建议使用 vibrator.startVibration 代替。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effectId EffectId 预置的振动效果ID。
callback AsyncCallback<void> 回调函数。当马达振动成功,err为undefined,否则为错误对象。

示例:

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.');
  }
})
1
2
3
4
5
6
7

vibrator.stop(deprecated)

stop(stopMode: VibratorStopMode): Promise<void>

按照指定模式停止马达的振动。

从API version 9 开始不再维护,建议使用 vibrator.stopVibration 代替。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

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

返回值:

类型 说明
Promise<void> Promise对象。

示例:

// 按照effectId类型启动振动
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_MODE_PRESET模式停止振动
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);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

vibrator.stop(deprecated)

stop(stopMode: VibratorStopMode, callback?: AsyncCallback<void>): void

按照指定模式停止马达的振动。

从API version 9 开始不再维护,建议使用 vibrator.stopVibration 代替。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
stopMode VibratorStopMode 马达停止指定的振动模式。
callback AsyncCallback<void> 回调函数。当马达停止振动成功,err为undefined,否则为错误对象。

示例:

// 按照effectId类型启动振动
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_MODE_PRESET模式停止振动
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.');
  }
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16