public class AlternativeCarrierRecordEncoder implements WellKnownRecordPayloadEncoder {
@Override
public byte[] encodePayload(WellKnownRecord wellKnownRecord, NdefMessageEncoder messageEncoder) {
AlternativeCarrierRecord alternativeCarrierRecord = (AlternativeCarrierRecord)wellKnownRecord;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
// cps
CarrierPowerState carrierPowerState = alternativeCarrierRecord.getCarrierPowerState();
if (carrierPowerState == null) {
throw new NdefEncoderException("Expected carrier power state", alternativeCarrierRecord);
}
bout.write(carrierPowerState.getValue() & 0x7); // 3 lsb
// carrier data reference: 1
String carrierDataReference = alternativeCarrierRecord.getCarrierDataReference();
if (carrierDataReference == null) {
throw new NdefEncoderException("Expected carrier data reference", alternativeCarrierRecord);
}
byte[] carrierDataReferenceChar = carrierDataReference.getBytes(NdefConstants.DEFAULT_CHARSET);
if (carrierDataReferenceChar.length > 255) {
throw new NdefEncoderException("Expected carrier data reference '" + carrierDataReference
+ "' <= 255 bytes", alternativeCarrierRecord);
}
// carrier data reference length (1)
bout.write(carrierDataReferenceChar.length);
// carrier data reference char
bout.write(carrierDataReferenceChar, 0, carrierDataReferenceChar.length);
// auxiliary data
List<String> auxiliaryDataReferences = alternativeCarrierRecord.getAuxiliaryDataReferences();
// auxiliary data reference count
bout.write(auxiliaryDataReferences.size());
for (String auxiliaryDataReference : auxiliaryDataReferences) {