From 9e168aafe99a42751bc247aeea2e10c750f00377 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Tue, 16 Aug 2022 06:05:52 +0200 Subject: [PATCH] WIP --- .../TrackRecordingServiceTestRecording.java | 4 +-- .../sensors/BluetoothConnectionManager.java | 30 +++++++++++-------- .../sensors/BluetoothRemoteSensorManager.java | 12 -------- .../sensors/sensorData/SensorData.java | 6 ++++ 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java index a1669ae941..049c465ade 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java +++ b/src/androidTest/java/de/dennisguse/opentracks/services/TrackRecordingServiceTestRecording.java @@ -275,11 +275,11 @@ public void testRecording_blesensor_only_no_distance() { // when String sensor1 = "2020-02-02T02:02:03Z"; trackPointCreator.setClock(sensor1); - remoteSensorManager.onChanged(new SensorDataHeartRate("", "", HeartRate.of(5))); //Should be ignored + remoteSensorManager.onSensorDataChanged(new SensorDataHeartRate("", "", HeartRate.of(5))); //Should be ignored String sensor3 = "2020-02-02T02:02:13Z"; trackPointCreator.setClock(sensor3); - remoteSensorManager.onChanged(new SensorDataHeartRate("", "", HeartRate.of(7))); + remoteSensorManager.onSensorDataChanged(new SensorDataHeartRate("", "", HeartRate.of(7))); String stopTime = "2020-02-02T02:02:15Z"; trackPointCreator.setClock(stopTime); diff --git a/src/main/java/de/dennisguse/opentracks/sensors/BluetoothConnectionManager.java b/src/main/java/de/dennisguse/opentracks/sensors/BluetoothConnectionManager.java index 22bfc7f85c..23bc27ba0c 100644 --- a/src/main/java/de/dennisguse/opentracks/sensors/BluetoothConnectionManager.java +++ b/src/main/java/de/dennisguse/opentracks/sensors/BluetoothConnectionManager.java @@ -99,20 +99,26 @@ public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCh return; } + SensorData sensorData = null; if (BluetoothUtils.BATTERY.getServiceUUID().equals(characteristic.getService().getUuid())) { final Integer batteryLevel = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); Log.d(TAG, "Battery level of " + gatt.getDevice().getAddress() + ": " + batteryLevel); - observer.onBatteryLevelChanged(batteryLevel); + + if (batteryLevel != null) { + sensorData = createBatterySensorData(gatt.getDevice().getName(), gatt.getDevice().getAddress(), batteryLevel); + } } else { - SensorData sensorData = parsePayload(serviceMeasurementUUID.get(), gatt.getDevice().getName(), gatt.getDevice().getAddress(), characteristic); - if (sensorData != null) { - Log.d(TAG, "Decoded data from " + gatt.getDevice().getAddress() + ": " + sensorData); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - observer.onSensorDataChanged(sensorData); - } else { - //TODO This might lead to NPEs in case of race conditions due to shutdown. - observer.getHandler().post(() -> observer.onSensorDataChanged(sensorData)); - } + sensorData = parsePayload(serviceMeasurementUUID.get(), gatt.getDevice().getName(), gatt.getDevice().getAddress(), characteristic); + } + + if (sensorData != null) { + Log.d(TAG, "Decoded data from " + gatt.getDevice().getAddress() + ": " + sensorData); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + observer.onSensorDataChanged(sensorData); + } else { + //TODO This might lead to NPEs in case of race conditions due to shutdown. + final SensorData fsd = sensorData; + observer.getHandler().post(() -> observer.onSensorDataChanged(fsd)); } } } @@ -170,6 +176,8 @@ synchronized boolean isSameBluetoothDevice(String address) { protected abstract SensorData createEmptySensorData(String address); + protected abstract SensorData createBatterySensorData(String address, String name, int batteryLevel); + /** * @return null if data could not be parsed. */ @@ -179,8 +187,6 @@ interface SensorDataObserver { void onSensorDataChanged(SensorData sensorData); - void onBatteryLevelChanged(int level); - void onDisconnecting(SensorData sensorData); @NonNull diff --git a/src/main/java/de/dennisguse/opentracks/sensors/BluetoothRemoteSensorManager.java b/src/main/java/de/dennisguse/opentracks/sensors/BluetoothRemoteSensorManager.java index c638e0bdfc..5d2bb5085d 100644 --- a/src/main/java/de/dennisguse/opentracks/sensors/BluetoothRemoteSensorManager.java +++ b/src/main/java/de/dennisguse/opentracks/sensors/BluetoothRemoteSensorManager.java @@ -16,19 +16,14 @@ package de.dennisguse.opentracks.sensors; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.content.SharedPreferences; -import android.os.Build; import android.os.Handler; import android.util.Log; -import android.widget.Toast; import androidx.annotation.NonNull; -import androidx.core.app.NotificationCompat; import java.time.Duration; @@ -225,13 +220,6 @@ public synchronized void onSensorDataChanged(SensorData sensorData) { observer.onChange(new SensorDataSet(sensorDataSet)); } - @Override - public void onBatteryLevelChanged(int level) { - //TODO How to pass this info to the UI? - // Make it a snackbar or make it a notification? - // Do we need to notify the user every time it changes? - } - @Override public void onDisconnecting(SensorData sensorData) { sensorDataSet.remove(sensorData); diff --git a/src/main/java/de/dennisguse/opentracks/sensors/sensorData/SensorData.java b/src/main/java/de/dennisguse/opentracks/sensors/sensorData/SensorData.java index 62b4b02fad..9410af9c3d 100644 --- a/src/main/java/de/dennisguse/opentracks/sensors/sensorData/SensorData.java +++ b/src/main/java/de/dennisguse/opentracks/sensors/sensorData/SensorData.java @@ -12,6 +12,8 @@ public abstract class SensorData { protected T value; + protected int batteryLevel; + private final String sensorAddress; private final String sensorName; @@ -63,6 +65,10 @@ public T getValue() { return getNoneValue(); } + public int getBatteryLevel() { + return batteryLevel; + } + /** * Reset long term aggregated values (more than derived from previous SensorData). e.g. overall distance. */