# Misc Device - [Introduction](#section11660541593) - [Directory Structure](#section44981327519) - [Constraints](#section98068674513) - [Usage](#section1581412211528) - [Available APIs](#section15684191115524) - [How to Use](#section79302049192310) - [Repositories Involved](#section96071132185310) ## Introduction Misc 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. **Figure 1** Misc device architecture ![](figures/en-us_image_0000001152988366.png) ## Directory Structure ``` /base/sensors/miscdevice ├── frameworks # Framework code │ └── native # Native methods for the client to connect to services ├── interfaces # External APIs │ ├── native # Native implementation │ └── plugin # JS APIs ├── sa_profile # Configuration file of system ability names and dynamic libraries ├── services # Code of services │ └── miscdevice_service # Misc device service, which is used to control the vibration of vibrators and lighting-on and lighting-off of LED lights └── utils # Common code, including permissions and communication capabilities ``` ## Constraints - The APIs are valid only when your hardware is equipped with the required misc devices. - To use vibrators, you need to request the required permissions. **Table 1** Permissions required by misc devices

Misc Device

Permission Name

Sensitivity

Permission Description

Vibrator

ohos.permission.VIBRATE

system_grant

Allows an application to use the vibrator.

## Usage This section describes the features and usage of the vibrator APIs. ### Available APIs The APIs provided for the vibrator are used to trigger and stop vibration. The following table describes these APIs. **Table 2** JS APIs for the vibrator

API

Description

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

Triggers vibration based on the duration specified by duration. This API uses a callback to indicate whether the vibration is triggered successfully.

vibrate(duration: number): Promise<void>

Triggers vibration based on the duration specified by duration. This API uses a promise to indicate whether the vibration is triggered successfully.

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

Triggers vibration based on the vibration effect specified by effectId. This API uses a callback to indicate whether the vibration is triggered successfully.

vibrate(effectId: EffectId): Promise<void>

Triggers vibration based on the vibration effect specified by effectId. This API uses a promise to indicate whether the vibration is triggered successfully.

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

Stops vibration based on the mode specified by stopMode. There are two modes: VIBRATOR_STOP_MODE_TIME and VIBRATOR_STOP_MODE_PRESET, which are used to stop vibration triggered by duration and effectId, respectively. This API uses a callback to indicate whether the vibration is stopped successfully.

stop(stopMode: VibratorStopMode): Promise<void>

Stops vibration based on the mode specified by stopMode. There are two modes: VIBRATOR_STOP_MODE_TIME and VIBRATOR_STOP_MODE_PRESET, which are used to stop vibration triggered by duration and effectId, respectively. This API uses a promise to indicate whether the vibration is stopped successfully.

### How to Use 1. Import the **vibrator** package. 2. Trigger vibration with a specific duration. 3. Stop vibration that is triggered with a specific duration. 4. Trigger vibration with a specific effect. 5. Stop vibration that is triggered with a specific effect. The following sample code provides a complete process of using the vibrator APIs: ``` //Step 1 Import the vibrator package. import vibrator from '@ohos.vibrator'; export default { onCreate() { console.info('MiscdeviceJsAPI AceApplication onCreate'); // Step 2 Trigger vibration with a specific duration. vibrator.vibrate(100, function(error) { if (error) { console.error("Failed to trigger vibration. Error code: " + error.code); return; } console.info("Succeeded in triggering vibration."); }); // Step 3 Stop vibration that is triggered with a specific duration. vibrator.stop("time", function(error) { if (error) { console.error("Failed to stop vibration. Error code: " + error.code); return; } console.info("Succeeded in stopping vibration."); }); // Step 4 Trigger vibration based on with a specific effect. 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."); }); // Step 54 Stop vibration that is triggered with a specific effect. 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'); } } ``` ## Repositories Involved Pan-sensor subsystem [sensors\_sensor](https://gitee.com/openharmony/sensors_sensor) **sensors\_miscdevice**