I'm working on an application right now, and part of it consists of estimating the floor of a building the user is in. I'm trying to use the barometer/pressure sensor on devices that support it and trying to make an estimate based off of that. If the user does not have a barometer/pressure sensor, the app opens up a dialog for manual entry, otherwise the height and floor number is estimated.
Right now, I'm just trying get the barometer/pressure sensor working. I'm using the Android Emulator which has pressure values which can be set, and the relevant sections of my code is set up as follows:
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 is an instance of a custom class UserData which stores the user's estimated floor number. My manifest file is set up as follows:
<?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>
Right now, the value of pressureBasedFloor stays 0 throughout even though the pressure value on my emulator is set to 990. There are no errors.
What am I doing wrong?