# sensors_miscdevice **Repository Path**: linuxkernelAnalyse/sensors_miscdevice ## Basic Information - **Project Name**: sensors_miscdevice - **Description**: 控制马达振动和LED灯开关 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 149 - **Created**: 2022-06-21 - **Last Updated**: 2022-06-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Miscdevice - [简介](#section11660541593) - [Sensors\_miscdevice组件目录](#section44981327519) - [约束](#section98068674513) - [使用](#section1581412211528) - [接口说明](#section15684191115524) - [使用说明](#section79302049192310) - [相关仓](#section96071132185310) ## 简介 小器件是指用于向外传递信号的设备,包括马达和LED灯,本组件对开发者提供控制马达振动和LED灯开关的能力,其架构图如下所示: **图 1** 小器件架构图 ![](figures/zh-cn_image_0000001152988366.png) ## 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的主要接口

接口名

描述

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

按照指定持续时间触发震动。duration为振动持续时长,callback为触发振动是否成功。

vibrate(duration: number): Promise<void>

按照指定持续时间触发震动,返回Promise表示触发振动是否成功。

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

按照指定振动效果字符串触发震动。effectId表示预置的振动效果串,callback为触发振动是否成功。

vibrate(effectId: EffectId): Promise<void>

按照指定振动效果字符串触发震动。effectId表示预置的振动效果串,返回Promise表示触发振动是否成功。

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

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

stop(stopMode: VibratorStopMode): Promise<void>

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

### 使用说明 1. 导入vibrator包。 2. 触发马达按照指定持续时间振动。 3. 停止马达按照指定持续时间振动。 4. 触发按照指定振动效果字符串振动。 5. 停止按照指定振动效果字符串振动。 下述的代码示例中,提供了马达震动使用的完整流程。 ``` //步骤1 导包 import vibrator from '@ohos.vibrator'; export default { onCreate() { console.info('MiscdeviceJsAPI AceApplication onCreate'); //步骤2 触发马达按照指定持续的时间振动 vibrator.vibrate(100, function(error) { if (error) { console.error("Failed to trigger vibration. Error code: " + error.code); return; } console.info("Succeeded in triggering vibration."); }); //步骤3 停止马达按照指定持续的时间振动 vibrator.stop("time", function(error) { if (error) { console.error("Failed to stop vibration. Error code: " + error.code); return; } console.info("Succeeded in stopping vibration."); }); //步骤4 触发马达按照指定的字符串效果振动 vibrator.vibrate("haptic.clock.timer", function(error) { if (error) { console.error("Failed to trigger vibration. Error code: " + error.code); return; } console.info("Succeeded in triggering vibration."); }); //步骤5 停止马达按照指定的字符串效果振动 vibrator.stop("preset", function(error) { if (error) { console.error("Failed to stop vibration. Error code: " + error.code); return; } console.info("Succeeded in stopping vibration."); }); } onDestroy() { console.info('AceApplication onDestroy'); } } ``` ## 相关仓 泛Sensor子系统 [sensors\_sensor](https://gitee.com/openharmony/sensors_sensor) **sensors\_miscdevice**