How to add custom filed to Workout

I have try to create java program which should add one extra Field to Workout message

Field 16 is the only one which makes difference between Strength, Joga or Pilates workouts created directly on Garmin Connect and after edit preset ones.

I have started with this information:

Read and write back fit file adds extra bytes - Discussion - FIT SDK - Garmin Forums

My code looks like this:

@Override
public void onMesg(Mesg mesg) {

Short[] values16 = new Short[16];
Arrays.fill(values16, (short) 0); // Fill with 0
values16[15] = 1; // Set last element to 1

if(mesg.getNum() == MesgNum.WORKOUT) {
  System.out.println("Workout:");
  if(mesg.hasField(16)) {
    // encoder.write(mesg); // or encoder.write(recordMesg) should not matter.
    System.out.println(" have Field #16");
  } else {
    Field field = Factory.createField(mesg.getNum(), 16);
    mesg.addField(field)
    mesg.setFieldValue(16, 0, values16, Fit.SUBFIELD_INDEX_MAIN_FIELD);
    // encoder.write(workoutMesg);
    System.out.println(" do not have Field #16");
  }
}
}

I guess due to missing definition of Field 16 in WorkoutMesg my new field has num 255 (INVALID) instead expected 16

And one more thing instead of Array of Shorts I get in that Field 255 simple Short value.

Can someone help me, what would be the best way to implement this?

Thanks

  • Never mind. I have managed to sort it out.

    My solution was to create two additional classes to create my Custom Mesg and Custom Field.

    There is one more tiny thing which doesn't make problems at this moment.

    My program suppose to copy whole input Mesg as is without doing anything except if it is Workout Type, then add that Field #16 if it not present.

    But in the output file in the workout_step message secondary_target_type field is missing. I am doing nothing else just copy whole Mesg from input to output, but that field is removed. Do not know why. Maybe I will chase it.