Bug fixed in CGMS: A CGM Measurement packet may have more than one CGM record

This is in case CGM reords were requested using Record Acces Control
Point.
This commit is contained in:
Aleksander Nowakowski
2016-10-06 11:47:25 +02:00
parent f055bfbd0c
commit 0d898e0557
4 changed files with 33 additions and 13 deletions

View File

@@ -185,12 +185,17 @@ public class CGMSActivity extends BleProfileServiceReadyActivity<CGMService.CGMS
public void onDeviceDisconnected(final BluetoothDevice device) {
super.onDeviceDisconnected(device);
setOperationInProgress(false);
clearRecords();
}
@Override
public void onError(final BluetoothDevice device, final String message, final int errorCode) {
super.onError(device, message, errorCode);
setOperationInProgress(false);
}
@Override
protected void setDefaultUI() {
// do nothing
clearRecords();
}
@Override

View File

@@ -184,16 +184,21 @@ public class CGMSManager extends BleManager<CGMSManagerCallbacks> {
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;
}
}

View File

@@ -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<CGMSRecord> mRecords;
private LayoutInflater mInflater;

View File

@@ -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;
}
}