@Override
public List<Record> readNdefMessage(MfCard card) throws IOException {
if (!MadUtils.hasApplicationDirectory(card, readerWriter))
throw new NdefException("unknown service type");
ApplicationDirectory applicationDirectory = MadUtils.getApplicationDirectory(card, readerWriter);
if (applicationDirectory.hasApplication(MfNdefConstants.NDEF_APP_ID)) {
Application application = applicationDirectory.openApplication(MfNdefConstants.NDEF_APP_ID);
byte[] tlvWrappedNdefMessage = application.read(new KeyValue(Key.A, MfConstants.NDEF_KEY));
if (log.isDebugEnabled())
log.debug(NfcUtils.convertBinToASCII(tlvWrappedNdefMessage));
TypeLengthValueReader lengthValueReader = new TypeLengthValueReader(new ByteArrayInputStream(
tlvWrappedNdefMessage));
List<Record> records = new ArrayList<Record>();
while (lengthValueReader.hasNext()) {
Tlv tlv = lengthValueReader.next();
if (tlv instanceof NdefMessageTlv) {
NdefMessage ndefMessage = ndefMessageDecoder.decode(((NdefMessageTlv)tlv).getNdefMessage());
for (Record record : ndefMessageDecoder.decodeToRecords(ndefMessage)) {
records.add(record);
}
}
}
return records;
}
else {
throw new NdefException("unknown service type");
}
}