diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java index 3b9e847ab..7d9761961 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/AbstractBTLEOperation.java @@ -43,6 +43,7 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.miband.operations.Op public abstract class AbstractBTLEOperation implements GattCallback, BTLEOperation { private final T mSupport; protected OperationStatus operationStatus = OperationStatus.INITIAL; + private String name; protected AbstractBTLEOperation(T support) { mSupport = support; @@ -115,6 +116,22 @@ public abstract class AbstractBTLEOperation return mSupport.getDevice(); } + protected void setName(String name) { + this.name = name; + } + + @Override + public String getName() { + if (name != null) { + return name; + } + String busyTask = getDevice().getBusyTask(); + if (busyTask != null) { + return busyTask; + } + return getClass().getSimpleName(); + } + protected BluetoothGattCharacteristic getCharacteristic(UUID uuid) { return mSupport.getCharacteristic(uuid); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BTLEOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BTLEOperation.java index b6efc50c3..867e9c516 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BTLEOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BTLEOperation.java @@ -20,4 +20,9 @@ import java.io.IOException; public interface BTLEOperation { void perform() throws IOException; + + /** + * Returns a human readable name of this operation, to be used e.g. in log output. + */ + String getName(); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/operations/AmazfitBipFetchLogsOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/operations/AmazfitBipFetchLogsOperation.java index dd47022d4..2c7e3fed2 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/operations/AmazfitBipFetchLogsOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/amazfitbip/operations/AmazfitBipFetchLogsOperation.java @@ -26,7 +26,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; -import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -50,6 +49,7 @@ public class AmazfitBipFetchLogsOperation extends AbstractFetchOperation { public AmazfitBipFetchLogsOperation(AmazfitBipSupport support) { super(support); + setName("fetch logs"); } @Override @@ -90,7 +90,7 @@ public class AmazfitBipFetchLogsOperation extends AbstractFetchOperation { @Override protected void handleActivityFetchFinish(boolean success) { - LOG.info("Fetching log data has finished"); + LOG.info(getName() +" data has finished"); try { logOutputStream.close(); logOutputStream = null; @@ -113,7 +113,7 @@ public class AmazfitBipFetchLogsOperation extends AbstractFetchOperation { lastPacketCounter++; bufferActivityData(value); } else { - GB.toast("Error fetching activity data, invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR); + GB.toast("Error " + getName() + " invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR); handleActivityFetchFinish(false); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/AbstractFetchOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/AbstractFetchOperation.java index 2e399e1e6..9b28c449e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/AbstractFetchOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/AbstractFetchOperation.java @@ -57,6 +57,7 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation { protected BluetoothGattCharacteristic characteristicActivityData; protected BluetoothGattCharacteristic characteristicFetch; protected Calendar startTimestamp; + protected int expectedDataLength; public AbstractFetchOperation(MiBand2Support support) { super(support); @@ -66,7 +67,8 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation { protected void enableNeededNotifications(TransactionBuilder builder, boolean enable) { if (!enable) { // dynamically enabled, but always disabled on finish - builder.notify(getCharacteristic(MiBand2Service.UUID_CHARACTERISTIC_5_ACTIVITY_DATA), enable); + builder.notify(characteristicFetch, enable); + builder.notify(characteristicActivityData, enable); } } @@ -78,7 +80,7 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation { protected void startFetching() throws IOException { lastPacketCounter = -1; - TransactionBuilder builder = performInitialized("fetching activity data"); + TransactionBuilder builder = performInitialized(getName()); getSupport().setLowLatency(builder); if (fetchCount == 0) { builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext())); @@ -139,7 +141,8 @@ public abstract class AbstractFetchOperation extends AbstractMiBand2Operation { // first two bytes are whether our request was accepted if (ArrayUtils.equals(value, MiBand2Service.RESPONSE_ACTIVITY_DATA_START_DATE_SUCCESS, 0)) { // the third byte (0x01 on success) = ? - // the 4th - 7th bytes probably somehow represent the number of bytes/packets to expect + // the 4th - 7th bytes epresent the number of bytes/packets to expect, excluding the counter bytes + expectedDataLength = BLETypeConversions.toUint32(Arrays.copyOfRange(value, 3, 7)); // last 8 bytes are the start date Calendar startTimestamp = getSupport().fromTimeBytes(Arrays.copyOfRange(value, 7, value.length)); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java index 8f9203012..91c008b51 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchActivityOperation.java @@ -57,6 +57,7 @@ public class FetchActivityOperation extends AbstractFetchOperation { public FetchActivityOperation(MiBand2Support support) { super(support); + setName("fetching activity data"); } @Override @@ -75,14 +76,14 @@ public class FetchActivityOperation extends AbstractFetchOperation { } protected void handleActivityFetchFinish(boolean success) { - LOG.info("Fetching activity data has finished round " + fetchCount); + LOG.info(getName() + " has finished round " + fetchCount); GregorianCalendar lastSyncTimestamp = saveSamples(); if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) { try { startFetching(); return; } catch (IOException ex) { - LOG.error("Error starting another round of fetching activity data", ex); + LOG.error("Error starting another round of " + getName(), ex); } } @@ -91,7 +92,7 @@ public class FetchActivityOperation extends AbstractFetchOperation { private boolean needsAnotherFetch(GregorianCalendar lastSyncTimestamp) { if (fetchCount > 5) { - LOG.warn("Already jave 5 fetch rounds, not doing another one."); + LOG.warn("Already have 5 fetch rounds, not doing another one."); return false; } @@ -166,12 +167,12 @@ public class FetchActivityOperation extends AbstractFetchOperation { lastPacketCounter++; bufferActivityData(value); } else { - GB.toast("Error fetching activity data, invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR); + GB.toast("Error " + getName() + ", invalid package counter: " + value[0], Toast.LENGTH_LONG, GB.ERROR); handleActivityFetchFinish(false); return; } } else { - GB.toast("Error fetching activity data, unexpected package length: " + value.length, Toast.LENGTH_LONG, GB.ERROR); + GB.toast("Error " + getName() + ", unexpected package length: " + value.length, Toast.LENGTH_LONG, GB.ERROR); handleActivityFetchFinish(false); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsDetailsOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsDetailsOperation.java index 9ea748525..259951212 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsDetailsOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsDetailsOperation.java @@ -56,11 +56,13 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation { public FetchSportsDetailsOperation(BaseActivitySummary summary, MiBand2Support support) { super(support); + setName("fetching sport details"); this.summary = summary; } @Override protected void startFetching(TransactionBuilder builder) { + LOG.info("start " + getName()); buffer = new ByteArrayOutputStream(1024); GregorianCalendar sinceWhen = getLastSuccessfulSyncTime(); builder.write(characteristicFetch, BLETypeConversions.join(new byte[] { @@ -74,7 +76,7 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation { @Override protected void handleActivityFetchFinish(boolean success) { - LOG.info("Fetching activity data has finished round " + fetchCount); + LOG.info(getName() + " has finished round " + fetchCount); // GregorianCalendar lastSyncTimestamp = saveSamples(); // if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) { // try { @@ -126,16 +128,16 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation { */ @Override protected void handleActivityNotif(byte[] value) { - LOG.warn("sports data: " + Logging.formatBytes(value)); + LOG.warn("sports details: " + Logging.formatBytes(value)); if (!isOperationRunning()) { - LOG.error("ignoring activity data notification because operation is not running. Data length: " + value.length); + LOG.error("ignoring sports details notification because operation is not running. Data length: " + value.length); getSupport().logMessageContent(value); return; } if (value.length < 2) { - LOG.error("unexpected sports summary data length: " + value.length); + LOG.error("unexpected sports details data length: " + value.length); getSupport().logMessageContent(value); return; } @@ -144,7 +146,7 @@ public class FetchSportsDetailsOperation extends AbstractFetchOperation { lastPacketCounter++; bufferActivityData(value); } else { - GB.toast("Error fetching activity data, invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR); + GB.toast("Error " + getName() + ", invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR); handleActivityFetchFinish(false); return; } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsSummaryOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsSummaryOperation.java index dea945620..d6c4b8a00 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsSummaryOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband2/operations/FetchSportsSummaryOperation.java @@ -38,7 +38,6 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; import nodomain.freeyourgadget.gadgetbridge.devices.huami.amazfitbip.AmazfitBipService; import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBand2Service; -import nodomain.freeyourgadget.gadgetbridge.devices.miband2.MiBand2Const; import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; import nodomain.freeyourgadget.gadgetbridge.entities.Device; @@ -60,14 +59,14 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation { private ByteArrayOutputStream buffer = new ByteArrayOutputStream(140); -// private List samples = new ArrayList<>(60*24); // 1day per default - public FetchSportsSummaryOperation(MiBand2Support support) { super(support); + setName("fetching sport summaries"); } @Override protected void startFetching(TransactionBuilder builder) { + LOG.info("start" + getName()); GregorianCalendar sinceWhen = getLastSuccessfulSyncTime(); builder.write(characteristicFetch, BLETypeConversions.join(new byte[] { MiBand2Service.COMMAND_ACTIVITY_DATA_START_DATE, @@ -80,7 +79,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation { @Override protected void handleActivityFetchFinish(boolean success) { - LOG.info("Fetching activity data has finished round " + fetchCount); + LOG.info(getName() + " has finished round " + fetchCount); // GregorianCalendar lastSyncTimestamp = saveSamples(); // if (lastSyncTimestamp != null && needsAnotherFetch(lastSyncTimestamp)) { @@ -137,7 +136,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation { */ @Override protected void handleActivityNotif(byte[] value) { - LOG.warn("sports data: " + Logging.formatBytes(value)); + LOG.warn("sports summary data: " + Logging.formatBytes(value)); if (!isOperationRunning()) { LOG.error("ignoring activity data notification because operation is not running. Data length: " + value.length); @@ -155,7 +154,7 @@ public class FetchSportsSummaryOperation extends AbstractFetchOperation { lastPacketCounter++; bufferActivityData(value); } else { - GB.toast("Error fetching activity data, invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR); + GB.toast("Error " + getName() + ", invalid package counter: " + value[0] + ", last was: " + lastPacketCounter, Toast.LENGTH_LONG, GB.ERROR); handleActivityFetchFinish(false); return; } diff --git a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/Tryout.java b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/Tryout.java index 17a720b69..9aca15ed7 100644 --- a/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/Tryout.java +++ b/app/src/test/java/nodomain/freeyourgadget/gadgetbridge/test/Tryout.java @@ -23,6 +23,11 @@ public class Tryout extends TestBase { byte b = (byte) v; LOG.info("v: " + v); Logging.logBytes(LOG, new byte[] { b }); + + byte[] bs = new byte[] {(byte) 0xf0,0x28,0x00,0x00 }; + LOG.warn("uint32: " + BLETypeConversions.toUint32(bs)); + LOG.warn("uint16: " + BLETypeConversions.toUint16(bs)); + } @Test