HarmonyOS 获取陀螺仪传感器数据报Permission denied?

报错如下:

Error message:Permission denied.
Error code:
  SourceCode:
    sensor.on(sensor.SensorId.GYROSCOPE, this.sensorCallback);
^
Stacktrace:
  at startMonitoring (ms_sdk/src/main/ets/utils/ShakeObserver.ets:64:7)
阅读 536
1 个回答

参考示例demo:

import { sensor } from '@kit.SensorServiceKit';

@Entry
@Component
struct Twist {
  @State x: string = ''
  @State y: string = ''
  @State z: string = ''
  aboutToAppear(): void {
    sensor.on(sensor.SensorId.GYROSCOPE, this.callback.bind(this))
  }

  aboutToDisappear(): void {
    sensor.off(sensor.SensorId.GYROSCOPE, this.callback.bind(this))
  }

  callback(data: sensor.GyroscopeResponse) {
    this.x = data.x.toString()
    this.y = data.y.toString()
    this.z = data.z.toString()
    console.info('Succeeded in invoking once. X-coordinate component: ' + data.x);
    console.info('Succeeded in invoking once. Y-coordinate component: ' + data.y);
    console.info('Succeeded in invoking once. Z-coordinate component: ' + data.z);
  }

  build() {
    Column() {
      Text(this.x)
      Text(this.y)
      Text(this.z)
    }
  }
}

以及权限授权:

"requestPermissions": [
{
  "name": "ohos.permission.GYROSCOPE"
}
]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
logo
HarmonyOS
子站问答
访问
宣传栏