mirror of
https://codeberg.org/OpenTracksApp/OpenTracks.git
synced 2026-09-30 16:52:21 +02:00
Merge branch 'stickflowerfightclub-main'
This commit is contained in:
@@ -9,6 +9,8 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import de.dennisguse.opentracks.data.models.AtmosphericPressure;
|
||||
@@ -17,12 +19,14 @@ public class BarometerInternal implements Driver {
|
||||
|
||||
private static final String TAG = BarometerInternal.class.getSimpleName();
|
||||
|
||||
private static final int SAMPLING_PERIOD = (int) TimeUnit.SECONDS.toMicros(5);
|
||||
private static final Duration SAMPLING_PERIOD = Duration.ofSeconds(5);
|
||||
|
||||
private final DriverObserver<AtmosphericPressure> observer;
|
||||
|
||||
private android.hardware.SensorManager sensorService;
|
||||
|
||||
private Instant lastEventTime = Instant.EPOCH;
|
||||
|
||||
private final SensorEventListener sensorEventListener = new SensorEventListener() {
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
@@ -31,6 +35,13 @@ public class BarometerInternal implements Driver {
|
||||
return;
|
||||
}
|
||||
|
||||
Instant currentTime = Instant.now();
|
||||
|
||||
if (lastEventTime.plus(SAMPLING_PERIOD).isAfter(currentTime)) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastEventTime = currentTime;
|
||||
observer.onDataReceived(AtmosphericPressure.ofHPA(event.values[0]));
|
||||
}
|
||||
|
||||
@@ -48,8 +59,9 @@ public class BarometerInternal implements Driver {
|
||||
public void connect(Context context, Handler handler, String addressIgnored) {
|
||||
sensorService = (android.hardware.SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
|
||||
Sensor pressureSensor = sensorService.getDefaultSensor(Sensor.TYPE_PRESSURE);
|
||||
int samplingPeriodUs = (int) TimeUnit.MICROSECONDS.convert(SAMPLING_PERIOD);
|
||||
|
||||
if (pressureSensor != null && sensorService.registerListener(sensorEventListener, pressureSensor, SAMPLING_PERIOD, handler)) {
|
||||
if (pressureSensor != null && sensorService.registerListener(sensorEventListener, pressureSensor, samplingPeriodUs, handler)) {
|
||||
observer.onConnected(null, null);
|
||||
return;
|
||||
}
|
||||
|
||||
+22
-20
@@ -191,26 +191,6 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
|
||||
List<ParcelUuid> serviceUUIDs = getArguments().getParcelableArrayList(ARG_BLE_SERVICE_UUIDS);
|
||||
boolean includeInternalSensor = getArguments().getBoolean(ARG_INCLUDE_INTERNAL);
|
||||
|
||||
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(getContext());
|
||||
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
|
||||
Log.w(TAG, "Bluetooth adapter is present or not enabled.");
|
||||
Toast.makeText(getContext(), R.string.bluetooth_disabled, Toast.LENGTH_SHORT).show();
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
|
||||
if (bluetoothAdapter.isDiscovering()) {
|
||||
Log.i(TAG, "Cancelling ongoing Bluetooth discovery.");
|
||||
bluetoothAdapter.cancelDiscovery();
|
||||
}
|
||||
|
||||
scanner = bluetoothAdapter.getBluetoothLeScanner();
|
||||
if (scanner == null) {
|
||||
Log.e(TAG, "BluetoothLeScanner is null.");
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
|
||||
listAdapter.add(SensorType.NONE.getPreferenceValue(), getContext().getString(DEVICE_NONE_RESOURCEID));
|
||||
selectedEntryIndex = 0;
|
||||
|
||||
@@ -228,6 +208,28 @@ public abstract class BluetoothLeSensorPreference extends DialogPreference {
|
||||
selectedEntryIndex = !includeInternalSensor ? 1 : 2;
|
||||
}
|
||||
|
||||
BluetoothAdapter bluetoothAdapter = BluetoothUtils.getAdapter(getContext());
|
||||
if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
|
||||
Log.w(TAG, "Bluetooth adapter is present or not enabled. Skipping BLE scan.");
|
||||
Toast.makeText(getContext(), R.string.bluetooth_disabled, Toast.LENGTH_SHORT).show();
|
||||
if (!includeInternalSensor) {
|
||||
dismiss();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (bluetoothAdapter.isDiscovering()) {
|
||||
Log.i(TAG, "Cancelling ongoing Bluetooth discovery.");
|
||||
bluetoothAdapter.cancelDiscovery();
|
||||
}
|
||||
|
||||
scanner = bluetoothAdapter.getBluetoothLeScanner();
|
||||
if (scanner == null) {
|
||||
Log.e(TAG, "BluetoothLeScanner is null.");
|
||||
dismiss();
|
||||
return;
|
||||
}
|
||||
|
||||
// Pre-populate with already-bonded BLE devices so that devices that are paired but not actively advertising (e.g. Bosch eBike) are still shown. In case we dont have any cached UUIDs for a device, we just add it to the list and let the user decide
|
||||
boolean filterEnabled = PreferencesUtils.getBluetoothFilterEnabled();
|
||||
bluetoothAdapter.getBondedDevices().stream()
|
||||
|
||||
Reference in New Issue
Block a user