From 026714701477706231298e92878286930baf3948 Mon Sep 17 00:00:00 2001 From: Alex Blasche Date: Mon, 8 Dec 2014 11:19:16 +0100 Subject: [PATCH] Enable automatic tag polling if message handler is not supported. All examples have used message handler registration so far. That's not always supported on all platforms. Neard is one of those platforms against which this this was verified. This patch converts some examples to use manual target detection if handler registration fails. Change-Id: Icfd8b7c695e63351a45b867fd69e9fc5fefb9360 Reviewed-by: Martin Leutelt Reviewed-by: Alex Blasche --- examples/nfc/annotatedurl/annotatedurl.cpp | 48 ++++++++++++++++++++++ examples/nfc/annotatedurl/annotatedurl.h | 13 +++--- examples/nfc/annotatedurl/main.cpp | 10 ----- examples/nfc/corkboard/corkboards.qml | 19 ++++++++- examples/nfc/poster/poster.qml | 46 +++++++++++++++++---- src/nfc/doc/src/nfc-overview.qdoc | 2 +- 6 files changed, 111 insertions(+), 27 deletions(-) diff --git a/examples/nfc/annotatedurl/annotatedurl.cpp b/examples/nfc/annotatedurl/annotatedurl.cpp index f29045b6..909f199b 100644 --- a/examples/nfc/annotatedurl/annotatedurl.cpp +++ b/examples/nfc/annotatedurl/annotatedurl.cpp @@ -40,6 +40,7 @@ #include "annotatedurl.h" +#include #include #include #include @@ -53,14 +54,61 @@ #include #include #include +#include AnnotatedUrl::AnnotatedUrl(QObject *parent) : QObject(parent) { + //! [QNearFieldManager register handler] + manager = new QNearFieldManager(this); + if (!manager->isAvailable()) { + qWarning() << "NFC not available"; + return; + } + + QNdefFilter filter; + filter.setOrderMatch(false); + filter.appendRecord(1, UINT_MAX); + filter.appendRecord(); + int result = manager->registerNdefMessageHandler(filter, this, + SLOT(handleMessage(QNdefMessage,QNearFieldTarget*))); + //! [QNearFieldManager register handler] + + if (result != -1) + return; + + manager->startTargetDetection(); + connect(manager, SIGNAL(targetDetected(QNearFieldTarget*)), + this, SLOT(targetDetected(QNearFieldTarget*))); + connect(manager, SIGNAL(targetLost(QNearFieldTarget*)), + this, SLOT(targetLost(QNearFieldTarget*))); } AnnotatedUrl::~AnnotatedUrl() { + +} + +void AnnotatedUrl::targetDetected(QNearFieldTarget *target) +{ + if (!target) + return; + + connect(target, SIGNAL(ndefMessageRead(QNdefMessage)), + this, SLOT(handlePolledNdefMessage(QNdefMessage))); + target->readNdefMessages(); +} + +void AnnotatedUrl::targetLost(QNearFieldTarget *target) +{ + if (target) + target->deleteLater(); +} + +void AnnotatedUrl::handlePolledNdefMessage(QNdefMessage message) +{ + QNearFieldTarget *target = qobject_cast(sender()); + handleMessage(message, target); } //! [handleMessage 1] diff --git a/examples/nfc/annotatedurl/annotatedurl.h b/examples/nfc/annotatedurl/annotatedurl.h index 863f0976..b0c69a62 100644 --- a/examples/nfc/annotatedurl/annotatedurl.h +++ b/examples/nfc/annotatedurl/annotatedurl.h @@ -42,16 +42,14 @@ #define ANNOTATEDURL_H #include +#include #include QT_FORWARD_DECLARE_CLASS(QUrl) QT_FORWARD_DECLARE_CLASS(QPixmap) - -QT_BEGIN_NAMESPACE -class QNearFieldTarget; -class QNdefMessage; -QT_END_NAMESPACE +QT_FORWARD_DECLARE_CLASS(QNearFieldManager) +QT_FORWARD_DECLARE_CLASS(QNearFieldTarget) QT_USE_NAMESPACE @@ -67,7 +65,12 @@ signals: void annotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap); public slots: + void targetDetected(QNearFieldTarget *target); + void targetLost(QNearFieldTarget *target); void handleMessage(const QNdefMessage &message, QNearFieldTarget *target); + void handlePolledNdefMessage(QNdefMessage message); +private: + QNearFieldManager *manager; }; #endif // ANNOTATEDURL_H diff --git a/examples/nfc/annotatedurl/main.cpp b/examples/nfc/annotatedurl/main.cpp index 351f33bf..659aeb15 100644 --- a/examples/nfc/annotatedurl/main.cpp +++ b/examples/nfc/annotatedurl/main.cpp @@ -57,18 +57,8 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); MainWindow mainWindow; - //! [QNearFieldManager register handler] - QNearFieldManager manager; AnnotatedUrl annotatedUrl; - QNdefFilter filter; - filter.setOrderMatch(false); - filter.appendRecord(1, UINT_MAX); - filter.appendRecord(); - manager.registerNdefMessageHandler(filter, &annotatedUrl, - SLOT(handleMessage(QNdefMessage,QNearFieldTarget*))); - //! [QNearFieldManager register handler] - QObject::connect(&annotatedUrl, SIGNAL(annotatedUrl(QUrl,QString,QPixmap)), &mainWindow, SLOT(displayAnnotatedUrl(QUrl,QString,QPixmap))); diff --git a/examples/nfc/corkboard/corkboards.qml b/examples/nfc/corkboard/corkboards.qml index 55c83723..550dc03b 100644 --- a/examples/nfc/corkboard/corkboards.qml +++ b/examples/nfc/corkboard/corkboards.qml @@ -39,7 +39,7 @@ ** ****************************************************************************/ -import QtQuick 2.1 +import QtQuick 2.3 import QtNfc 5.2 Rectangle { @@ -47,11 +47,27 @@ Rectangle { color: "black" NearField { + property bool requiresManualPolling: false orderMatch: false onMessageRecordsChanged: { list.get(listView.currentIndex).notes.append({"noteText":messageRecords[0].text}) } + + onPollingChanged: { + if (!polling && requiresManualPolling) + polling = true; //restart polling + } + + Component.onCompleted: { + // Polling should be true if + // QNearFieldManager::registerNdefMessageHandler() was successful; + // otherwise the platform requires manual polling mode. + if (!polling) { + requiresManualPolling = true; + polling = true; + } + } } ListModel { @@ -60,7 +76,6 @@ Rectangle { ListElement { name: "Personal" notes: [ - ListElement { noteText: "https://developer.blackberry.com" }, ListElement { noteText: "Near Field Communication" }, ListElement { noteText: "Touch a tag and its contents will appear as a new note" } ] diff --git a/examples/nfc/poster/poster.qml b/examples/nfc/poster/poster.qml index e2c67dd9..34181017 100644 --- a/examples/nfc/poster/poster.qml +++ b/examples/nfc/poster/poster.qml @@ -38,7 +38,7 @@ ** ****************************************************************************/ -import QtQuick 2.0 +import QtQuick 2.3 import QtNfc 5.2 Rectangle { @@ -48,6 +48,19 @@ Rectangle { NearField { id: nearfield + property bool requiresManualPolling: false + + onPollingChanged: { + if (!polling && requiresManualPolling) + polling = true; //restart polling + } + + Component.onCompleted: { + if (!polling) { + requiresManualPolling = true; + polling = true; + } + } filter: [ NdefFilter { type: "U"; typeNameFormat: NdefRecord.NfcRtd; minimum: 1; maximum: 1 }, @@ -61,17 +74,32 @@ Rectangle { var currentLocaleMatch = NdefTextRecord.LocaleMatchedNone; var i; + var found = false; for (i = 0; i < messageRecords.length; ++i) { - if (messageRecords[i].recordType == "urn:nfc:wkt:T") { - if (messageRecords[i].localeMatch > currentLocaleMatch) { - currentLocaleMatch = messageRecords[i].localeMatch; - posterText.text = messageRecords[i].text; + switch (messageRecords[i].typeNameFormat) { + case NdefRecord.NfcRtd: + if (messageRecords[i].type === "T") { + if (messageRecords[i].localeMatch > currentLocaleMatch) { + currentLocaleMatch = messageRecords[i].localeMatch; + posterText.text = messageRecords[i].text; + found = true; + } + + } else if (messageRecords[i].type === "U") { + posterUrl.text = messageRecords[i].uri; + found = true; } - } else if (messageRecords[i].recordType == "urn:nfc:wkt:U") { - posterUrl.text = messageRecords[i].uri; - } else if (messageRecords[i].recordType.substr(0, 19) == "urn:nfc:mime:image/") { - posterImage.source = messageRecords[i].uri; + break; + case NdefRecord.Mime: + if (messageRecords[i].type.startsWith("image/")) { + posterImage.source = messageRecords[i].uri; + found = true; + } + break; } + + if (!found) + console.warn("Unknown NFC tag detected. Cannot display content.") } root.state = "show"; diff --git a/src/nfc/doc/src/nfc-overview.qdoc b/src/nfc/doc/src/nfc-overview.qdoc index b068dfb5..f068201c 100644 --- a/src/nfc/doc/src/nfc-overview.qdoc +++ b/src/nfc/doc/src/nfc-overview.qdoc @@ -105,7 +105,7 @@ Depending on the platform it may even be possible to start the application that \note This feature is not available on all platforms and, in addition to the code snippets below, may require further platform specific setup. -\snippet annotatedurl/main.cpp QNearFieldManager register handler +\snippet annotatedurl/annotatedurl.cpp QNearFieldManager register handler For comparison an application that uses an empty NDEF filter (match all behavior) in combination with \l QNearFieldManager::registerNdefMessageHandler() would behave similarly to another application that uses