From dcd776e09ae4e4639dc01821413d22c96ee58413 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 18 Oct 2015 22:08:52 +0200 Subject: [PATCH] Attempt to fix never finishing activity data fetching #142 (by disabling unrelated notifications) --- CHANGELOG.md | 1 + .../gadgetbridge/service/btle/BtLEQueue.java | 2 +- .../service/btle/TransactionBuilder.java | 5 +++++ .../miband/operations/FetchActivityOperation.java | 12 +++++++++++- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45fe04cf9..4ab9c3e87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Support pull down to synchronize activity data (#138) * Display tabs in the Charts activity (#139) * Mi Band: initial support for Mi Band 1a (the one with white LEDs) (thanks @sarg) (#136) +* Mi Band: Attempt at fixing problem with never finishing activity data fetching (#141, #142) * Register/unregister BroadcastReceivers instead of enabling/disabling them with PackageManager (#134) (should fix disconnection because the service is being killed) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java index 77e90f84a..c286d64e7 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/BtLEQueue.java @@ -108,7 +108,7 @@ public final class BtLEQueue { mConnectionLatch = null; LOG.debug("Thread interrupted"); } catch (Throwable ex) { - LOG.error("Queue Dispatch Thread died: " + ex.getMessage()); + LOG.error("Queue Dispatch Thread died: " + ex.getMessage(), ex); mCrashed = true; mConnectionLatch = null; } finally { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java index 90efe84bc..09d6b1b87 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/btle/TransactionBuilder.java @@ -15,6 +15,7 @@ public class TransactionBuilder { private static final Logger LOG = LoggerFactory.getLogger(TransactionBuilder.class); private Transaction mTransaction; + private boolean mQueued; public TransactionBuilder(String taskName) { mTransaction = new Transaction(taskName); @@ -83,6 +84,10 @@ public class TransactionBuilder { * @param queue */ public void queue(BtLEQueue queue) { + if (mQueued) { + throw new IllegalStateException("This builder had already been queued. You must not reuse it."); + } + mQueued = true; queue.add(mTransaction); } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java index 945d85ab2..fa542aa45 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/miband/operations/FetchActivityOperation.java @@ -124,11 +124,18 @@ public class FetchActivityOperation extends AbstractBTLEOperation public void perform() throws IOException { TransactionBuilder builder = performInitialized("fetch activity data"); // builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_LE_PARAMS), getLowLatency()); + + enableOtherNotifications(builder, false); builder.add(new SetDeviceBusyAction(getDevice(), getContext().getString(R.string.busy_task_fetch_activity_data), getContext())); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), fetch); builder.queue(getQueue()); } + private void enableOtherNotifications(TransactionBuilder builder, boolean enable) { + builder.notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_REALTIME_STEPS), enable) + .notify(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_SENSOR_DATA), enable); + } + @Override public void onCharacteristicChanged(BluetoothGatt gatt, @@ -141,9 +148,12 @@ public class FetchActivityOperation extends AbstractBTLEOperation } } - private void handleActivityFetchFinish() { + private void handleActivityFetchFinish() throws IOException { LOG.info("Fetching activity data has finished."); activityStruct = null; + TransactionBuilder builder = performInitialized("enabling other notifications again"); + enableOtherNotifications(builder, true); + builder.queue(getQueue()); unsetBusy(); }