import sensor from "@ohos.sensor"
import BusinessError from "@ohos.base"
try {
sensor.once(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => {
console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta);
console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma);
console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha);
});
} catch (error) {
let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`);
}
调用OrientationResponse接口可以读取到alpha、beta、gamma三个属性分别表示Z轴、X轴、Y轴的旋转角度,可以读取角度进行判断,示例参考如下: