Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | - | - | ||||
figures/ | 12-May-2024 | - | ||||
frameworks/ | 12-May-2024 | - | 3,100 | 2,603 | ||
interfaces/ | 12-May-2024 | - | 787 | 256 | ||
sa_profile/ | 12-May-2024 | - | 36 | 32 | ||
services/miscdevice_service/ | 12-May-2024 | - | 4,073 | 3,337 | ||
test/ | 12-May-2024 | - | 17,337 | 14,852 | ||
utils/ | 12-May-2024 | - | 3,028 | 2,271 | ||
CODEOWNERS | D | 12-May-2024 | 803 | 17 | 16 | |
LICENSE | D | 12-May-2024 | 10.1 KiB | 177 | 150 | |
OAT.xml | D | 12-May-2024 | 3.3 KiB | 55 | 36 | |
README.md | D | 12-May-2024 | 15.8 KiB | 257 | 227 | |
README_zh.md | D | 12-May-2024 | 13.8 KiB | 258 | 227 | |
bundle.json | D | 12-May-2024 | 2.9 KiB | 93 | 93 | |
hisysevent.yaml | D | 12-May-2024 | 1.6 KiB | 39 | 21 | |
miscdevice.gni | D | 12-May-2024 | 1.4 KiB | 47 | 37 |
README.md
1# Misc Device<a name="EN-US_TOPIC_0000001148523206"></a> 2 3- [Introduction](#section11660541593) 4- [Directory Structure](#section44981327519) 5- [Constraints](#section98068674513) 6- [Usage](#section1581412211528) 7 - [Available APIs](#section15684191115524) 8 - [How to Use](#section79302049192310) 9 10- [Repositories Involved](#section96071132185310) 11 12## Introduction<a name="section11660541593"></a> 13 14Misc devices, including vibrators and LED indicators, are used to send signals externally. You can call APIs to control the vibration of vibrators and lighting-on and lighting-off of LED indicators. 15 16**Figure 1** Misc device architecture<a name="fig4460722185514"></a> 17 18 19 20 21## Directory Structure<a name="section44981327519"></a> 22 23``` 24/base/sensors/miscdevice 25├── frameworks # Framework code 26│ └── native # Native methods for the client to connect to services 27├── interfaces # External APIs 28│ ├── native # Native implementation 29│ └── plugin # JS APIs 30├── sa_profile # Configuration file of system ability names and dynamic libraries 31├── services # Code of services 32│ └── miscdevice_service # Misc device service, which is used to control the vibration of vibrators and lighting-on and lighting-off of LED lights 33└── utils # Common code, including permissions and communication capabilities 34``` 35 36## Constraints<a name="section98068674513"></a> 37 38- The APIs are valid only when your hardware is equipped with the required misc devices. 39- To use vibrators, you need to request the required permissions. 40 41 **Table 1** Permissions required by misc devices 42 43 <a name="table1379101653916"></a> 44 <table><thead align="left"><tr id="row137911161397"><th class="cellrowborder" valign="top" width="15.728427157284273%" id="mcps1.2.5.1.1"><p id="p15138132134118"><a name="p15138132134118"></a><a name="p15138132134118"></a>Misc Device</p> 45 </th> 46 <th class="cellrowborder" valign="top" width="28.537146285371463%" id="mcps1.2.5.1.2"><p id="p2798169398"><a name="p2798169398"></a><a name="p2798169398"></a>Permission Name</p> 47 </th> 48 <th class="cellrowborder" valign="top" width="18.168183181681833%" id="mcps1.2.5.1.3"><p id="p14791716163914"><a name="p14791716163914"></a><a name="p14791716163914"></a>Sensitivity</p> 49 </th> 50 <th class="cellrowborder" valign="top" width="37.56624337566243%" id="mcps1.2.5.1.4"><p id="p379171633919"><a name="p379171633919"></a><a name="p379171633919"></a>Permission Description</p> 51 </th> 52 </tr> 53 </thead> 54 <tbody><tr id="row12791216173912"><td class="cellrowborder" valign="top" width="15.728427157284273%" headers="mcps1.2.5.1.1 "><p id="p18658132191310"><a name="p18658132191310"></a><a name="p18658132191310"></a>Vibrator</p> 55 </td> 56 <td class="cellrowborder" valign="top" width="28.537146285371463%" headers="mcps1.2.5.1.2 "><p id="p8826341131218"><a name="p8826341131218"></a><a name="p8826341131218"></a>ohos.permission.VIBRATE</p> 57 </td> 58 <td class="cellrowborder" valign="top" width="18.168183181681833%" headers="mcps1.2.5.1.3 "><p id="p207991683914"><a name="p207991683914"></a><a name="p207991683914"></a>system_grant</p> 59 </td> 60 <td class="cellrowborder" valign="top" width="37.56624337566243%" headers="mcps1.2.5.1.4 "><p id="p118061614399"><a name="p118061614399"></a><a name="p118061614399"></a>Allows an application to use the vibrator.</p> 61 </td> 62 </tr> 63 </tbody> 64 </table> 65 66 67## Usage<a name="section1581412211528"></a> 68 69This section describes the features and usage of the vibrator APIs. 70 71### Available APIs<a name="section15684191115524"></a> 72 73The APIs provided for the vibrator are used to trigger and stop vibration. The following table describes these APIs. 74 75**Table 2** JS APIs for the vibrator 76 77<a name="table1738121244713"></a> 78 79<table><thead align="left"><tr id="row4381111254710"><th class="cellrowborder" valign="top" width="31.209999999999997%" id="mcps1.2.3.1.1"><p id="p1738116127470"><a name="p1738116127470"></a><a name="p1738116127470"></a>API</p> 80</th> 81<th class="cellrowborder" valign="top" width="68.78999999999999%" id="mcps1.2.3.1.2"><p id="p10381161224717"><a name="p10381161224717"></a><a name="p10381161224717"></a>Description</p> 82</th> 83</tr> 84</thead> 85<tbody><tr id="row18381121274715"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p15297217910"><a name="p15297217910"></a><a name="p15297217910"></a>startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback?: AsyncCallback<void>)</p> 86</td> 87<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p143812128476"><a name="p143812128476"></a><a name="p143812128476"></a>Triggers vibration based on the vibration effect specified by <strong id="b137434619532"><a name="b137434619532"></a><a name="b137434619532"></a>effect</strong> and <strong id="b137434619532"><a name="b137434619532"></a><a name="b137434619532"></a>attribute</strong>. This API uses a callback to indicate whether the vibration is triggered successfully.</p> 88</td> 89</tr> 90<tr id="row29912332312"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p599153172316"><a name="p599153172316"></a><a name="p599153172316"></a>startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void></p> 91</td> 92<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1099114392316"><a name="p1099114392316"></a><a name="p1099114392316"></a>Triggers vibration based on the vibration effect specified by <strong id="b137434619532"><a name="b137434619532"></a><a name="b137434619532"></a>effect</strong> and <strong id="b137434619532"><a name="b137434619532"></a><a name="b137434619532"></a>attribute</strong>. This API uses a promise to indicate whether the vibration is triggered successfully.</p> 93</td> 94</tr> 95<tr id="row10382181218477"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p764313511343"><a name="p764313511343"></a><a name="p764313511343"></a>stopVibration(stopMode: VibratorStopMode, callback?: AsyncCallback<void>)</p> 96</td> 97<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1738291234712"><a name="p1738291234712"></a><a name="p1738291234712"></a>Stops vibration based on the mode specified by <strong id="b1019916113564"><a name="b1019916113564"></a><a name="b1019916113564"></a>stopMode</strong>. There are two modes: <strong id="b1784893665611"><a name="b1784893665611"></a><a name="b1784893665611"></a>VIBRATOR_STOP_MODE_TIME</strong> and <strong id="b157644016560"><a name="b157644016560"></a><a name="b157644016560"></a>VIBRATOR_STOP_MODE_PRESET</strong>, which are used to stop vibration triggered by <strong id="b1217141320585"><a name="b1217141320585"></a><a name="b1217141320585"></a>duration</strong> and <strong id="b1639019514578"><a name="b1639019514578"></a><a name="b1639019514578"></a>effectId</strong>, respectively. This API uses a callback to indicate whether the vibration is stopped successfully.</p> 98</td> 99</tr> 100<tr id="row2087541618235"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p13875201620231"><a name="p13875201620231"></a><a name="p13875201620231"></a>stopVibration(stopMode: VibratorStopMode): Promise<void></p> 101</td> 102<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p14875916142317"><a name="p14875916142317"></a><a name="p14875916142317"></a>Stops vibration based on the mode specified by <strong id="b128871517589"><a name="b128871517589"></a><a name="b128871517589"></a>stopMode</strong>. There are two modes: <strong id="b16566172217582"><a name="b16566172217582"></a><a name="b16566172217582"></a>VIBRATOR_STOP_MODE_TIME</strong> and <strong id="b1757216228581"><a name="b1757216228581"></a><a name="b1757216228581"></a>VIBRATOR_STOP_MODE_PRESET</strong>, which are used to stop vibration triggered by <strong id="b257214227586"><a name="b257214227586"></a><a name="b257214227586"></a>duration</strong> and <strong id="b457362275812"><a name="b457362275812"></a><a name="b457362275812"></a>effectId</strong>, respectively. This API uses a promise to indicate whether the vibration is stopped successfully.</p> 103</td> 104</tr> 105<tr id="row10382181218477"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p764313511343"><a name="p764313511343"></a><a name="p764313511343"></a>stopVibration(callback: AsyncCallback<void>)</p> 106</td> 107<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1738291234712"><a name="p1738291234712"></a><a name="p1738291234712"></a>Stop all types of vibration. This API uses a callback to indicate whether the vibration is stopped successfully.</p> 108</td> 109</tr> 110<tr id="row2087541618235"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p13875201620231"><a name="p13875201620231"></a><a name="p13875201620231"></a>stopVibration(): Promise<void></p> 111</td> 112<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p14875916142317"><a name="p14875916142317"></a><a name="p14875916142317"></a>Stop all types of vibration. This API uses a promise to indicate whether the vibration is stopped successfully.</p> 113</td> 114</tr> 115<tr id="row10382181218477"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p764313511343"><a name="p764313511343"></a><a name="p764313511343"></a>isSupportEffect(effectId: string, callback: AsyncCallback<boolean>)</p> 116</td> 117<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1738291234712"><a name="p1738291234712"></a><a name="p1738291234712"></a>Query whether the <strong id="b128871517589"><a name="b128871517589"></a><a name="b128871517589"></a>effectId</strong> is supported. This API uses a callback to indicate whether the query result of EffectId is successful.</p> 118</td> 119</tr> 120<tr id="row2087541618235"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p13875201620231"><a name="p13875201620231"></a><a name="p13875201620231"></a>isSupportEffect(effectId: string): Promise<boolean></p> 121</td> 122<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p14875916142317"><a name="p14875916142317"></a><a name="p14875916142317"></a>Query whether the <strong id="b128871517589"><a name="b128871517589"></a><a name="b128871517589"></a>effectId</strong> is supported. This API uses a promise to indicate whether the query result of EffectId is successful.</p> 123</td> 124</tr> 125</tbody> 126</table> 127 128 129### How to Use<a name="section79302049192310"></a> 130 1311. Import the **vibrator** package. 1322. Trigger vibration with a specific duration. 1333. Stop vibration that is triggered with a specific duration. 1344. Trigger vibration with a specific effect. 1355. Stop vibration that is triggered with a specific effect. 1366. Query whether 'haptic. lock. timer' is supported. If so, vibrate the effectId. 1377. Stop all types of vibration. 138 139The following sample code provides a complete process of using the vibrator APIs: 140 141``` 142//Step 1 Import the vibrator package. 143import vibrator from '@ohos.vibrator'; 144export default { 145 onCreate() { 146 console.info('MiscdeviceJsAPI AceApplication onCreate'); 147 // Step 2 Trigger vibration with a specific duration. 148 try { 149 vibrator.startVibration({ 150 type: 'time', 151 duration: 1000, 152 }, { 153 id: 0, 154 usage: 'alarm' 155 }, (error) => { 156 if (error) { 157 console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message); 158 return; 159 } 160 console.log('Callback returned to indicate a successful vibration.'); 161 }); 162 } catch (error) { 163 console.error('errCode: ' + error.code + ' ,msg: ' + error.message); 164 } 165 // Step 3 Stop vibration that is triggered with a specific duration. 166 try { 167 vibrator.stopVibration("time").then(() => { 168 console.log("stopVibration success"); 169 }, (error)=>{ 170 console.log("stopVibration error:" + JSON.stringify(error)); 171 }); 172 } catch (error) { 173 console.error('Exception in, error:' + JSON.stringify(error)); 174 } 175 // Step 4 Trigger vibration based on with a specific effect. 176 try { 177 vibrator.startVibration({ 178 type: 'preset', 179 effectId: 'haptic.clock.timer', 180 count: 1, 181 }, { 182 usage: 'unknown' 183 }).then(()=>{ 184 console.log('Promise returned to indicate a successful vibration'); 185 }).catch((error)=>{ 186 console.error('Promise returned to indicate a failed vibration:' + JSON.stringify(error)); 187 }); 188 } catch (error) { 189 console.error('exception in, error:' + JSON.stringify(error)); 190 } 191 // Step 5 Stop vibration that is triggered with a specific effect. 192 try { 193 vibrator.stopVibration("preset").then(() => { 194 console.log("stopVibration success"); 195 }, (error)=>{ 196 console.log("stopVibration error:" + JSON.stringify(error)); 197 }); 198 } catch (error) { 199 console.error('Exception in, error:' + JSON.stringify(error)); 200 } 201 // Step 6 Query whether 'haptic. lock. timer' is supported. If so, vibrate the effectId. 202 try { 203 vibrator.isSupportEffect('haptic.clock.timer', function (err, state) { 204 if (err) { 205 console.error('isSupportEffect failed, error:' + JSON.stringify(err)); 206 return; 207 } 208 console.log('The effectId is ' + (state ? 'supported' : 'unsupported')); 209 if (state) { 210 try { 211 vibrator.startVibration({ 212 type: 'preset', 213 effectId: 'haptic.clock.timer', 214 count: 1, 215 }, { 216 usage: 'unknown' 217 }, (error) => { 218 if(error) { 219 console.error('haptic.clock.timer vibrator error:' + JSON.stringify(error)); 220 } else { 221 console.log('haptic.clock.timer vibrator success'); 222 } 223 }); 224 } catch (error) { 225 console.error('Exception in, error:' + JSON.stringify(error)); 226 } 227 } 228 }) 229 } catch (error) { 230 console.error('Exception in, error:' + JSON.stringify(error)); 231 } 232 // Step 7 Stop all types of vibration. 233 try { 234 vibrator.stopVibration(function (error) { 235 if (error) { 236 console.log('error.code' + error.code + 'error.message' + error.message); 237 return; 238 } 239 console.log('Callback returned to indicate successful.'); 240 }) 241 } catch (error) { 242 console.info('errCode: ' + error.code + ' ,msg: ' + error.message); 243 } 244 } 245 onDestroy() { 246 console.info('AceApplication onDestroy'); 247 } 248} 249``` 250 251## Repositories Involved<a name="section96071132185310"></a> 252 253Pan-sensor subsystem 254 255[sensors\_sensor](https://gitee.com/openharmony/sensors_sensor) 256 257**sensors\_miscdevice**
README_zh.md
1# Miscdevice<a name="ZH-CN_TOPIC_0000001148523206"></a> 2 3- [简介](#section11660541593) 4- [Sensors\_miscdevice组件目录](#section44981327519) 5- [约束](#section98068674513) 6- [使用](#section1581412211528) 7 - [接口说明](#section15684191115524) 8 - [使用说明](#section79302049192310) 9 10- [相关仓](#section96071132185310) 11 12## 简介<a name="section11660541593"></a> 13 14小器件是指用于向外传递信号的设备,包括马达和LED灯,本组件对开发者提供控制马达振动和LED灯开关的能力,其架构图如下所示: 15 16**图 1** 小器件架构图<a name="fig4460722185514"></a> 17 18 19 20 21## Sensors\_miscdevice组件目录<a name="section44981327519"></a> 22 23``` 24/base/sensors/miscdevice 25├── frameworks # 框架代码 26│ └── native # 客户端连接服务的Native方法 27├── interfaces # 对外接口存放目录 28│ ├── native # native 实现 29│ └── plugin # Js API 30├── sa_profile # 服务名称和服务的动态库的配置文件 31├── services # 服务的代码目录 32│ └── miscdevice_service # 小器件服务,包含马达和Led灯,控制马达振动和灯的开关 33└── utils # 公共代码,包括权限、通信等能力 34``` 35 36## 约束<a name="section98068674513"></a> 37 38- 要使用器件的功能,设备必须具有对应的器件。 39- 针对马达,开发者需要请求相应的权限才能使用。 40 41 **表 1** 器件权限列表 42 43 <a name="table1379101653916"></a> 44 <table><thead align="left"><tr id="row137911161397"><th class="cellrowborder" valign="top" width="15.728427157284273%" id="mcps1.2.5.1.1"><p id="p15138132134118"><a name="p15138132134118"></a><a name="p15138132134118"></a>器件</p> 45 </th> 46 <th class="cellrowborder" valign="top" width="28.537146285371463%" id="mcps1.2.5.1.2"><p id="p2798169398"><a name="p2798169398"></a><a name="p2798169398"></a>权限名</p> 47 </th> 48 <th class="cellrowborder" valign="top" width="18.168183181681833%" id="mcps1.2.5.1.3"><p id="p14791716163914"><a name="p14791716163914"></a><a name="p14791716163914"></a>敏感级别</p> 49 </th> 50 <th class="cellrowborder" valign="top" width="37.56624337566243%" id="mcps1.2.5.1.4"><p id="p379171633919"><a name="p379171633919"></a><a name="p379171633919"></a>权限描述</p> 51 </th> 52 </tr> 53 </thead> 54 <tbody><tr id="row12791216173912"><td class="cellrowborder" valign="top" width="15.728427157284273%" headers="mcps1.2.5.1.1 "><p id="p18658132191310"><a name="p18658132191310"></a><a name="p18658132191310"></a>马达</p> 55 </td> 56 <td class="cellrowborder" valign="top" width="28.537146285371463%" headers="mcps1.2.5.1.2 "><p id="p8826341131218"><a name="p8826341131218"></a><a name="p8826341131218"></a>ohos.permission.VIBRATE</p> 57 </td> 58 <td class="cellrowborder" valign="top" width="18.168183181681833%" headers="mcps1.2.5.1.3 "><p id="p207991683914"><a name="p207991683914"></a><a name="p207991683914"></a>system_grant</p> 59 </td> 60 <td class="cellrowborder" valign="top" width="37.56624337566243%" headers="mcps1.2.5.1.4 "><p id="p118061614399"><a name="p118061614399"></a><a name="p118061614399"></a>允许应用程序使用马达</p> 61 </td> 62 </tr> 63 </tbody> 64 </table> 65 66 67## 使用<a name="section1581412211528"></a> 68 69本节以马达为例,说明其提供的接口功能以及使用流程。 70 71### 接口说明<a name="section15684191115524"></a> 72 73马达主要提供的功能有:触发振动,停止振动。JS API类开放能力如下: 74 75**表 2** 马达JS API的主要接口 76 77<a name="table1738121244713"></a> 78<table><thead align="left"><tr id="row4381111254710"><th class="cellrowborder" valign="top" width="31.209999999999997%" id="mcps1.2.3.1.1"><p id="p1738116127470"><a name="p1738116127470"></a><a name="p1738116127470"></a>接口名</p> 79</th> 80<th class="cellrowborder" valign="top" width="68.78999999999999%" id="mcps1.2.3.1.2"><p id="p10381161224717"><a name="p10381161224717"></a><a name="p10381161224717"></a>描述</p> 81</th> 82</tr> 83</thead> 84<tbody><tr id="row18381121274715"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p15297217910"><a name="p15297217910"></a><a name="p15297217910"></a>startVibration(effect: VibrateEffect, attribute: VibrateAttribute, callback?: AsyncCallback<void>)</p> 85</td> 86<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p143812128476"><a name="p143812128476"></a><a name="p143812128476"></a>按照指定的effect和attribute触发振动。effect为振动效果,attribute为振动属性,返回callback表示触发振动是否成功。</p> 87</td> 88</tr> 89<tr id="row29912332312"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p599153172316"><a name="p599153172316"></a><a name="p599153172316"></a>startVibration(effect: VibrateEffect, attribute: VibrateAttribute): Promise<void></p> 90</td> 91<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1099114392316"><a name="p1099114392316"></a><a name="p1099114392316"></a>按照指定的effect和attribute触发振动。effect为振动效果,attribute为振动属性,返回Promise表示触发振动是否成功。</p> 92</td> 93</tr> 94<tr id="row10382181218477"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p764313511343"><a name="p764313511343"></a><a name="p764313511343"></a>stopVibration(stopMode: VibratorStopMode, callback?: AsyncCallback<void>)</p> 95</td> 96<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1738291234712"><a name="p1738291234712"></a><a name="p1738291234712"></a>按照指定的要停止的振动模式来停止振动。stopMode为枚举马达两种振动类型,VIBRATOR_STOP_MODE_TIME、VIBRATOR_STOP_MODE_PRESET分别为停止duration模式的振动和停止预置EffectId模式的振动,返回callback表示停止振动是否成功。</p> 97</td> 98</tr> 99<tr id="row2087541618235"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p13875201620231"><a name="p13875201620231"></a><a name="p13875201620231"></a>stopVibration(stopMode: VibratorStopMode): Promise<void></p> 100</td> 101<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p14875916142317"><a name="p14875916142317"></a><a name="p14875916142317"></a>按照指定的要停止的振动模式来停止振动。stopMode为枚举马达两种振动类型,VIBRATOR_STOP_MODE_TIME、VIBRATOR_STOP_MODE_PRESET分别为停止duration模式的振动和停止预置EffectId模式的振动,返回Promise表示停止振动是否成功。</p> 102</td> 103</tr> 104<tr id="row10382181218477"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p764313511343"><a name="p764313511343"></a><a name="p764313511343"></a>stopVibration(callback: AsyncCallback<void>)</p> 105</td> 106<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1738291234712"><a name="p1738291234712"></a><a name="p1738291234712"></a>停止所有模式的马达振动,返回callback表示停止振动是否成功。</p> 107</td> 108</tr> 109<tr id="row2087541618235"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p13875201620231"><a name="p13875201620231"></a><a name="p13875201620231"></a>stopVibration(): Promise<void></p> 110</td> 111<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p14875916142317"><a name="p14875916142317"></a><a name="p14875916142317"></a>停止所有模式的马达振动,返回Promise表示停止振动是否成功。</p> 112</td> 113</tr> 114<tr id="row10382181218477"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p764313511343"><a name="p764313511343"></a><a name="p764313511343"></a>isSupportEffect(effectId: string, callback: AsyncCallback<boolean>)</p> 115</td> 116<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p1738291234712"><a name="p1738291234712"></a><a name="p1738291234712"></a>查询是否支持传入的参数effectId,返回callback表示effectId是否支持。</p> 117</td> 118</tr> 119<tr id="row2087541618235"><td class="cellrowborder" valign="top" width="31.209999999999997%" headers="mcps1.2.3.1.1 "><p id="p13875201620231"><a name="p13875201620231"></a><a name="p13875201620231"></a>isSupportEffect(effectId: string): Promise<boolean></p> 120</td> 121<td class="cellrowborder" valign="top" width="68.78999999999999%" headers="mcps1.2.3.1.2 "><p id="p14875916142317"><a name="p14875916142317"></a><a name="p14875916142317"></a>查询是否支持传入的参数effectId,返回Promise表示effectId是否支持。</p> 122</td> 123</tr> 124</tbody> 125</table> 126 127 128### 使用说明<a name="section79302049192310"></a> 129 1301. 导入vibrator包。 1312. 触发马达按照指定持续时间振动。 1323. 停止马达按照指定持续时间振动。 1334. 触发按照指定振动效果字符串振动。 1345. 停止按照指定振动效果字符串振动。 1356. 查询是否支持'haptic.clock.timer',如果支持则振动该effectId。 1367. 停止所有类型振动。 137 138下述的代码示例中,提供了马达振动使用的完整流程。 139 140``` 141//步骤1 导包 142import vibrator from '@ohos.vibrator'; 143export default { 144 onCreate() { 145 console.info('MiscdeviceJsAPI AceApplication onCreate'); 146 //步骤2 触发马达按照指定持续的时间振动 147 try { 148 vibrator.startVibration({ 149 type: 'time', 150 duration: 1000, 151 }, { 152 id: 0, 153 usage: 'alarm' 154 }, (error) => { 155 if (error) { 156 console.error('vibrate fail, error.code: ' + error.code + 'error.message: ', + error.message); 157 return; 158 } 159 console.log('Callback returned to indicate a successful vibration.'); 160 }); 161 } catch (error) { 162 console.error('errCode: ' + error.code + ' ,msg: ' + error.message); 163 } 164 //步骤3 停止马达按照指定持续的时间振动 165 try { 166 vibrator.stopVibration("time").then(() => { 167 console.log("stopVibration success"); 168 }, (error)=>{ 169 console.log("stopVibration error:" + JSON.stringify(error)); 170 }); 171 } catch (error) { 172 console.error('Exception in, error:' + JSON.stringify(error)); 173 } 174 //步骤4 触发马达按照指定的字符串效果振动 175 try { 176 vibrator.startVibration({ 177 type: 'preset', 178 effectId: 'haptic.clock.timer', 179 count: 1, 180 }, { 181 usage: 'unknown' 182 }).then(()=>{ 183 console.log('Promise returned to indicate a successful vibration'); 184 }).catch((error)=>{ 185 console.error('Promise returned to indicate a failed vibration:' + JSON.stringify(error)); 186 }); 187 } catch (error) { 188 console.error('exception in, error:' + JSON.stringify(error)); 189 } 190 //步骤5 停止马达按照指定的字符串效果振动 191 try { 192 vibrator.stopVibration("preset").then(() => { 193 console.log("stopVibration success"); 194 }, (error)=>{ 195 console.log("stopVibration error:" + JSON.stringify(error)); 196 }); 197 } catch (error) { 198 console.error('Exception in, error:' + JSON.stringify(error)); 199 } 200 //步骤6 查询是否支持'haptic.clock.timer',如果支持则振动该effectId 201 try { 202 vibrator.isSupportEffect('haptic.clock.timer', function (err, state) { 203 if (err) { 204 console.error('isSupportEffect failed, error:' + JSON.stringify(err)); 205 return; 206 } 207 console.log('The effectId is ' + (state ? 'supported' : 'unsupported')); 208 if (state) { 209 try { 210 vibrator.startVibration({ 211 type: 'preset', 212 effectId: 'haptic.clock.timer', 213 count: 1, 214 }, { 215 usage: 'unknown' 216 }, (error) => { 217 if(error) { 218 console.error('haptic.clock.timer vibrator error:' + JSON.stringify(error)); 219 } else { 220 console.log('haptic.clock.timer vibrator success'); 221 } 222 }); 223 } catch (error) { 224 console.error('Exception in, error:' + JSON.stringify(error)); 225 } 226 } 227 }) 228 } catch (error) { 229 console.error('Exception in, error:' + JSON.stringify(error)); 230 } 231 //步骤7 停止所有类型振动 232 try { 233 vibrator.stopVibration(function (error) { 234 if (error) { 235 console.log('error.code' + error.code + 'error.message' + error.message); 236 return; 237 } 238 console.log('Callback returned to indicate successful.'); 239 }) 240 } catch (error) { 241 console.info('errCode: ' + error.code + ' ,msg: ' + error.message); 242 } 243 } 244 onDestroy() { 245 console.info('AceApplication onDestroy'); 246 } 247} 248``` 249 250## 相关仓<a name="section96071132185310"></a> 251 252泛Sensor子系统 253 254[sensors\_sensor](https://gitee.com/openharmony/sensors_sensor) 255 256**sensors\_miscdevice** 257 258