Welcome Guest,Register Now
Log In

ANT Forum

Welcome guest, please Login or Register

   

Java. Not working .fit file generated with Java library

Rank

Total Posts: 6

Joined 2016-05-29

PM

Hi. 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);
encode.write(fileIdMesg); // Encode the FileIDMesg 


Then i found in Ant documentation this code... but i cannot undertand

byte[] appId = new byte[] {
     0x1
0x10x20x3,
     
0x50x80xD0x15,
     
0x220x370x59, (byte)0x90,
     (
byte)0xE90x790x62, (byte)0xDB
  }
;

  
DeveloperDataIdMesg developerIdMesg = new DeveloperDataIdMesg();
  for(
int i 0appId.lengthi++)
  
{
     developerIdMesg
.setApplicationId(iappId[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"Myinstrument");
  
fieldDescMesg.setUnits(0"Myinstrument");
  
encode.write(fieldDescMesg);

  
RecordMesg record = new RecordMesg();
  
DeveloperField doughnutsEarnedField = new DeveloperField(fieldDescMesgdeveloperIdMesg);
  
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); 


HELP ME PLEASE......

Thx!!!!!!
     
RankRankRank

Total Posts: 68

Joined 0

PM

The Base type you are using for your custom field is not valid
fieldDescMesg.setFitBaseTypeId((short)Fit.MAX_FIELD_SIZE)); 

The base type defines the number of bytes that are used in the file to record your data. And should be taken from
public class FitBaseType {
   
public static final short ENUM 0;
   public static final 
short SINT8 1;
   public static final 
short UINT8 2;
   public static final 
short SINT16 131;
   public static final 
short UINT16 132;
   public static final 
short SINT32 133;
   public static final 
short UINT32 134;
   public static final 
short STRING 7;
   public static final 
short FLOAT32 136;
   public static final 
short FLOAT64 137;
   public static final 
short UINT8Z 10;
   public static final 
short UINT16Z 139;
   public static final 
short UINT32Z 140;
   public static final 
short BYTE 13;
   public static final 
short INVALID Fit.UINT8_INVALID;