Bluetooth logs: make them slightly more readable.

This commit is contained in:
Dennis Guse
2023-12-24 21:49:48 +01:00
parent 333f7956b5
commit aed4af5736
3 changed files with 16 additions and 14 deletions
@@ -47,24 +47,23 @@ public class BluetoothConnectionManager {
private final SensorHandlerInterface sensorHandler; private final SensorHandlerInterface sensorHandler;
private BluetoothGatt bluetoothGatt; private BluetoothGatt bluetoothGatt;
private final BluetoothGattCallback connectCallback = new BluetoothGattCallback() { private final BluetoothGattCallback connectCallback = new BluetoothGattCallback() {
@Override @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
switch (newState) { switch (newState) {
case BluetoothProfile.STATE_CONNECTING -> case BluetoothProfile.STATE_CONNECTING ->
Log.i(TAG, "Connecting to sensor: " + gatt.getDevice()); Log.i(TAG, gatt.getDevice() + ": connecting to sensor");
case BluetoothProfile.STATE_CONNECTED -> { case BluetoothProfile.STATE_CONNECTED -> {
Log.i(TAG, "Connected to sensor: " + gatt.getDevice() + "; discovering services."); Log.i(TAG, gatt.getDevice() + ": connected to sensor; discovering services");
gatt.discoverServices(); gatt.discoverServices();
} }
case BluetoothProfile.STATE_DISCONNECTING -> case BluetoothProfile.STATE_DISCONNECTING ->
Log.i(TAG, "Disconnecting from sensor: " + gatt.getDevice()); Log.i(TAG, gatt.getDevice() + ": disconnecting from sensor: ");
case BluetoothProfile.STATE_DISCONNECTED -> { case BluetoothProfile.STATE_DISCONNECTED -> {
//This is also triggered, if no connection was established (ca. 30s) //This is also triggered, if no connection was established (ca. 30s)
Log.i(TAG, "Disconnected from sensor: " + gatt.getDevice() + "; trying to reconnect"); Log.i(TAG, gatt.getDevice() + ": disconnected from sensor: trying to reconnect");
if (gatt.connect()) { if (gatt.connect()) {
Log.e(TAG, "Could not trigger reconnect for sensor: " + gatt.getDevice()); Log.e(TAG, gatt.getDevice() + ": could not trigger reconnect for sensor");
} }
clearData(); clearData();
} }
@@ -84,13 +83,13 @@ public class BluetoothConnectionManager {
} }
if (gattService == null) { if (gattService == null) {
Log.e(TAG, "Could not get gattService for address=" + gatt.getDevice().getAddress() + " serviceUUID=" + serviceMeasurement); Log.e(TAG, gatt.getDevice() + ": could not get gattService for serviceUUID=" + serviceMeasurement);
return; return;
} }
BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(serviceMeasurement.measurementUUID()); BluetoothGattCharacteristic characteristic = gattService.getCharacteristic(serviceMeasurement.measurementUUID());
if (characteristic == null) { if (characteristic == null) {
Log.e(TAG, "Could not get BluetoothCharacteristic for address=" + gatt.getDevice().getAddress() + " serviceUUID=" + serviceMeasurement.serviceUUID() + " characteristicUUID=" + serviceMeasurement.measurementUUID()); Log.e(TAG, gatt.getDevice() + ": could not get BluetoothCharacteristic for serviceUUID=" + serviceMeasurement.serviceUUID() + " characteristicUUID=" + serviceMeasurement.measurementUUID());
return; return;
} }
gatt.setCharacteristicNotification(characteristic, true); gatt.setCharacteristicNotification(characteristic, true);
@@ -110,13 +109,14 @@ public class BluetoothConnectionManager {
@Override @Override
public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic) { public void onCharacteristicChanged(BluetoothGatt gatt, @NonNull BluetoothGattCharacteristic characteristic) {
UUID serviceUUID = characteristic.getService().getUuid(); UUID serviceUUID = characteristic.getService().getUuid();
Log.d(TAG, "Received data from " + gatt.getDevice().getAddress() + " with service " + serviceUUID + " and characteristics " + characteristic.getUuid()); BluetoothDevice device = gatt.getDevice();
Log.d(TAG, device + ": Received data with service " + serviceUUID + " and characteristics " + characteristic.getUuid());
Optional<ServiceMeasurementUUID> serviceMeasurementUUID = sensorHandler.getServices() Optional<ServiceMeasurementUUID> serviceMeasurementUUID = sensorHandler.getServices()
.stream() .stream()
.filter(s -> s.serviceUUID().equals(characteristic.getService().getUuid())) .filter(s -> s.serviceUUID().equals(characteristic.getService().getUuid()))
.findFirst(); .findFirst();
if (serviceMeasurementUUID.isEmpty()) { if (serviceMeasurementUUID.isEmpty()) {
Log.e(TAG, "Unknown service UUID; not supported?"); Log.e(TAG, device + ": Unknown service UUID; not supported?");
return; return;
} }
@@ -135,7 +135,7 @@ public class BluetoothConnectionManager {
return; return;
} }
Log.d(TAG, "Connecting to: " + device); Log.d(TAG, device + ": trying to connect");
bluetoothGatt = device.connectGatt(context, false, connectCallback, BluetoothDevice.TRANSPORT_AUTO, 0, handler); bluetoothGatt = device.connectGatt(context, false, connectCallback, BluetoothDevice.TRANSPORT_AUTO, 0, handler);
@@ -148,11 +148,13 @@ public class BluetoothConnectionManager {
synchronized void disconnect() { synchronized void disconnect() {
if (bluetoothGatt == null) { if (bluetoothGatt == null) {
Log.w(TAG, "Cannot disconnect if not connected.");
return; return;
} }
Log.i(TAG, bluetoothGatt.getDevice() + ": start disconnect");
bluetoothGatt.disconnect();
bluetoothGatt.close(); bluetoothGatt.close();
clearData(); clearData();
Log.i(TAG, bluetoothGatt.getDevice() + ": disconnect finished");
bluetoothGatt = null; bluetoothGatt = null;
} }
@@ -188,7 +188,7 @@ public final class SensorDataSet {
} }
private void set(@NonNull Aggregator<?, ?> type, @Nullable Aggregator<?, ?> sensorData) { private void set(@NonNull Aggregator<?, ?> type, @Nullable Aggregator<?, ?> sensorData) {
Log.i(TAG, "Setting aggregator " + type.getClass().getCanonicalName()); Log.i(TAG, "Setting aggregator " + type.getClass().getCanonicalName() + " to " + sensorData);
if (type instanceof AggregatorHeartRate) { if (type instanceof AggregatorHeartRate) {
heartRate = (AggregatorHeartRate) sensorData; heartRate = (AggregatorHeartRate) sensorData;
@@ -255,7 +255,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator.
@Override @Override
public void newGpsStatus(GpsStatusValue gpsStatusValue) { public void newGpsStatus(GpsStatusValue gpsStatusValue) {
Log.e(TAG, "newGpsStatus: " + gpsStatusValue.message); Log.i(TAG, "newGpsStatus: " + gpsStatusValue.message);
if (notificationManager == null) { if (notificationManager == null) {