From 716bbc7b787cd16978fea7d9370b430ce2cb26f8 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sat, 25 Jul 2015 00:08:15 +0200 Subject: [PATCH] Avoid NPE when fetching activity data: java.lang.NullPointerException: Attempt to write to field 'int nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport$ActivityStruct.activityDataHolderProgress' on a null object reference at nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport.flushActivityDataHolder(MiBandSupport.java:748) at nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport.handleActivityNotif(MiBandSupport.java:689) at nodomain.freeyourgadget.gadgetbridge.miband.MiBandSupport.onCharacteristicChanged(MiBandSupport.java:583) at nodomain.freeyourgadget.gadgetbridge.btle.BtLEQueue$2.onCharacteristicChanged(BtLEQueue.java:369) --- .../gadgetbridge/miband/MiBandSupport.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java index 33da795d0..6d2b49fa0 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/miband/MiBandSupport.java @@ -672,7 +672,6 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { if (activityStruct.activityDataRemainingBytes == 0) { sendAckDataTransfer(activityStruct.activityDataTimestampToAck, activityStruct.activityDataUntilNextHeader); - flushActivityDataHolder(); } } @@ -712,6 +711,11 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { } private void flushActivityDataHolder() { + if (activityStruct == null) { + LOG.debug("nothing to flush, struct is already null"); + return; + } + LOG.debug("flushing activity data holder"); byte category, intensity, steps; ActivityDatabaseHandler dbHandler = GBApplication.getActivityDatabaseHandler(); @@ -770,6 +774,10 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { TransactionBuilder builder = performInitialized("send acknowledge"); builder.write(getCharacteristic(MiBandService.UUID_CHARACTERISTIC_CONTROL_POINT), ack); builder.queue(getQueue()); + + // flush to the DB after sending the ACK + flushActivityDataHolder(); + //The last data chunk sent by the miband has always length 0. //When we ack this chunk, the transfer is done. if(getDevice().isBusy() && bytesTransferred==0) { @@ -781,6 +789,7 @@ public class MiBandSupport extends AbstractBTLEDeviceSupport { } private void handleActivityFetchFinish() { + LOG.info("Fetching activity data has finished."); activityStruct = null; unsetBusy(); }