diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSActivity.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSActivity.java index 792fcbd8..6f34f47e 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSActivity.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSActivity.java @@ -185,12 +185,17 @@ public class CGMSActivity extends BleProfileServiceReadyActivity { public void onCharacteristicNotified(final BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) { Logger.a(mLogSession, "\"" + CGMMeasurementParser.parse(characteristic) + "\" received"); - // CGM Measurement characteristic - final int cgmSize = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0); - if (cgmSize > 0) { - final float cgmValue = characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_SFLOAT, 2); - final int sequenceNumber = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, 4); + // CGM Measurement characteristic may have one or more CGM records + int totalSize = characteristic.getValue().length; + int offset = 0; + while (offset < totalSize) { + final int cgmSize = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, offset); + final float cgmValue = characteristic.getFloatValue(BluetoothGattCharacteristic.FORMAT_SFLOAT, offset + 2); + final int sequenceNumber = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset + 4); final long timestamp = mSessionStartTime + (sequenceNumber * 60000L); // Sequence number is in minutes since Start Session + //This will send callback to CGMSActivity when new concentration value is received from CGMS device final CGMSRecord cgmsRecord = new CGMSRecord(sequenceNumber, cgmValue, timestamp); mRecords.put(cgmsRecord.sequenceNumber, cgmsRecord); mCallbacks.onCGMValueReceived(gatt.getDevice(), cgmsRecord); + + offset += cgmSize; } } diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSRecordsAdapter.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSRecordsAdapter.java index 6b7feecf..9ecbbbc6 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSRecordsAdapter.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/cgms/CGMSRecordsAdapter.java @@ -19,7 +19,7 @@ import no.nordicsemi.android.nrftoolbox.R; * Created by rora on 02.09.2016. */ public class CGMSRecordsAdapter extends BaseAdapter { - private final static SimpleDateFormat mTimeFormat = new SimpleDateFormat("dd.MM.YYYY HH:mm:ss", Locale.US); + private final static SimpleDateFormat mTimeFormat = new SimpleDateFormat("dd.MM.YYYY HH:mm", Locale.US); private List mRecords; private LayoutInflater mInflater; diff --git a/app/src/main/java/no/nordicsemi/android/nrftoolbox/parser/CGMMeasurementParser.java b/app/src/main/java/no/nordicsemi/android/nrftoolbox/parser/CGMMeasurementParser.java index e091447f..830d3ef5 100644 --- a/app/src/main/java/no/nordicsemi/android/nrftoolbox/parser/CGMMeasurementParser.java +++ b/app/src/main/java/no/nordicsemi/android/nrftoolbox/parser/CGMMeasurementParser.java @@ -23,8 +23,6 @@ package no.nordicsemi.android.nrftoolbox.parser; import android.bluetooth.BluetoothGattCharacteristic; -import java.util.ArrayList; -import java.util.List; import java.util.Locale; public class CGMMeasurementParser { @@ -58,7 +56,20 @@ public class CGMMeasurementParser { private static final int SSA_RESULT_HIGHER_THAN_DEVICE_CAN_PROCESS = 1 << 23; public static String parse(final BluetoothGattCharacteristic characteristic) { + // The CGM Measurement characteristic is a variable length structure containing one or more CGM Measurement records + int totalSize = characteristic.getValue().length; + + final StringBuilder builder = new StringBuilder(); int offset = 0; + while (offset < totalSize) { + offset += parseRecord(builder, characteristic, offset); + if (offset < totalSize) + builder.append("\n\n"); + } + return builder.toString(); + } + + private static int parseRecord(final StringBuilder builder, final BluetoothGattCharacteristic characteristic, int offset) { // Read size and flags bytes final int size = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, offset++); final int flags = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, offset++); @@ -101,9 +112,8 @@ public class CGMMeasurementParser { final int timeOffset = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT16, offset); offset += 2; - final StringBuilder builder = new StringBuilder(); builder.append("Glucose concentration: ").append(glucoseConcentration).append(" mg/dL\n"); - builder.append("Sequence number: ").append(timeOffset).append(" (minutes since start)\n"); + builder.append("Sequence number: ").append(timeOffset).append(" (Time Offset in min)\n"); if (ssaWarningOctetPresent) { final int ssaWarningOctet = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, offset++); @@ -178,6 +188,6 @@ public class CGMMeasurementParser { builder.append(String.format(Locale.US, "E2E-CRC: 0x%04X\n", crc)); } builder.setLength(builder.length() - 1); // Remove last \n - return builder.toString(); + return size; } }