Files:
The NFC Receiver example allows the user to simply read data from NFC tags.
In this example we'll learn how to use the bb::system::InvokeManager to be invoked on the arrival of a NFC tag and how to extract data out of the received NDEF message. The business logic is encapsulated in the C++ class NfcReceiver, which is exported to QML under the name '_nfcReceiver'.
NfcReceiver nfcReceiver; QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(&app); qml->setContextProperty("_nfcReceiver", &nfcReceiver);
The UI is intended to display the properties of the NDEF message, which is shared via some NFC tag, or shared data from another NFC enabled device. The NDEF message records, containing the property information, are displayed through a ListView.
// The list view that shows the records of the received NDEF message ListView { horizontalAlignment: HorizontalAlignment.Fill verticalAlignment: VerticalAlignment.Fill dataModel: _nfcReceiver.messageRecords listItemComponents: [ ListItemComponent { type: "" Container { preferredWidth: 768 leftPadding: 10 rightPadding: 10 Field { title: qsTr("tnf type") value: ListItemData.tnfType == "0" ? qsTr("Empty Tag") : ListItemData.tnfType == "1" ? qsTr("Well Known Type") : ListItemData.tnfType == "2" ? qsTr("Media (MIME)") : ListItemData.tnfType == "3" ? qsTr("Uri") : ListItemData.tnfType == "4" ? qsTr("External") : "" } Field { title: qsTr("record type") value: ListItemData.recordType == "Sp" ? qsTr("Smart Poster") : ListItemData.recordType == "T" ? qsTr("Text") : ListItemData.recordType == "U" ? qsTr("Uri") : ListItemData.recordType == "Gc" ? qsTr("Generic Control") : ListItemData.recordType == "Hr" ? qsTr("Handover Request") : ListItemData.recordType == "Hs" ? qsTr("Handover Select") : ListItemData.recordType == "Hc" ? qsTr("Handover Carrier") : ListItemData.recordType == "Sig" ? qsTr("Signature") : "" } Field { title: qsTr("payload") value: ListItemData.payload } Field { title: qsTr("hex") value: ListItemData.hexPayload } } } ] }
This class retrieves the NdefMessage being transmitted, extracts its properties and displays them on the screen.
NfcReceiver::NfcReceiver(QObject *parent) : QObject(parent) , m_messageRecords(new bb::cascades::QVariantListDataModel()) { m_messageRecords->setParent(this); // Create an InvokeManager bb::system::InvokeManager *invokeManager = new bb::system::InvokeManager(this); /** * The signal invoked(const bb::system::InvokeRequest&) is used for applications. */ bool ok = connect(invokeManager, SIGNAL(invoked(const bb::system::InvokeRequest&)), this, SLOT(receivedInvokeTarget(const bb::system::InvokeRequest&))); Q_ASSERT(ok); Q_UNUSED(ok); }
Inside the constructor the InvokeManager is initialized, from whom we listen for invoke requests.
void NfcReceiver::receivedInvokeTarget(const bb::system::InvokeRequest& request) { // The data contains our NDEF message const QByteArray data = request.data(); // Create out NDEF message const QtMobilitySubset::QNdefMessage ndefMessage = QtMobilitySubset::QNdefMessage::fromByteArray(data); // Fill the model with the single records m_messageRecords->clear(); for (int i = 0; i < ndefMessage.size(); ++i) { const QtMobilitySubset::QNdefRecord record = ndefMessage.at(i); QVariantMap entry; entry["tnfType"] = record.typeNameFormat(); entry["recordType"] = QString::fromLatin1(record.type()); entry["payload"] = QString::fromLatin1(record.payload()); entry["hexPayload"] = QString::fromLatin1(record.payload().toHex()); m_messageRecords->append(entry); } emit messageReceived(); }
This method extracts the data portion of the request, which is the QNdefMessage we have been waiting for. Afterwards, it access' the record entries from the message and saves them in the QVariantListDataModel for display.