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
/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
-
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
This section describes the features and usage of the vibrator 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
- Import the vibrator package.
- Trigger vibration with a specific duration.
- Stop vibration that is triggered with a specific duration.
- Trigger vibration with a specific effect.
- Stop vibration that is triggered with a specific effect.
- Query whether 'haptic. lock. timer' is supported. If so, vibrate the effectId.
- 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');
}
}
Pan-sensor subsystem
sensors_miscdevice