gpt4 book ai didi

java - Android 上的设备方向

转载 作者:行者123 更新时间:2023-12-01 13:46:38 26 4
gpt4 key购买 nike

我正在尝试获取 Galaxy Note 2 上的设备方向。这是我的 onSensorChanged() 方法的代码,但 RotationMatrix 不断返回 false,因此未计算方向。我花了几个小时浏览文档并在谷歌上搜索解决方案,但它们都提供了类似的实现,我似乎无法理解我做错了什么。

出于测试目的,我刚刚将 Azimut 输出到控制台。

private float[] gravityMatrix;
private float[] magneticFieldMatrix;
@Override
public void onSensorChanged(SensorEvent event)
{
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
gravityMatrix = event.values;
}
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
{
magneticFieldMatrix = event.values;
}
if(gravityMatrix != null && magneticFieldMatrix != null)
{
float[] rotationMatrix = new float[9];
float[] inclinationMatrix = new float[9];
boolean success = SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, gravityMatrix, magneticFieldMatrix);
System.out.println(success);
if(success)
{
float orientation[] = new float[3];
SensorManager.getOrientation(rotationMatrix, orientation);
System.out.println("Azimut: " + orientation[0]);
}
}

这是 LogCat:

12-02 01:20:45.885: D/GestureDetector(18770): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
12-02 01:20:46.040: D/SensorManager(18770): registerListener :: handle = 0 name= LSM330DLC Acceleration Sensor delay= 200000 Listener= com.Ayush.Finch.Misson_Finch.ActivityControlPanel@42bfefd0
12-02 01:20:46.045: D/SensorManager(18770): registerListener :: handle = 1 name= AK8963C Magnetic field Sensor delay= 200000 Listener= com.Ayush.Finch.Misson_Finch.ActivityControlPanel@42bfefd0
12-02 01:20:46.050: D/SensorManager(18770): onAccuracyChanged :: accuracy = 3
12-02 01:20:46.050: I/System.out(18770): false
12-02 01:20:46.195: I/System.out(18770): false
12-02 01:20:46.245: I/System.out(18770): false
12-02 01:20:46.375: I/System.out(18770): false
12-02 01:20:46.445: I/System.out(18770): false
12-02 01:20:46.555: I/System.out(18770): false
12-02 01:20:46.645: I/System.out(18770): false
12-02 01:20:46.735: I/System.out(18770): false
12-02 01:20:46.845: I/System.out(18770): false
12-02 01:20:46.915: I/System.out(18770): false
12-02 01:20:47.045: I/System.out(18770): false
12-02 01:20:47.095: I/System.out(18770): false
12-02 01:20:47.245: I/System.out(18770): false
12-02 01:20:47.275: I/System.out(18770): false
12-02 01:20:47.445: I/System.out(18770): false
12-02 01:20:47.455: I/System.out(18770): false
12-02 01:20:47.635: I/System.out(18770): false
12-02 01:20:47.645: I/System.out(18770): false
12-02 01:20:47.815: I/System.out(18770): false
12-02 01:20:47.845: I/System.out(18770): false
12-02 01:20:47.995: I/System.out(18770): false
12-02 01:20:48.045: I/System.out(18770): false
12-02 01:20:48.175: I/System.out(18770): false
12-02 01:20:48.245: I/System.out(18770): false
12-02 01:20:48.355: I/System.out(18770): false
12-02 01:20:48.445: I/System.out(18770): false
12-02 01:20:48.535: I/System.out(18770): false
12-02 01:20:48.650: I/System.out(18770): false
12-02 01:20:48.715: I/System.out(18770): false
12-02 01:20:48.850: I/System.out(18770): false
12-02 01:20:48.895: I/System.out(18770): false
12-02 01:20:49.045: I/System.out(18770): false
12-02 01:20:49.075: I/System.out(18770): false
12-02 01:20:49.245: I/System.out(18770): false
12-02 01:20:49.255: I/System.out(18770): false
12-02 01:20:49.415: D/GestureDetector(18770): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
12-02 01:20:49.415: D/SensorManager(18770): unregisterListener:: Listener= com.Ayush.Finch.Misson_Finch.ActivityControlPanel@42bfefd0
12-02 01:20:49.420: D/Sensors(18770): Remain listener = Sending .. normal delay 200ms
12-02 01:20:49.420: I/Sensors(18770): sendDelay --- 200000000
12-02 01:20:49.420: D/SensorManager(18770): JNI - sendDelay
12-02 01:20:49.420: I/SensorManager(18770): Set normal delay = true
12-02 01:20:49.420: D/Sensors(18770): Remain listener = Sending .. normal delay 200ms
12-02 01:20:49.420: I/Sensors(18770): sendDelay --- 200000000
12-02 01:20:49.420: D/SensorManager(18770): JNI - sendDelay
12-02 01:20:49.420: I/SensorManager(18770): Set normal delay = true

最佳答案

尝试在 Manifest 文件中添加这两个权限:

android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION

祝你编程顺利!

关于java - Android 上的设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com