writeNdefMessage(card, ndefData);
}
private void writeNdefMessage(MfCard card, byte[] ndefData) throws IOException, MfException {
ApplicationDirectory applicationDirectory = null;
if (MadUtils.hasApplicationDirectory(card, readerWriter))
applicationDirectory = MadUtils.getApplicationDirectory(card, readerWriter, writeKeyValue);
else
applicationDirectory = MadUtils.createApplicationDirectory(card, readerWriter, createKey, createKeyValue,
writeKeyValue);
TrailerBlock trailerBlock = createNdefTrailerBlock();
if (applicationDirectory.hasApplication(MfNdefConstants.NDEF_APP_ID)) {
applicationDirectory.deleteApplication(MfNdefConstants.NDEF_APP_ID, deleteKeyValue, new TrailerBlock());
}
byte[] tlvWrappedNdefData = wrapNdefMessageWithTlv(ndefData, applicationDirectory.getMaxContinousSize());
/*
* The specs states that the TLV terminator can be left out if the message ends with the available space.
* One might want to check if the available size on the card is one byte less than the tlv wrapped message. If
* it is the case the TLV terminator could be removed.
*/
Application application = applicationDirectory.createApplication(MfNdefConstants.NDEF_APP_ID,
tlvWrappedNdefData.length, writeKeyValue, trailerBlock);
if (log.isDebugEnabled())
log.debug("Length: " + tlvWrappedNdefData.length + " [" + NfcUtils.convertBinToASCII(tlvWrappedNdefData)
+ "]");