问题
Need help to parse mealtype on glucometer android BLE.
https://github.com/oesmith/gatt-xml/blob/master/org.bluetooth.characteristic.glucose_measurement_context.xml
Here's my data: [27, 5, 0, -28, 7, 8, 24, 17, 18, 41, -29, 1, 102, -80, -8, 0, 0]
I also found this one: C3: Field exists if the key of bit 1 of the Flags field is set to 1
Here's my enum
public static Meal from(final int code) {
switch (code) {
case 1:
return PREPRANDIAL;
case 2:
return POSTPRANDIAL;
case 3:
return FASTING;
case 4:
return CASUAL;
case 5:
return BEDTIME;
default:
return RESERVED;
}
}
Here's my code but I got a mealtype value greather that 20 instead of less than 6
public final ByteBuffer data;
public final byte flags;
public byte secondaryFlags;
public byte carbFlags;
public int carbInfo;
public int mealType = -1;
public final int sequence;
public final boolean hasSecondaryFlags;
public final boolean hasMealType;
public final boolean hasCarbInfo;
public ContextBG(byte[] packet) {
data = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN);
flags = data.get();
hasSecondaryFlags = (flags & 128) > 0;
hasMealType = (flags & 2) > 0;
hasCarbInfo = (flags & 1) > 0;
sequence = data.getShort();
if (hasSecondaryFlags) {
secondaryFlags = data.get();
}
if (hasCarbInfo) {
carbFlags = data.get();
carbInfo = data.getShort();
}
if (hasMealType) {
mealType = data.get();
Log.d("MEAL TYPE", "======= " + mealType);
}
}
回答1:
The data you have shared is 17 bytes long.
I've used this xml to get the fields.
The first byte is flags
and in your data that is decimal 27 which is binary 00011011
So the flags are set as follows:
<Bit index="0" size="1" name="Carbohydrate ID And Carbohydrate Present"> = True
<Bit index="1" size="1" name="Meal Present"> = True
<Bit index="2" size="1" name="Tester-Health Present"> = False
<Bit index="3" size="1" name="Exercise Duration And Exercise Intensity Present"> = True
<Bit index="4" size="1" name="Medication ID And Medication Present"> = True
<Bit index="5" size="1" name="Medication Value Units"> = kilograms
<Bit index="6" size="1" name="HbA1c Present"> = False
<Bit index="7" size="1" name="Extended Flags Present"> = False
I make this that the data should be:
<Field name="Flags"> = 1 byte
<Field name="Sequence Number"> = 2 bytes
<Field name="Extended Flags "> = 1 byte
<Field name="Carbohydrate ID"> = 1 byte
<Field name="Carbohydrate - units of kilograms"> = 2 bytes
<Field name="Meal"> = 1 byte
<Field name="Exercise Duration"> = 2 bytes
<Field name="Exercise Intensity"> = 1 byte
<Field name="Medication ID"> = 1 byte
<Field name="Medication - units of kilograms"> = 2 bytes
<Field name="Medication - units of liters"> = 2 bytes
Which is 16 bytes long whereas the example data is 17 bytes long. In addition none of the values look like they would make sensible values for the Meal
field.
The org.bluetooth.characteristic.glucose_measurement
[0x2A18] is 17 bytes long. Could it be that you have mixed the two characteristics up?
来源:https://stackoverflow.com/questions/63643666/need-help-to-parse-mealtype-on-glucometer-android-ble