Skip to content

clipsheep6/sensors_miscdevice

Repository files navigation

Misc Device

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

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

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

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

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

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

stopVibration(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.

stopVibration(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.

stopVibration(callback: AsyncCallback<void>)

Stop all types of vibration. This API uses a callback to indicate whether the vibration is stopped successfully.

stopVibration(): Promise<void>

Stop all types of vibration. This API uses a promise to indicate whether the vibration is stopped successfully.

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

Query whether the effectId is supported. This API uses a callback to indicate whether the query result of EffectId is successful.

isSupportEffect(effectId: string): Promise<boolean>

Query whether the effectId is supported. This API uses a promise to indicate whether the query result of EffectId is successful.

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.
  6. Query whether 'haptic. lock. timer' is supported. If so, vibrate the effectId.
  7. Stop all types of vibration.

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.
        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);
        }
        // Step 3 Stop vibration that is triggered with a specific duration.
        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));
        }
        // Step 4 Trigger vibration based on with a specific effect.
        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));
        }
        // Step 5 Stop vibration that is triggered with a specific effect.
        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));
        }
        // Step 6 Query whether 'haptic. lock. timer' is supported. If so, vibrate the 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));
        }
        // Step 7 Stop all types of vibration.
        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');
    }
}

Repositories Involved

Pan-sensor subsystem

sensors_sensor

sensors_miscdevice

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published