压力传感器没有任何值

时间:2017-04-06 04:40:06

标签: android android-sensors

我正在处理一个应用程序,其中一部分包括估算用户所在建筑物的楼层。我试图在支持它的设备上使用气压计/压力传感器并试图根据这个做出估计。如果用户没有气压计/压力传感器,应用程序会打开一个手动输入对话框,否则会估算高度和楼层数。

现在,我正试着让气压计/压力传感器正常工作。我使用的Android Emulator具有可设置的压力值,我的代码的相关部分设置如下:

MainActivity.java

public class MainActivity extends AppCompatActivity implements SensorEventListener, .... {

    //sensor variables
    public float mPressureValue = 0.0f;
    public float mHeight = 0.0f;
    public Integer pressureBasedFloor = 0;

    //check if device has pressure sensor, setup in OnCreate
    boolean hasBarometer = false;

    //pressure sensor to get pressure, height and floor
    @Override
    public void onSensorChanged(SensorEvent event) {
        //if you use this listener as listener of only one sensor (ex, Pressure), then you don't need to check sensor type.
        //if a pressure sensor exists, use it to calculate height
        if (hasBarometer) {
            if( Sensor.TYPE_PRESSURE == event.sensor.getType() ) {
                mPressureValue = event.values[0];
                System.out.println("Pressure" + mPressureValue);
                mHeight = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, mPressureValue);
                System.out.println("Height" + mHeight);
                pressureBasedFloor = Math.round(mHeight);
            }
        }
    }

     //pressure sensor
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }


     @Override
    protected void onCreate(Bundle savedInstanceState) {

        //check if barometer sensor exists
        hasBarometer = getPackageManager().hasSystemFeature(PackageManager.FEATURE_SENSOR_BAROMETER);

        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    //if user has no barometer sensor, request dialog
                    if (!hasBarometer) {
                        showRequestFloorDialog(view); //no barometer found, manual entry
                    } else {
                        currUserData.setUserFloorNumber(pressureBasedFloor); //barometer found, add calculated floor number to field
                    }
                }
            }
        });
    }
}

currUserData是自定义类UserData的实例,它存储用户的估计楼层数。我的清单文件设置如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.flamedra.findmycar">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-feature android:name="android.hardware.sensor.pressure" android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_profile"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:configChanges="orientation|keyboardHidden|screenSize">
        <activity
            android:name=".activities.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:configChanges="orientation|keyboardHidden|screenSize"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
                    android:name=".activities.CompassFragmentActivity"
                    android:theme="@style/AppTheme.NoActionBar"
                    android:configChanges="orientation|keyboardHidden|screenSize"
                   />

        <!--Maps Android Key-->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_api_key" />
        <!--Maps Android Key-->
    </application>

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

</manifest>

现在,即使我的模拟器上的压力值设置为990,pressureBasedFloor的值仍然保持为0。没有错误。

我做错了什么?

1 个答案:

答案 0 :(得分:4)

Emulator没有硬件传感器,因此您需要从AndroidManifest.xml中删除以下行:

android:name="android.hardware.sensor.pressure"
android:required="true"

在MainActivity中,您忘记使用SensorManager注册。看看下面的代码:

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    float pressure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_PRESSURE) {
            pressure = event.values[0];
            Log.i("Baro", " Pressure " + pressure);
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        Log.i("Baro", " Accuracy " + accuracy);
    }
}