Reputation: 7
I'm developing an application in Java that reads and displays geographic data produced by an instrument (it has a GPS integrated). I would develop the export function in ".fit" format of such data to use them in lots free applications. I read this "ANT +" documentation and the sdk stuff, but i have a problem developing the following code. The file is correctly generated in ".fit" format, but this file is not compatible (no web or desktop application can read this data). it seems as if something was missing. into the SDK although there are some examples in Java but they are not clear. Thank you The code is this:
FileEncoder encode;
try {
encode = new FileEncoder(new java.io.File(file.getAbsolutePath()+".fit"), Fit.ProtocolVersion.V2_0);
} catch (FitRuntimeException e) {
System.err.println("Error opening file " + file.getName()+".fit");
return;
}
//Generate FileIdMessage
FileIdMesg fileIdMesg = new FileIdMesg(); // Every FIT file MUST contain a 'File ID' message as the first message
fileIdMesg.setManufacturer(15);
fileIdMesg.setType(com.garmin.fit.File.ACTIVITY);
fileIdMesg.setProduct(4);
fileIdMesg.setSerialNumber(1701L);
fileIdMesg.setTimeCreated(new DateTime(systemStartTime.getTime()));
fileIdMesg.setNumber(0);
This code is necessary because every ".fit" file need this. as explained in doc.
Then i found the following code for java.
encode.write(fileIdMesg); // Encode the FileIDMesg
byte[] appId = new byte[] {
0x1, 0x1, 0x2, 0x3,
0x5, 0x8, 0xD, 0x15,
0x22, 0x37, 0x59, (byte)0x90,
(byte)0xE9, 0x79, 0x62, (byte)0xDB
};
DeveloperDataIdMesg developerIdMesg = new DeveloperDataIdMesg();
for(int i = 0; i < appId.length; i++)
{
developerIdMesg.setApplicationId(i, appId[i]);
}
developerIdMesg.setDeveloperDataIndex((short)0);
encode.write(developerIdMesg);
FieldDescriptionMesg fieldDescMesg = new FieldDescriptionMesg();
fieldDescMesg.setDeveloperDataIndex((short)0);
fieldDescMesg.setFieldDefinitionNumber((short)0);
fieldDescMesg.setFitBaseTypeId((short)Fit.MAX_FIELD_SIZE);
fieldDescMesg.setFieldName(0, "Bepop2");
fieldDescMesg.setUnits(0, "Bepop22");
encode.write(fieldDescMesg);
RecordMesg record = new RecordMesg();
DeveloperField doughnutsEarnedField = new DeveloperField(fieldDescMesg, developerIdMesg);
record.addDeveloperField(doughnutsEarnedField);
// This is my code added to try to record something.
Date d=new Date();
DateTime d2 =new DateTime(d.getTime());
for (int ii=0;ii<ndatitot-2;ii++){
record.timestampToDateTime((d.getTime()));
record.setTimestamp(d2);
record.setPositionLat(495280430+ii);
record.setPositionLong(-872696681+ii);
record.setHeartRate((short)140);
record.setCadence((short)88);
record.setDistance(2080f);
record.setSpeed(2800f);
doughnutsEarnedField.setValue(ii+1);
encode.write(record);
Upvotes: 0
Views: 1580
Reputation: 71
Reposting my answer from the FIT forums here (https://www.thisisant.com/forum/viewthread/6501/)
Encoding files as Fit.ProtocolVersion.V2_0
as has been done above is a breaking change for the FIT protocol, and the resulting file will not be properly decoded by old (i.e, FIT protocol version 1.0) decoders.
FIT 2.0 only exited its beta stage at the beginning of May, so many applications using the FIT SDK have likely not updated yet.
If the encoder is instead created by specifying Fit.ProtocolVersion.V1_0
then a FIT protocol version 1.0 compatible file will be created (assuming 2.0 features are not used).
Upvotes: 3