harmony 鸿蒙@ohos.vibrator (振动)

  • 2022-08-09
  • 浏览 (788)

@ohos.vibrator (振动)

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

说明:

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

导入模块

import vibrator from '@ohos.vibrator';

vibrator.startVibration9+

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

根据指定的振动效果和振动属性触发马达振动。使用callback异步回调。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effect VibrateEffect 马达振动效果,支持三种:
1、VibrateTime:按照指定持续时间触发马达振动;
2、VibratePreset:按照预置振动效果触发马达振动;
3、VibrateFromFile:按照自定义振动配置文件触发马达振动。
attribute VibrateAttribute 马达振动属性。
callback AsyncCallback<void> 回调函数,当马达振动成功,err为undefined,否则为错误对象。

错误码

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

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

示例:

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

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  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 {
  vibrator.startVibration({
    type: 'preset',
    effectId: 'haptic.clock.timer',
    count: 1,
  }, {
    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 resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

const fileName: string = 'xxx.json';

let rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);

try {
  vibrator.startVibration({
    type: "file",
    hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
  }, {
    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}`);
}

getContext().resourceManager.closeRawFdSync(fileName);

vibrator.startVibration9+

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

根据指定的振动效果和振动属性触发马达振动。使用promise异步回调。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
effect VibrateEffect 马达振动效果,支持三种:
1、VibrateTime:按照指定持续时间触发马达振动;
2、VibratePreset:按照预置振动效果触发马达振动;
3、VibrateFromFile:按照自定义振动配置文件触发马达振动。
attribute VibrateAttribute 马达振动属性。

返回值:

类型 说明
Promise<void> 无返回结果的Promise对象。

错误码

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

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

示例:

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

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  vibrator.startVibration({
    type: 'time',
    duration: 1000
  }, {
    id: 0,
    usage: 'alarm'
  }).then(() => {
    console.info('Succeed in starting vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
  });
} 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 {
  vibrator.startVibration({
    type: 'preset',
    effectId: 'haptic.clock.timer',
    count: 1,
  }, {
    id: 0,
    usage: 'alarm'
  }).then(() => {
    console.info('Succeed in starting vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
  });
} 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 resourceManager from '@ohos.resourceManager';
import { BusinessError } from '@ohos.base';

const fileName: string = 'xxx.json';

let rawFd: resourceManager.RawFileDescriptor = getContext().resourceManager.getRawFdSync(fileName);

try {
  vibrator.startVibration({
    type: "file",
    hapticFd: { fd: rawFd.fd, offset: rawFd.offset, length: rawFd.length }
  }, {
    id: 0,
    usage: 'alarm'
  }).then(() => {
    console.info('Succeed in starting vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
  });
} catch (err) {
  let e: BusinessError = err as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

getContext().resourceManager.closeRawFdSync(fileName);

vibrator.stopVibration9+

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

按照指定模式停止马达振动。使用callback异步回调。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
stopMode VibratorStopMode 指定的停止振动模式,支持两种:
VIBRATOR_STOP_MODE_TIME:停止固定时长振动;
VIBRATOR_STOP_MODE_PRESET:停止预置振动。
此接口无法停止自定义振动,请使用vibrator.stopVibration10+
callback AsyncCallback<void> 回调函数,当马达停止振动成功,err为undefined,否则为错误对象。

示例:

停止固定时长振动:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  // 按照固定时长振动
  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}`);
}

try {
  // 按照VIBRATOR_STOP_MODE_TIME模式停止振动
  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME, (error: BusinessError) => {
    if (error) {
      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
      return;
    }
    console.info('Succeed in stopping 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 {
  // 按照预置效果振动
  vibrator.startVibration({
    type: 'preset',
    effectId: 'haptic.clock.timer',
    count: 1,
  }, {
    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}`);
}

try {
  // 按照VIBRATOR_STOP_MODE_PRESET模式停止振动
  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
    if (error) {
      console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
      return;
    }
    console.info('Succeed in stopping vibration');
  })
} catch (err) {
  let e: BusinessError = err as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

vibrator.stopVibration9+

stopVibration(stopMode: VibratorStopMode): Promise<void>

按照指定模式停止马达的振动。使用promise异步回调。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

参数名 类型 必填 说明
stopMode VibratorStopMode 指定的停止振动模式,支持两种:
VIBRATOR_STOP_MODE_TIME:停止固定时长振动;
VIBRATOR_STOP_MODE_PRESET:停止预置振动。
此接口无法停止自定义振动,请使用vibrator.stopVibration10+

返回值:

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

示例:

停止固定时长振动:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  // 按照固定时长振动
  vibrator.startVibration({
    type: 'time',
    duration: 1000,
  }, {
    id: 0,
    usage: 'alarm'
  }).then(() => {
    console.info('Succeed in starting vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
  });
} catch (err) {
  let e: BusinessError = err as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

try {
  // 按照VIBRATOR_STOP_MODE_TIME模式停止振动
  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_TIME).then(() => {
    console.info('Succeed in stopping vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
  });
} 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 {
  // 按照预置效果振动
  vibrator.startVibration({
    type: 'preset',
    effectId: 'haptic.clock.timer',
    count: 1,
  }, {
    id: 0,
    usage: 'alarm'
  }).then(() => {
    console.info('Succeed in starting vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
  });
} catch (err) {
  let e: BusinessError = err as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

try {
  // 按照VIBRATOR_STOP_MODE_PRESET模式停止振动
  vibrator.stopVibration(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
    console.info('Succeed in stopping vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
  });
} catch (err) {
  let e: BusinessError = err as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

vibrator.stopVibration10+

stopVibration(callback: AsyncCallback<void>): void

停止所有模式的马达振动。使用callback异步回调。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

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

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  // 停止所有模式的马达振动
  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}`);
}

vibrator.stopVibration10+

stopVibration(): Promise<void>

停止所有模式的马达振动。使用promise异步回调。

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

返回值:

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

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  // 停止所有模式的马达振动
  vibrator.stopVibration().then(() => {
    console.info('Succeed in stopping vibration');
  }, (error: BusinessError) => {
    console.error(`Failed to stop vibration. Code: ${error.code}, message: ${error.message}`);
  });
} catch (error) {
  let e: BusinessError = error as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

vibrator.isSupportEffect10+

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

查询是否支持传入参数effectId。使用callback异步回调。

系统能力:SystemCapability.Sensors.MiscDevice

参数:

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

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  // 查询是否支持'haptic.clock.timer'
  vibrator.isSupportEffect('haptic.clock.timer', (err: BusinessError, state: boolean) => {
    if (err) {
      console.error(`Failed to query effect. Code: ${err.code}, message: ${err.message}`);
      return;
    }
    console.info('Succeed in querying effect');
    if (state) {
      try {
        // 使用startVibration需要添加ohos.permission.VIBRATE权限
        vibrator.startVibration({
          type: 'preset',
          effectId: 'haptic.clock.timer',
          count: 1,
        }, {
          usage: 'unknown'
        }, (error: BusinessError) => {
          if (error) {
            console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
          } else {
            console.info('Succeed in starting vibration');
          }
        });
      } catch (error) {
        let e: BusinessError = error as BusinessError;
        console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
      }
    }
  })
} catch (error) {
  let e: BusinessError = error as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

vibrator.isSupportEffect10+

isSupportEffect(effectId: string): Promise<boolean>

查询是否支持传入参数effectId。使用promise异步回调。

系统能力:SystemCapability.Sensors.MiscDevice

参数:

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

返回值:

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

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

try {
  // 查询是否支持'haptic.clock.timer'
  vibrator.isSupportEffect('haptic.clock.timer').then((state: boolean) => {
    console.info(`The query result is ${state}`);
    if (state) {
      try {
        vibrator.startVibration({
          type: 'preset',
          effectId: 'haptic.clock.timer',
          count: 1,
        }, {
          usage: 'unknown'
        }).then(() => {
          console.info('Succeed in starting vibration');
        }).catch((error: BusinessError) => {
          console.error(`Failed to start vibration. Code: ${error.code}, message: ${error.message}`);
        });
      } catch (error) {
        let e: BusinessError = error as BusinessError;
        console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
      }
    }
  }, (error: BusinessError) => {
    console.error(`Failed to query effect. Code: ${error.code}, message: ${error.message}`);
  })
} catch (error) {
  let e: BusinessError = error as BusinessError;
  console.error(`An unexpected error occurred. Code: ${e.code}, message: ${e.message}`);
}

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 按照预置振动类型触发马达振动。
VibrateFromFile10+ 按照自定义振动配置文件触发马达振动。

VibrateTime9+

固定时长振动类型。

系统能力:SystemCapability.Sensors.MiscDevice

名称 类型 必填 说明
type string 值为”time”,按照指定持续时间触发马达振动。
duration number 马达持续振动时长, 单位ms。

VibratePreset9+

预置振动类型。

系统能力:SystemCapability.Sensors.MiscDevice

名称 类型 必填 说明
type string 值为”preset”,按照预置振动效果触发马达振动。
effectId string 预置的振动效果ID。
count number 重复振动的次数。

VibrateFromFile10+

自定义振动类型,仅部分设备支持,当设备不支持此振动类型时,返回设备不支持错误码

系统能力:SystemCapability.Sensors.MiscDevice

名称 类型 必填 说明
type string 值为”file”,按照振动配置文件触发马达振动。
hapticFd HapticFileDescriptor 振动配置文件的描述符。

HapticFileDescriptor10+

自定义振动配置文件的描述符,必须确认资源文件可用,其参数可通过文件管理API从沙箱路径获取或者通过资源管理API从HAP资源获取。使用场景:振动序列被存储在一个文件中,需要根据偏移量和长度进行振动,振动序列存储格式,请参考自定义振动格式

系统能力:SystemCapability.Sensors.MiscDevice

名称 类型 必填 说明
fd number 资源文件描述符。
offset number 距文件起始位置的偏移量,单位为字节,默认为文件起始位置,不可超出文件有效范围。
length number 资源长度,单位为字节,默认值为从偏移位置至文件结尾的长度,不可超出文件有效范围。

VibrateAttribute9+

马达振动属性。

系统能力:SystemCapability.Sensors.MiscDevice

名称 类型 必填 说明
id number 振动器id, 默认值为0。
usage 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对象。

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

vibrator.vibrate(1000).then(() => {
  console.info('Succeed in vibrating');
}, (error: BusinessError) => {
  console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
});

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,否则为错误对象。

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

vibrator.vibrate(1000, (error: BusinessError) => {
  if (error) {
    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
  } else {
    console.info('Succeed in vibrating');
  }
})

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对象。

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER).then(() => {
  console.info('Succeed in vibrating');
}, (error: BusinessError) => {
  console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
});

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,否则为错误对象。

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
  if (error) {
    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
  } else {
    console.info('Succeed in vibrating');
  }
})

vibrator.stop(deprecated)

stop(stopMode: VibratorStopMode): Promise<void>

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

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

需要权限:ohos.permission.VIBRATE

系统能力:SystemCapability.Sensors.MiscDevice

参数:

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

返回值:

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

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

// 按照effectId类型启动振动
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
  if (error) {
    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
  } else {
    console.info('Succeed in vibrating');
  }
})
// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET).then(() => {
  console.info('Succeed in stopping');
}, (error: BusinessError) => {
  console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
});

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,否则为错误对象。

示例:

import vibrator from '@ohos.vibrator';
import { BusinessError } from '@ohos.base';

// 按照effectId类型启动振动
vibrator.vibrate(vibrator.EffectId.EFFECT_CLOCK_TIMER, (error: BusinessError) => {
  if (error) {
    console.error(`Failed to vibrate. Code: ${error.code}, message: ${error.message}`);
  } else {
    console.info('Succeed in vibrating');
  }
})
// 使用VIBRATOR_STOP_MODE_PRESET模式停止振动
vibrator.stop(vibrator.VibratorStopMode.VIBRATOR_STOP_MODE_PRESET, (error: BusinessError) => {
  if (error) {
    console.error(`Failed to stop. Code: ${error.code}, message: ${error.message}`);
  } else {
    console.info('Succeed in stopping');
  }
})

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞