Skip to content

Latest commit

 

History

History
257 lines (227 loc) · 13.8 KB

README_zh.md

File metadata and controls

257 lines (227 loc) · 13.8 KB

Miscdevice

简介

小器件是指用于向外传递信号的设备,包括马达和LED灯,本组件对开发者提供控制马达振动和LED灯开关的能力,其架构图如下所示:

图 1 小器件架构图

Sensors_miscdevice组件目录

/base/sensors/miscdevice
├── frameworks                 # 框架代码
│   └── native                 # 客户端连接服务的Native方法
├── interfaces                 # 对外接口存放目录
│   ├── native                 # native 实现
│   └── plugin                 # Js API
├── sa_profile                 # 服务名称和服务的动态库的配置文件
├── services                   # 服务的代码目录
│   └── miscdevice_service     # 小器件服务,包含马达和Led灯,控制马达振动和灯的开关
└── utils                      # 公共代码,包括权限、通信等能力

约束

  • 要使用器件的功能,设备必须具有对应的器件。

  • 针对马达,开发者需要请求相应的权限才能使用。

    表 1 器件权限列表

    器件

    权限名

    敏感级别

    权限描述

    马达

    ohos.permission.VIBRATE

    system_grant

    允许应用程序使用马达

使用

本节以马达为例,说明其提供的接口功能以及使用流程。

接口说明

马达主要提供的功能有:触发振动,停止振动。JS API类开放能力如下:

表 2 马达JS API的主要接口

接口名

描述

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

按照指定的effect和attribute触发振动。effect为振动效果,attribute为振动属性,返回callback表示触发振动是否成功。

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

按照指定的effect和attribute触发振动。effect为振动效果,attribute为振动属性,返回Promise表示触发振动是否成功。

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

按照指定的要停止的振动模式来停止振动。stopMode为枚举马达两种振动类型,VIBRATOR_STOP_MODE_TIME、VIBRATOR_STOP_MODE_PRESET分别为停止duration模式的振动和停止预置EffectId模式的振动,返回callback表示停止振动是否成功。

stopVibration(stopMode: VibratorStopMode): Promise<void>

按照指定的要停止的振动模式来停止振动。stopMode为枚举马达两种振动类型,VIBRATOR_STOP_MODE_TIME、VIBRATOR_STOP_MODE_PRESET分别为停止duration模式的振动和停止预置EffectId模式的振动,返回Promise表示停止振动是否成功。

stopVibration(callback: AsyncCallback<void>)

停止所有模式的马达振动,返回callback表示停止振动是否成功。

stopVibration(): Promise<void>

停止所有模式的马达振动,返回Promise表示停止振动是否成功。

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

查询是否支持传入的参数effectId,返回callback表示effectId是否支持。

isSupportEffect(effectId: string): Promise<boolean>

查询是否支持传入的参数effectId,返回Promise表示effectId是否支持。

使用说明

  1. 导入vibrator包。
  2. 触发马达按照指定持续时间振动。
  3. 停止马达按照指定持续时间振动。
  4. 触发按照指定振动效果字符串振动。
  5. 停止按照指定振动效果字符串振动。
  6. 查询是否支持'haptic.clock.timer',如果支持则振动该effectId。
  7. 停止所有类型振动。

下述的代码示例中,提供了马达振动使用的完整流程。

//步骤1 导包
import vibrator from '@ohos.vibrator';
export default {
    onCreate() {
        console.info('MiscdeviceJsAPI AceApplication onCreate');
        //步骤2 触发马达按照指定持续的时间振动
        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);
        }
        //步骤3 停止马达按照指定持续的时间振动
        try {
            vibrator.stopVibration("time").then(() => {
                console.log("stopVibration success");
            }, (error)=>{
                console.log("stopVibration error:" + JSON.stringify(error));
            });
        } catch (error) {
            console.error('Exception in, error:' + JSON.stringify(error));
        }
        //步骤4 触发马达按照指定的字符串效果振动
        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));
        }
        //步骤5 停止马达按照指定的字符串效果振动
        try {
            vibrator.stopVibration("preset").then(() => {
                console.log("stopVibration success");
            }, (error)=>{
                console.log("stopVibration error:" + JSON.stringify(error));
            });
        } catch (error) {
            console.error('Exception in, error:' + JSON.stringify(error));
        }
        //步骤6 查询是否支持'haptic.clock.timer',如果支持则振动该effectId
        try {
            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({
                            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));
        }
        //步骤7 停止所有类型振动
        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);
        }
    }
    onDestroy() {
        console.info('AceApplication onDestroy');
    }
}

相关仓

泛Sensor子系统

sensors_sensor

sensors_miscdevice