#跟着坚果学鸿蒙#场景介绍 • 通过方向传感器数据,可以感知用户设备当前的朝向,从而达到为用户指明方位的 目的。 • 通过重力和陀螺仪传感器数据,能感知设备倾斜和旋转量,提高用户在游戏场景中 的体验。 • 通过接近光传感器数据,感知距离遮挡物的距离,使设备能够自动亮灭屏,达到防 误触目的。 • 通过气压计传感器数据,可以准确的判断设备当前所处的海拔。 • 通过环境光传感器数据,设备能够实现背光自动调节。

接口说明 HarmonyOS 传感器提供的功能有:查询传感器的列表、订阅/取消订阅传感器数 据、查询传感器的最小采样时间间隔、执行控制命令。 以订阅方向类别的传感器数据为例,本节示例涉及的接口如下:

开发步骤 权限配置 需要请求相应的权限,开发者才能获取到传 感器数据。

开发者需要在 config.json 里面配置权限: • 开发者如果需要获取加速度的数据,需要进行如下权限配置。

"reqPermissions": [ 
 { 
 "name": "ohos.permission.ACCELEROMETER", 
 "reason": "", 
 "usedScene": { 
 "ability": [ 
 ".MainAbility" 
 ], 
 "when": "inuse" 
 } 
 } 
] 

对于需要用户授权的权限,如计步器传感器,需要进行如下权限配置。

"reqPermissions": [ 
 { 
 "name": "ohos.permission.ACTIVITY_MOTION", 
 "reason": "", 
"usedScene": { 
 "ability": [ 
 ".MainAbility" 
 ], 
 "when": "inuse" 
 } 
 } 
] 

由于敏感权限需要用户授权,因此,开发者在应用启动时或者调用订阅数据接口 前,需要调用权限检查和请求权限接口。

@Override 
public void onStart(Intent intent) { 
 super.onStart(intent); 
 if (verifySelfPermission("ohos.permission.ACTIVITY_MOTION") != 0) { 
 if (canRequestPermission("ohos.permission.ACTIVITY_MOTION")) { 
 requestPermissionsFromUser(new String[] 
{"ohos.permission.ACTIVITY_MOTION"}, 1); 
 } 
 } 
 // ... 
} 
@Override 
public void onRequestPermissionsFromUserResult(int requestCode, String[] 
permissions, 
 int[] grantResults) { 
 switch (requestCode) { 
case 1: { 
 // 匹配 requestPermissionsFromUser 的 requestCode 
 if (grantResults.length > 0 && grantResults[0] == 0) { 
 // 权限被授予 
 } else { 
 // 权限被拒绝 
 } 
 return; 
 } 
 } 
} 

使用传感器 以使用方向类别的传感器为例,运动类、环境类、健康类等类别的传感器使用方 法类似。 1. 获取待订阅数据的传感器。 2. 创建传感器回调。 3. 订阅传感器数据。 4. 接收并处理传感器数据。 5. 取消订阅传感器数据。

private Button btnSubscribe; 
 
private Button btnUnsubscribe; 
 
private CategoryOrientationAgent categoryOrientationAgent = new 
CategoryOrientationAgent(); 
 
private ICategoryOrientationDataCallback orientationDataCallback; 
private CategoryOrientation orientationSensor; 
 
private long interval = 100000000; 
 
@Override 
public void onStart(Intent intent) { 
 super.onStart(intent); 
 super.setUIContent(ResourceTable.Layout_sensor_layout); 
 findComponent(rootComponent); 
 
 // 创建传感器回调对象。 
 orientationDataCallback = new ICategoryOrientationDataCallback() {
@Override 
 public void onSensorDataModified(CategoryOrientationData 
categoryOrientationData) { 
 // 对接收的 categoryOrientationData 传感器数据对象解析和使
用 
 int dim = categoryOrientationData.getSensorDataDim(); //
获取传感器的维度信息 
 float degree = categoryOrientationData.getValues()[0]; // 
获取方向类传感器的第一维数据 
 } 
 
 @Override 
 public void onAccuracyDataModified(CategoryOrientation 
categoryOrientation, int i) { 
 // 使用变化的精度 
 } 
@Override 
 public void onCommandCompleted(CategoryOrientation 
categoryOrientation) { 
 // 传感器执行命令回调 
 } 
 }; 
 
 btnSubscribe.setClickedListener(v -> { 
 // 获取传感器对象,并订阅传感器数据 
 orientationSensor = categoryOrientationAgent.getSingleSensor( 
 CategoryOrientation.SENSOR_TYPE_ORIENTATION); 
 if (orientationSensor != null) { 
 categoryOrientationAgent.setSensorDataCallback( 
orientationDataCallback, orientationSensor, 
interval); 
 } 
 }); 
 // 取消订阅传感器数据 
 btnUnsubscribe.setClickedListener(v -> { 
 if (orientationSensor != null) { 
 categoryOrientationAgent.releaseSensorDataCallback( 
 orientationDataCallback, orientationSensor); 
 } 
 }); 
} private void findComponent(Component component) { 
 btnSubscribe = (Button) 
component.findComponentById(Resource.Id.btnSubscribe); btnUnsubscribe = (Button) 
component.findComponentById(Resource.Id.btnUnsubscribe); 
} 

 

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐