我正在處理一個應用程序,其中的一部分是估計用戶所在建築物的樓層。我試圖在設備上使用氣壓計/壓力傳感器,支持它並試圖根據這個做出估計。如果用戶沒有氣壓計/壓力傳感器,應用程序會打開一個手動輸入對話框,否則估計高度和樓層數量。壓力傳感器沒有得到任何值
現在,我只是想讓氣壓計/壓力傳感器工作。我使用的是具有可設定壓力值的Android模擬器,和我的代碼的相關部分被設置如下:
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是一個自定義的一個實例class 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>
眼下,pressureBasedFloor的整個價值即使在我的仿真器的壓力值設定爲990。有沒有錯誤,保持0。
我在做什麼錯?