This commit is contained in:
Dennis Guse
2019-10-29 23:05:17 +01:00
parent 9e877e4762
commit e52b1188a3
4 changed files with 16 additions and 22 deletions
@@ -32,6 +32,7 @@ import java.util.UUID;
import de.dennisguse.opentracks.content.sensor.SensorDataSet; import de.dennisguse.opentracks.content.sensor.SensorDataSet;
import de.dennisguse.opentracks.content.sensor.SensorState; import de.dennisguse.opentracks.content.sensor.SensorState;
import de.dennisguse.opentracks.util.BluetoothUtils;
/** /**
* Manages connection to Bluetooth LE heart rate monitor. * Manages connection to Bluetooth LE heart rate monitor.
@@ -103,7 +104,7 @@ public class BluetoothConnectionManager {
@Override @Override
public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic) { public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic) {
int heartRate = BluetoothLEUtils.parseHeartRate(characteristic); int heartRate = BluetoothUtils.parseHeartRate(characteristic);
Log.d(TAG, "Received heart beat rate: " + heartRate); Log.d(TAG, "Received heart beat rate: " + heartRate);
SensorDataSet sensorDataSet = new SensorDataSet(heartRate, gatt.getDevice().getName(), gatt.getDevice().getAddress()); SensorDataSet sensorDataSet = new SensorDataSet(heartRate, gatt.getDevice().getName(), gatt.getDevice().getAddress());
@@ -1,17 +0,0 @@
package de.dennisguse.opentracks.services.sensors;
import android.bluetooth.BluetoothGattCharacteristic;
final class BluetoothLEUtils {
private BluetoothLEUtils() {
}
static int parseHeartRate(BluetoothGattCharacteristic characteristic) {
//DOCUMENTATION https://www.bluetooth.com/specifications/gatt/characteristics/
byte[] raw = characteristic.getValue();
int index = ((raw[0] & 0x1) == 1) ? 2 : 1;
int format = (index == 1) ? BluetoothGattCharacteristic.FORMAT_UINT8 : BluetoothGattCharacteristic.FORMAT_UINT16;
return characteristic.getIntValue(format, index);
}
}
@@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import de.dennisguse.opentracks.R; import de.dennisguse.opentracks.R;
import de.dennisguse.opentracks.util.BluetoothDeviceUtils; import de.dennisguse.opentracks.util.BluetoothUtils;
import de.dennisguse.opentracks.util.PreferencesUtils; import de.dennisguse.opentracks.util.PreferencesUtils;
import de.dennisguse.opentracks.util.UnitConversions; import de.dennisguse.opentracks.util.UnitConversions;
@@ -192,7 +192,7 @@ final class PreferenceHelper {
List<String> devicesAddressList = new ArrayList<>(); List<String> devicesAddressList = new ArrayList<>();
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) { if (bluetoothAdapter != null) {
BluetoothDeviceUtils.populateDeviceLists(bluetoothAdapter, devicesNameList, devicesAddressList); BluetoothUtils.populateDeviceLists(bluetoothAdapter, devicesNameList, devicesAddressList);
} }
// Was the previously configured device unpaired? Then forget it. // Was the previously configured device unpaired? Then forget it.
@@ -18,6 +18,7 @@ package de.dennisguse.opentracks.util;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGattCharacteristic;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -27,9 +28,9 @@ import java.util.Set;
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class BluetoothDeviceUtils { public class BluetoothUtils {
private BluetoothDeviceUtils() { private BluetoothUtils() {
} }
/** /**
@@ -53,4 +54,13 @@ public class BluetoothDeviceUtils {
} }
} }
} }
public static int parseHeartRate(BluetoothGattCharacteristic characteristic) {
//DOCUMENTATION https://www.bluetooth.com/specifications/gatt/characteristics/
byte[] raw = characteristic.getValue();
int index = ((raw[0] & 0x1) == 1) ? 2 : 1;
int format = (index == 1) ? BluetoothGattCharacteristic.FORMAT_UINT8 : BluetoothGattCharacteristic.FORMAT_UINT16;
return characteristic.getIntValue(format, index);
}
} }