void MacAddressHandler::handleNfcHandoverCompletedEvent(nfc_target_t *target)
{
    unsigned int messageCount = 0;
    nfc_get_ndef_message_count(target, &messageCount);

    if (messageCount > 0) {
        nfc_ndef_message_t *ndefMessage = 0;
        nfc_get_ndef_message(target, 0, &ndefMessage);
        nfc_ndef_record_t *record = 0;
        nfc_get_ndef_record(ndefMessage, 0, &record);

        // The MAC address is in little-endian order
        char *macAddress = 0;

        nfc_get_handover_bluetooth_mac_address(record, &macAddress);
        m_macAddress.sprintf("%02x:%02x:%02x:%02x:%02x:%02x", (unsigned int) macAddress[5],
                                (unsigned int) macAddress[4], (unsigned int) macAddress[3],
                                (unsigned int) macAddress[2], (unsigned int) macAddress[1],
                                (unsigned int) macAddress[0]);
        emit macAddressChanged();
        qDebug() << "[INFO] MAC ADDRESS: " << m_macAddress;
    } else {
        qWarning() << "[ERRO] No NdefMessage's found";
    }
}
void NfcWorker::parseNdefMessage(nfc_ndef_message_t *ndefMessage) {

	int ndefMsgCount = 0;

	ndefMsgCount = 1;

	for (int ndefMsgIndex = 0; ndefMsgIndex < ndefMsgCount; ++ndefMsgIndex) {

		qDebug() << "XXXX Processing NDEF Message index: " << ndefMsgIndex;

		unsigned int ndefRecordCount = 0;
		CHECK(nfc_get_ndef_record_count(ndefMessage, &ndefRecordCount));

		for (unsigned int ndefRecordIndex = 0; ndefRecordIndex < ndefRecordCount; ++ndefRecordIndex) {

			qDebug() << "XXXX Processing NDEF Record index: " << ndefRecordIndex;
			nfc_ndef_record_t *ndefRecord;
			CHECK( nfc_get_ndef_record(ndefMessage, ndefRecordIndex, &ndefRecord));

			uchar_t* ndefRecordPayloadData;
			size_t ndefRecordPayloadLength;
			CHECK(nfc_get_ndef_record_payload(ndefRecord, &ndefRecordPayloadData, &ndefRecordPayloadLength));

			tnf_type_t ndefRecordTnf;
			CHECK(nfc_get_ndef_record_tnf(ndefRecord, &ndefRecordTnf));

			char *ndefRecordNdefType;
			CHECK(nfc_get_ndef_record_type(ndefRecord, &ndefRecordNdefType));

			char *ndefRecordIdentifier;
			CHECK(nfc_get_ndef_record_id(ndefRecord, &ndefRecordIdentifier));

			QString ndefRecordPayloadAsHex =
					QString::fromAscii(reinterpret_cast<const char *>(ndefRecordPayloadData), ndefRecordPayloadLength).toAscii().toHex();

			QByteArray payLoadData = QByteArray::fromRawData(reinterpret_cast<const char *>(ndefRecordPayloadData), ndefRecordPayloadLength);

			emit message(QString("TNF: %1").arg(ndefRecordTnf));
			emit message(QString("Type: %1").arg(ndefRecordNdefType));
			emit message(QString("Id: %1").arg(ndefRecordIdentifier));

			if (strcmp(ndefRecordNdefType, Settings::NfcRtdSmartPoster) == 0) {
				qDebug() << "XXXX Smart Poster";

				emit message(QString("Detected a Smart Poster Tag"));

				char *utf_title;
				char *found_lang;
				char *uri;
				nfc_ndef_rtd_encoding_t rtd_encoding;
				rtd_encoding = UTF_8;

				qDebug() << "XXXX Calling nfc_get_sp_title";
				CHECK( nfc_get_sp_title(ndefRecord, Settings::LANG_EN, &utf_title, &found_lang, &rtd_encoding, true));

				qDebug() << "XXXX Done calling nfc_get_sp_title";

				qDebug() << "XXXX Calling nfc_get_sp_uri";
				CHECK(nfc_get_sp_uri(ndefRecord, &uri));
				qDebug() << "XXXX Done calling nfc_get_sp_uri";

				emit message(QString("Language: %1").arg(found_lang));
				emit message(QString("Title: %1").arg(utf_title));
				emit message(QString("URI: %1").arg(uri));

				qDebug() << QString("Message(%1)/Record(%2) Language: %3").arg(ndefMsgIndex).arg(ndefRecordIndex).arg(found_lang);

				qDebug() << QString("Message(%1)/Record(%2) Title: %3").arg(ndefMsgIndex).arg(ndefRecordIndex).arg(utf_title);

				qDebug() << QString("Message(%1)/Record(%2) URI: %3").arg(ndefMsgIndex).arg(ndefRecordIndex).arg(uri);

				free(utf_title);
				free(uri);
			} else if (strcmp(ndefRecordNdefType, Settings::NfcRtdUri) == 0) {

				qDebug() << "XXXX URI Tag";
				emit message(QString("Detected a URI Tag"));

				QString uri;
				uchar_t uriType;
				int uriLen;

				uriType = ndefRecordPayloadData[0];
				uriLen = payLoadData.length() - 1;

				uri = QString::fromUtf8(payLoadData.right(uriLen).constData(), uriLen);

				if (uriType == Settings::NfcRtdUriPrefixHttpWww) {
					uri.prepend(QString("http://www."));

				} else if (uriType == Settings::NfcRtdUriPrefixHttpsWww) {
					uri.prepend(QString("https://www."));

				} else if (uriType == Settings::NfcRtdUriPrefixHttp) {
					uri.prepend(QString("http://"));

				} else if (uriType == Settings::NfcRtdUriPrefixHttps) {
					uri.prepend(QString("https://"));

				} else if (uriType != Settings::NfcRtdUriPrefixNone) {
					emit message(QString("URI Prefix %1 not implemented").arg(uriType));
				}

				emit message(QString("URI: %1").arg(uri));

			} else if (strcmp(ndefRecordNdefType, Settings::NfcRtdText) == 0) {

				qDebug() << "XXXX Text Tag";
				emit message(QString("Detected a Text Tag"));

				QString text;
				QString language;
				int languageLen;
				int textLen;

				languageLen = ndefRecordPayloadData[0];
				textLen = payLoadData.length() - (languageLen + 1);

				language = QString::fromUtf8(payLoadData.mid(1, languageLen).constData(), languageLen);
				text = QString::fromUtf8(payLoadData.right(textLen).constData(), textLen);

				emit message(QString("Language: %1").arg(language));
				emit message(QString("Text: %1").arg(text));
			}

			qDebug() << QString("Message(%1)/Record(%2) Payload (Hex): %3").arg(ndefMsgIndex).arg(ndefRecordIndex).arg(ndefRecordPayloadAsHex);
			qDebug() << QString("Message(%1)/Record(%2) Type: %3").arg(ndefMsgIndex).arg(ndefRecordIndex).arg(ndefRecordNdefType);

			qDebug() << QString("Message(%1)/Record(%2) Id: %3").arg(ndefMsgIndex).arg(ndefRecordIndex).arg(ndefRecordIdentifier);

		}
	}
}