Shuffle some logging things around

This commit is contained in:
cpfeiffer 2016-10-20 21:42:36 +02:00
parent c87d08bf4b
commit 839da4f06a
5 changed files with 37 additions and 37 deletions

View File

@ -125,4 +125,12 @@ public abstract class Logging {
} }
return false; return false;
} }
public static void logBytes(Logger logger, byte[] value) {
if (value != null) {
for (byte b : value) {
logger.warn("DATA: " + String.format("0x%2x", b));
}
}
}
} }

View File

@ -6,7 +6,6 @@ import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService; import android.bluetooth.BluetoothGattService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -16,6 +15,7 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.Logging;
import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport; import nodomain.freeyourgadget.gadgetbridge.service.AbstractDeviceSupport;
import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.CheckInitializedAction; import nodomain.freeyourgadget.gadgetbridge.service.btle.actions.CheckInitializedAction;
import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.AbstractBleProfile; import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.AbstractBleProfile;
@ -32,15 +32,21 @@ import nodomain.freeyourgadget.gadgetbridge.service.btle.profiles.AbstractBlePro
* @see BtLEQueue * @see BtLEQueue
*/ */
public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport implements GattCallback { public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport implements GattCallback {
private static final Logger LOG = LoggerFactory.getLogger(AbstractBTLEDeviceSupport.class);
private BtLEQueue mQueue; private BtLEQueue mQueue;
private HashMap<UUID, BluetoothGattCharacteristic> mAvailableCharacteristics; private HashMap<UUID, BluetoothGattCharacteristic> mAvailableCharacteristics;
private final Set<UUID> mSupportedServices = new HashSet<>(4); private final Set<UUID> mSupportedServices = new HashSet<>(4);
private final List<AbstractBleProfile<?>> mSupportedProfiles = new ArrayList<>(); private Logger logger;
private final List<AbstractBleProfile<?>> mSupportedProfiles = new ArrayList<>();
public static final String BASE_UUID = "0000%s-0000-1000-8000-00805f9b34fb"; //this is common for all BTLE devices. see http://stackoverflow.com/questions/18699251/finding-out-android-bluetooth-le-gatt-profiles public static final String BASE_UUID = "0000%s-0000-1000-8000-00805f9b34fb"; //this is common for all BTLE devices. see http://stackoverflow.com/questions/18699251/finding-out-android-bluetooth-le-gatt-profiles
public AbstractBTLEDeviceSupport(Logger logger) {
this.logger = logger;
if (logger == null) {
throw new IllegalArgumentException("logger must not be null");
}
}
@Override @Override
public boolean connect() { public boolean connect() {
if (mQueue == null) { if (mQueue == null) {
@ -168,27 +174,27 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
private void gattServicesDiscovered(List<BluetoothGattService> discoveredGattServices) { private void gattServicesDiscovered(List<BluetoothGattService> discoveredGattServices) {
if (discoveredGattServices == null) { if (discoveredGattServices == null) {
LOG.warn("No gatt services discovered: null!"); logger.warn("No gatt services discovered: null!");
return; return;
} }
Set<UUID> supportedServices = getSupportedServices(); Set<UUID> supportedServices = getSupportedServices();
mAvailableCharacteristics = new HashMap<>(); mAvailableCharacteristics = new HashMap<>();
for (BluetoothGattService service : discoveredGattServices) { for (BluetoothGattService service : discoveredGattServices) {
if (supportedServices.contains(service.getUuid())) { if (supportedServices.contains(service.getUuid())) {
LOG.debug("discovered supported service: " + BleNamesResolver.resolveServiceName(service.getUuid().toString()) + ": " + service.getUuid()); logger.debug("discovered supported service: " + BleNamesResolver.resolveServiceName(service.getUuid().toString()) + ": " + service.getUuid());
List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics(); List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
if (characteristics == null || characteristics.isEmpty()) { if (characteristics == null || characteristics.isEmpty()) {
LOG.warn("Supported LE service " + service.getUuid() + "did not return any characteristics"); logger.warn("Supported LE service " + service.getUuid() + "did not return any characteristics");
continue; continue;
} }
HashMap<UUID, BluetoothGattCharacteristic> intmAvailableCharacteristics = new HashMap<>(characteristics.size()); HashMap<UUID, BluetoothGattCharacteristic> intmAvailableCharacteristics = new HashMap<>(characteristics.size());
for (BluetoothGattCharacteristic characteristic : characteristics) { for (BluetoothGattCharacteristic characteristic : characteristics) {
intmAvailableCharacteristics.put(characteristic.getUuid(), characteristic); intmAvailableCharacteristics.put(characteristic.getUuid(), characteristic);
LOG.info(" characteristic: " + BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString()) + ": " + characteristic.getUuid()); logger.info(" characteristic: " + BleNamesResolver.resolveCharacteristicName(characteristic.getUuid().toString()) + ": " + characteristic.getUuid());
} }
mAvailableCharacteristics.putAll(intmAvailableCharacteristics); mAvailableCharacteristics.putAll(intmAvailableCharacteristics);
} else { } else {
LOG.debug("discovered unsupported service: " + BleNamesResolver.resolveServiceName(service.getUuid().toString()) + ": " + service.getUuid()); logger.debug("discovered unsupported service: " + BleNamesResolver.resolveServiceName(service.getUuid().toString()) + ": " + service.getUuid());
} }
} }
} }
@ -197,6 +203,16 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
return mSupportedServices; return mSupportedServices;
} }
/**
* Utility method that may be used to log incoming messages when we don't know how to deal with them yet.
*
* @param value
*/
public void logMessageContent(byte[] value) {
logger.info("RECEIVED DATA WITH LENGTH: " + ((value != null) ? value.length : "(null)"));
Logging.logBytes(logger, value);
}
// default implementations of event handler methods (gatt callbacks) // default implementations of event handler methods (gatt callbacks)
@Override @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

View File

@ -106,6 +106,7 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo(); private final GBDeviceEventBatteryInfo batteryCmd = new GBDeviceEventBatteryInfo();
public MiBand2Support() { public MiBand2Support() {
super(LOG);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS); addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE); addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE);
addSupportedService(GattService.UUID_SERVICE_HEART_RATE); addSupportedService(GattService.UUID_SERVICE_HEART_RATE);
@ -917,20 +918,6 @@ public class MiBand2Support extends AbstractBTLEDeviceSupport {
return false; return false;
} }
/**
* Utility method that may be used to log incoming messages when we don't know how to deal with them yet.
*
* @param value
*/
public void logMessageContent(byte[] value) {
LOG.info("RECEIVED DATA WITH LENGTH: " + ((value != null) ? value.length : "(null)"));
if (value != null) {
for (byte b : value) {
LOG.warn("DATA: " + String.format("0x%2x", b));
}
}
}
public void logDate(byte[] value, int status) { public void logDate(byte[] value, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value); GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value);

View File

@ -20,6 +20,7 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.Logging;
import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
@ -103,6 +104,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
private RealtimeSamplesSupport realtimeSamplesSupport; private RealtimeSamplesSupport realtimeSamplesSupport;
public MiBandSupport() { public MiBandSupport() {
super(LOG);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS); addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE); addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE);
addSupportedService(MiBandService.UUID_SERVICE_MIBAND_SERVICE); addSupportedService(MiBandService.UUID_SERVICE_MIBAND_SERVICE);
@ -873,20 +875,6 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport {
return false; return false;
} }
/**
* Utility method that may be used to log incoming messages when we don't know how to deal with them yet.
*
* @param value
*/
public void logMessageContent(byte[] value) {
LOG.info("RECEIVED DATA WITH LENGTH: " + ((value != null) ? value.length : "(null)"));
if (value != null) {
for (byte b : value) {
LOG.warn("DATA: " + String.format("0x%2x", b));
}
}
}
public void logDate(byte[] value, int status) { public void logDate(byte[] value, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { if (status == BluetoothGatt.GATT_SUCCESS) {
GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value); GregorianCalendar calendar = MiBandDateConverter.rawBytesToCalendar(value);

View File

@ -53,6 +53,7 @@ public class VibratissimoSupport extends AbstractBTLEDeviceSupport {
}; };
public VibratissimoSupport() { public VibratissimoSupport() {
super(LOG);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS); addSupportedService(GattService.UUID_SERVICE_GENERIC_ACCESS);
addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE); addSupportedService(GattService.UUID_SERVICE_GENERIC_ATTRIBUTE);
addSupportedService(GattService.UUID_SERVICE_DEVICE_INFORMATION); addSupportedService(GattService.UUID_SERVICE_DEVICE_INFORMATION);