Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisguse committed Sep 18, 2022
1 parent c8f3495 commit 9e168aa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,26 @@ public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCh
return;
}

SensorData<DataType> 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<DataType> 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<DataType> fsd = sensorData;
observer.getHandler().post(() -> observer.onSensorDataChanged(fsd));
}
}
}
Expand Down Expand Up @@ -170,6 +176,8 @@ synchronized boolean isSameBluetoothDevice(String address) {

protected abstract SensorData<DataType> createEmptySensorData(String address);

protected abstract SensorData<DataType> createBatterySensorData(String address, String name, int batteryLevel);

/**
* @return null if data could not be parsed.
*/
Expand All @@ -179,8 +187,6 @@ interface SensorDataObserver {

void onSensorDataChanged(SensorData<?> sensorData);

void onBatteryLevelChanged(int level);

void onDisconnecting(SensorData<?> sensorData);

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public abstract class SensorData<T> {

protected T value;

protected int batteryLevel;

private final String sensorAddress;
private final String sensorName;

Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 9e168aa

Please sign in to comment.