小器件是指用于向外传递信号的设备,包括马达和LED灯,本组件对开发者提供控制马达振动和LED灯开关的能力,其架构图如下所示:
/base/sensors/miscdevice
├── frameworks # 框架代码
│ └── native # 客户端连接服务的Native方法
├── interfaces # 对外接口存放目录
│ ├── native # native 实现
│ └── plugin # Js API
├── sa_profile # 服务名称和服务的动态库的配置文件
├── services # 服务的代码目录
│ └── miscdevice_service # 小器件服务,包含马达和Led灯,控制马达振动和灯的开关
└── utils # 公共代码,包括权限、通信等能力
-
要使用器件的功能,设备必须具有对应的器件。
-
针对马达,开发者需要请求相应的权限才能使用。
表 1 器件权限列表
本节以马达为例,说明其提供的接口功能以及使用流程。
马达主要提供的功能有:触发振动,停止振动。JS API类开放能力如下:
表 2 马达JS API的主要接口
- 导入vibrator包。
- 触发马达按照指定持续时间振动。
- 停止马达按照指定持续时间振动。
- 触发按照指定振动效果字符串振动。
- 停止按照指定振动效果字符串振动。
- 查询是否支持'haptic.clock.timer',如果支持则振动该effectId。
- 停止所有类型振动。
下述的代码示例中,提供了马达振动使用的完整流程。
//步骤1 导包
import vibrator from '@ohos.vibrator';
export default {
onCreate() {
console.info('MiscdeviceJsAPI AceApplication onCreate');
//步骤2 触发马达按照指定持续的时间振动
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);
}
//步骤3 停止马达按照指定持续的时间振动
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));
}
//步骤4 触发马达按照指定的字符串效果振动
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));
}
//步骤5 停止马达按照指定的字符串效果振动
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));
}
//步骤6 查询是否支持'haptic.clock.timer',如果支持则振动该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));
}
//步骤7 停止所有类型振动
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');
}
}
泛Sensor子系统
sensors_miscdevice