Barometer: select INTERNAL or NONE (also while recording).

Part of #1424.
This commit is contained in:
Dennis Guse
2023-12-27 18:43:47 +01:00
parent 490141db2a
commit 94540606ef
9 changed files with 86 additions and 28 deletions
@@ -101,9 +101,12 @@ public class BluetoothConnectionManager {
return;
}
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
if (!descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE)) {
Log.e(TAG, "CLIENT_CHARACTERISTIC_CONFIG_UUID could not be set to ENABLE_NOTIFICATION_VALUE");
}
if (!gatt.writeDescriptor(descriptor)) {
Log.e(TAG, "CLIENT_CHARACTERISTIC_CONFIG_UUID descriptor could not be written");
}
}
@Override
@@ -105,7 +105,7 @@ public class BluetoothRemoteSensorManager implements SensorConnector, SharedPref
return;
}
if (PreferencesUtils.isBluetoothSensorAddressNone(address)) {
if (SensorType.NONE.getPreferenceValue().equals(address)) {
Log.w(TAG, "No Bluetooth address.");
connectionManager.disconnect();
return;
@@ -1,13 +1,17 @@
package de.dennisguse.opentracks.sensors;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.util.Log;
import androidx.annotation.NonNull;
import de.dennisguse.opentracks.data.models.AtmosphericPressure;
import de.dennisguse.opentracks.sensors.driver.BarometerInternal;
import de.dennisguse.opentracks.sensors.sensorData.AggregatorBarometer;
import de.dennisguse.opentracks.sensors.sensorData.Raw;
import de.dennisguse.opentracks.settings.PreferencesUtils;
/**
* Estimates the altitude gain and altitude loss using the device's pressure sensor (i.e., barometer).
@@ -16,31 +20,65 @@ public class GainManager implements SensorConnector {
private static final String TAG = GainManager.class.getSimpleName();
private final BarometerInternal driver;
private BarometerInternal driver;
private final SensorManager.SensorDataChangedObserver listener;
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener = (sharedPreferences, key) -> {
connect();
};
private Context context;
private Handler handler;
public GainManager(SensorManager.SensorDataChangedObserver listener) {
this.listener = listener;
driver = new BarometerInternal();
}
public void start(Context context, Handler handler) {
driver.connect(context, handler, this);
if (driver.isConnected()) {
listener.onConnect(new AggregatorBarometer("internal", null));
}
this.context = context;
this.handler = handler;
PreferencesUtils.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}
public void stop(Context context) {
Log.d(TAG, "Stop");
this.context = null;
this.handler = null;
PreferencesUtils.unregisterOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
driver.disconnect(context);
listener.onDisconnect(new AggregatorBarometer("internal", null));
onDisconnect(context);
}
public void onSensorValueChanged(AtmosphericPressure currentSensorValue) {
listener.onChange(new Raw<>(currentSensorValue));
}
private void connect() {
onDisconnect(context);
String address = PreferencesUtils.getBarometerSensorAddress();
switch (PreferencesUtils.getSensorType(address)) {
case NONE -> driver = null;
case INTERNAL -> driver = new BarometerInternal();
case REMOTE -> throw new RuntimeException("Not implemented"); //TODO #1424
default -> throw new RuntimeException("Not implemented");
}
if (driver != null) {
driver.connect(context, handler, this);
if (driver.isConnected()) {
listener.onConnect(new AggregatorBarometer("internal", null));
}
}
}
private void onDisconnect(@NonNull Context context) {
if (driver == null) return;
driver.disconnect(context);
listener.onDisconnect(new AggregatorBarometer("internal", null));
}
}
@@ -0,0 +1,17 @@
package de.dennisguse.opentracks.sensors;
public enum SensorType {
NONE("NONE"),
INTERNAL("INTERNAL"),
REMOTE("*");
private final String preferenceValue;
SensorType(String preferenceValue) {
this.preferenceValue = preferenceValue;
}
public String getPreferenceValue() {
return preferenceValue;
}
}