【问题标题】:Android: Only Acclerometer reading is shown,Gyroscope sensor is not readingAndroid:仅显示加速度计读数,陀螺仪传感器未读数
【发布时间】:2013-12-02 10:16:34
【问题描述】:

我尝试了this link的陀螺仪代码

问题是只有当传感器是加速度计时它才会显示值。 我尝试使用

显示所有传感器
List<Sensor> sensors = sm.getSensorList(Sensor.TYPE_ALL);
    for (Sensor sensor : sensors) {
        Log.d("Sensors", "" + sensor.getName());
    }

我得到了结果

12-02 15:44:20.816: D/Sensors(15757): AMI304 3-axis Magnetic Field sensor
12-02 15:44:20.816: D/Sensors(15757): ADXL345 3-axis Accelerometer
12-02 15:44:20.817: D/Sensors(15757): bh1620fvc Light Sensor
12-02 15:44:20.817: D/Sensors(15757): MPU3000  gyroscope Sensor
12-02 15:44:20.817: D/Sensors(15757): Rotation Vector Sensor
12-02 15:44:20.817: D/Sensors(15757): Gravity Sensor
12-02 15:44:20.817: D/Sensors(15757): Linear Acceleration Sensor
12-02 15:44:20.817: D/Sensors(15757): Orientation Sensor
12-02 15:44:20.817: D/Sensors(15757): Corrected Gyroscope Sensor

所以陀螺仪就在那里。那为什么加速度计只显示值? 请帮助我

编辑:

我正在使用带有 android 4.0.4 的 HCL ME Y3 平板电脑

代码

package fortyonepost.com.ag;//Created by: DimasTheDriver on Apr/27/2010. Available at: 

http://www.41post.com/?p=3745

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AccessGyroscope extends Activity implements SensorEventListener
{
//a TextView
private TextView tv;
//the Sensor Manager
private SensorManager sManager;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //get the TextView from the layout file
    tv = (TextView) findViewById(R.id.tv);

    //get a hook to the sensor service
    sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}

//when this Activity starts
@Override
protected void onResume() 
{
    super.onResume();
    /*register the sensor listener to listen to the gyroscope sensor, use the 
     * callbacks defined in this class, and gather the sensor information as  
     * quick as possible*/
    sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
}

//When this Activity isn't visible anymore
@Override
protected void onStop() 
{
    //unregister the sensor listener
    sManager.unregisterListener(this);
    super.onStop();
}

@Override
public void onAccuracyChanged(Sensor arg0, int arg1) 
{
    //Do nothing
}

@Override
public void onSensorChanged(SensorEvent event) 
{
    //if sensor is unreliable, return void
    if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
    {
        return;
    }

    //else it will output the Roll, Pitch and Yawn values
    tv.setText("Orientation X (Roll) :"+ Float.toString(event.values[2]) +"\n"+
               "Orientation Y (Pitch) :"+ Float.toString(event.values[1]) +"\n"+
               "Orientation Z (Yaw) :"+ Float.toString(event.values[0]));
}
}

编辑 2

我在 Galaxy 标签中尝试了相同的代码。它正在工作。但我希望它也能在这里工作。 函数onSensorChanged 只为Acceleromter 调用。它没有被任何其他传感器调用。为什么? 我是否遗漏了平板电脑设置中的任何内容?需要做些什么来启用陀螺仪?

【问题讨论】:

  • 您是否下载了项目并按原样运行,或者您是否修改了源代码?如果是这样,请发布您的代码。这个项目在我的手机上运行良好。
  • 我没有修改就运行它。但没有读数。
  • 也许操作系统在内核级别看不到硬件?
  • 无论如何请发布您的代码,那里一定有问题。另外,你在什么手机上测试它?
  • 在 onSensorChanged 中记录您的event.sensor.getType(),看看您收到了哪种传感器。也可能发生这种情况,因为它返回的准确性不可靠。请先校准陀螺仪,然后重试。

标签: android android-sensors


【解决方案1】:

您的代码看起来不错。唯一想到的是,在 onSensorChanged 中,event.accuracy 在您的设备上总是不可靠的。试试添加 Log.d("MYTAG", "Accuracy unreliable") 看看是不是这样:

//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
    Log.d("MYTAG", "Accuracy unreliable");
    return;
}

另请注意,Sensor.TYPE_ORIENTATION 自 API 级别 8 起已弃用。请改用 SensorManager.getOrientation() 或 Sensor.TYPE_GYROSCOPE。查看文档here

【讨论】:

  • 他不想使用方向......如果我理解正确的话,准确性指的是别的东西。检查文档developer.android.com/reference/android/hardware/…
  • 他从他提供的链接运行项目,该链接又使用 TYPE_ORIENTATION,他的问题是为什么它不显示任何值。同样的项目在我的手机上运行良好。现在,如果您提供的文档中指定的可以使用 event.accuracy 检查的传感器精度是 SENSOR_STATUS_UNRELIABLE,那么他用于显示值的代码甚至不会运行。
  • Log.d("MYTAG", "Accuracy unreliable"); 没有记录。 onSensorChanged 函数甚至没有被 Gyro 调用,只被 Accelerometer 调用
  • 看起来与您的设备有关。从 Google Play 安装任何陀螺仪应用程序,看看它是否有效。此外,尝试尝试不同的延迟,甚至 TYPE_GYROSCOPE。
猜你喜欢
  • 1970-01-01
  • 2021-01-01
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多