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)
HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

参考示例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"
}
]
分享
微博
QQ
微信
回复
2天前
相关问题
获取加速度传感器的方法
513浏览 • 1回复 待解决
HarmonyOS 传感器判断屏幕方向
58浏览 • 1回复 待解决
重力传感器相关问题咨询
371浏览 • 1回复 待解决
Sensor 步数传感器后台情况
496浏览 • 1回复 待解决
订阅接近光传感器只出一次数据
1788浏览 • 1回复 待解决
HarmonyOS 计步传感器相关问题咨询?
240浏览 • 1回复 待解决
如何保证振弦传感器的测量精度?
3446浏览 • 0回复 待解决
什么是智能振弦传感器的电子标签?
3364浏览 • 0回复 待解决