diff options
author | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-12-08 11:19:16 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2014-12-10 12:47:40 +0100 |
commit | 026714701477706231298e92878286930baf3948 (patch) | |
tree | 6a2cd84e6d91dbf9efdd8f26884357ac0a931d94 /examples/nfc | |
parent | 994934b36bcfa74da8975ec31da528ddded50e16 (diff) |
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 <martin.leutelt@basyskom.com>
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'examples/nfc')
-rw-r--r-- | examples/nfc/annotatedurl/annotatedurl.cpp | 48 | ||||
-rw-r--r-- | examples/nfc/annotatedurl/annotatedurl.h | 13 | ||||
-rw-r--r-- | examples/nfc/annotatedurl/main.cpp | 10 | ||||
-rw-r--r-- | examples/nfc/corkboard/corkboards.qml | 19 | ||||
-rw-r--r-- | examples/nfc/poster/poster.qml | 46 |
5 files changed, 110 insertions, 26 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 <qnearfieldmanager.h> #include <qnearfieldtarget.h> #include <qndefmessage.h> #include <qndefrecord.h> @@ -53,14 +54,61 @@ #include <QLabel> #include <QMouseEvent> #include <QDesktopServices> +#include <QDebug> 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<QNdefNfcTextRecord>(1, UINT_MAX); + filter.appendRecord<QNdefNfcUriRecord>(); + 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<QNearFieldTarget *>(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 <qnfcglobal.h> +#include <QNdefMessage> #include <QtCore/QObject> 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<QNdefNfcTextRecord>(1, UINT_MAX); - filter.appendRecord<QNdefNfcUriRecord>(); - 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; + } + break; + case NdefRecord.Mime: + if (messageRecords[i].type.startsWith("image/")) { + posterImage.source = 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; } + + if (!found) + console.warn("Unknown NFC tag detected. Cannot display content.") } root.state = "show"; |