summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-06-09 11:45:59 +0200
committerIvan Solovev <ivan.solovev@qt.io>2021-06-14 10:55:00 +0200
commitf6e57299b44552fa063a69f0549f35fb635fd650 (patch)
treec48297c73b19d99e5e9c91ee2104a2b224cdd17a /examples
parent5b9d9454d5cfcd31840a2c34a1bab30ab5e6ca18 (diff)
AnnotatedUrl: add QNdefFilter support
Update the example to show the usage of QNdefFilter. Task-number: QTBUG-94033 Pick-to: 6.2 Change-Id: I41b463bcf41bd3f4cb5b60981d9e3a89f7622531 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/nfc/annotatedurl/annotatedurl.cpp26
-rw-r--r--examples/nfc/annotatedurl/annotatedurl.h3
-rw-r--r--examples/nfc/annotatedurl/doc/images/annotatedurl3.pngbin0 -> 6392 bytes
-rw-r--r--examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc35
-rw-r--r--examples/nfc/annotatedurl/main.cpp2
-rw-r--r--examples/nfc/annotatedurl/mainwindow.cpp12
-rw-r--r--examples/nfc/annotatedurl/mainwindow.h1
7 files changed, 72 insertions, 7 deletions
diff --git a/examples/nfc/annotatedurl/annotatedurl.cpp b/examples/nfc/annotatedurl/annotatedurl.cpp
index c1945083..9c9ef351 100644
--- a/examples/nfc/annotatedurl/annotatedurl.cpp
+++ b/examples/nfc/annotatedurl/annotatedurl.cpp
@@ -74,6 +74,13 @@ AnnotatedUrl::AnnotatedUrl(QObject *parent)
this, &AnnotatedUrl::targetLost);
connect(manager, &QNearFieldManager::adapterStateChanged,
this, &AnnotatedUrl::handleAdapterStateChange);
+
+//! [populateFilter]
+ messageFilter.setOrderMatch(false);
+ messageFilter.appendRecord<QNdefNfcTextRecord>(1, 100);
+ messageFilter.appendRecord<QNdefNfcUriRecord>(1, 1);
+ messageFilter.appendRecord(QNdefRecord::Mime, "", 0, 1);
+//! [populateFilter]
}
AnnotatedUrl::~AnnotatedUrl()
@@ -100,6 +107,8 @@ void AnnotatedUrl::targetDetected(QNearFieldTarget *target)
connect(target, &QNearFieldTarget::ndefMessageRead,
this, &AnnotatedUrl::handlePolledNdefMessage);
+ connect(target, &QNearFieldTarget::error, this,
+ [this]() { emit tagError("Tag read error"); });
target->readNdefMessages();
}
@@ -133,6 +142,13 @@ void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *
//! [handleMessage 1]
Q_UNUSED(target);
+//! [handleMessage 2]
+ if (!messageFilter.match(message)) {
+ emit tagError("Invalid message format");
+ return;
+ }
+//! [handleMessage 2]
+
enum {
MatchedNone,
MatchedFirst,
@@ -147,14 +163,14 @@ void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *
QUrl url;
QPixmap pixmap;
-//! [handleMessage 2]
+//! [handleMessage 3]
for (const QNdefRecord &record : message) {
if (record.isRecordType<QNdefNfcTextRecord>()) {
QNdefNfcTextRecord textRecord(record);
title = textRecord.text();
QLocale locale(textRecord.locale());
-//! [handleMessage 2]
+//! [handleMessage 3]
// already found best match
if (bestMatch == MatchedLanguageAndCountry) {
// do nothing
@@ -168,7 +184,7 @@ void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *
} else if (bestMatch == MatchedNone) {
bestMatch = MatchedFirst;
}
-//! [handleMessage 3]
+//! [handleMessage 4]
} else if (record.isRecordType<QNdefNfcUriRecord>()) {
QNdefNfcUriRecord uriRecord(record);
@@ -177,10 +193,10 @@ void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *
record.type().startsWith("image/")) {
pixmap = QPixmap::fromImage(QImage::fromData(record.payload()));
}
-//! [handleMessage 3]
//! [handleMessage 4]
+//! [handleMessage 5]
}
emit annotatedUrl(url, title, pixmap);
}
-//! [handleMessage 4]
+//! [handleMessage 5]
diff --git a/examples/nfc/annotatedurl/annotatedurl.h b/examples/nfc/annotatedurl/annotatedurl.h
index 4c0bdbe0..3e823f3e 100644
--- a/examples/nfc/annotatedurl/annotatedurl.h
+++ b/examples/nfc/annotatedurl/annotatedurl.h
@@ -53,6 +53,7 @@
#include <QtNfc/QNdefMessage>
#include <QtNfc/QNearFieldManager>
+#include <QtNfc/QNdefFilter>
#include <QtCore/QObject>
#include <QtCore/QUrl>
@@ -74,6 +75,7 @@ public:
signals:
void annotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap);
void nfcStateChanged(bool enabled);
+ void tagError(const QString &error);
public slots:
void targetDetected(QNearFieldTarget *target);
@@ -84,6 +86,7 @@ public slots:
private:
QNearFieldManager *manager;
+ QNdefFilter messageFilter;
};
//! [0]
diff --git a/examples/nfc/annotatedurl/doc/images/annotatedurl3.png b/examples/nfc/annotatedurl/doc/images/annotatedurl3.png
new file mode 100644
index 00000000..363b7687
--- /dev/null
+++ b/examples/nfc/annotatedurl/doc/images/annotatedurl3.png
Binary files differ
diff --git a/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc b/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc
index b2004839..fc229d14 100644
--- a/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc
+++ b/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc
@@ -63,6 +63,31 @@ is touched. Such usecase is currently supported on Android.
\snippet annotatedurl/main.cpp 0
+\section1 Message Filtering
+
+As it is mentioned above, the application supports the NDEF messages of a
+specific format. A correct message should contain the following fields:
+
+\list
+ \li \e {At least one} NDEF Text record, which will be used as a header.
+ \li \e {Exactly one} NDEF URI record.
+ \li \e {An optional} MIME record with an icon.
+\endlist
+
+The order of the records is not strictly specified.
+
+An instance of \l QNdefFilter is used to validate the NDEF message. The filter
+is populated as follows:
+
+\snippet annotatedurl/annotatedurl.cpp populateFilter
+
+If the incoming message does not match the filter, an error message is shown:
+
+\image annotatedurl3.png
+
+\note The \l {ndefeditor}{NDEF Editor} example application can be used to create
+the tags with correct or incorrect message structure.
+
\section1 AnnotatedUrl Handler Implementation
NFC messages read by the \l QNearFieldManager are forwarded to
@@ -70,17 +95,23 @@ NFC messages read by the \l QNearFieldManager are forwarded to
\snippet annotatedurl/annotatedurl.cpp handleMessage 1
+At first the messages are validated using the \l QNdefFilter::match() method:
+
+\snippet annotatedurl/annotatedurl.cpp handleMessage 2
+
+If the messages have the correct format, the parsing continues.
+
Because NFC messages are composed of several NDEF records, looping
through all of the records allows the extraction of the 3 parameters
to be displayed in the UI: the Uri, the Title and the Pixmap:
-\snippet annotatedurl/annotatedurl.cpp handleMessage 2
\snippet annotatedurl/annotatedurl.cpp handleMessage 3
+\snippet annotatedurl/annotatedurl.cpp handleMessage 4
Finally after having extracted the parameters of the NFC message the
corresponding signal is emitted so that the UI can handle it.
-\snippet annotatedurl/annotatedurl.cpp handleMessage 4
+\snippet annotatedurl/annotatedurl.cpp handleMessage 5
\section1 Adapter State Handling
diff --git a/examples/nfc/annotatedurl/main.cpp b/examples/nfc/annotatedurl/main.cpp
index 8134b002..52cb40aa 100644
--- a/examples/nfc/annotatedurl/main.cpp
+++ b/examples/nfc/annotatedurl/main.cpp
@@ -68,6 +68,8 @@ int main(int argc, char *argv[])
&mainWindow, &MainWindow::displayAnnotatedUrl);
QObject::connect(&annotatedUrl, &AnnotatedUrl::nfcStateChanged,
&mainWindow, &MainWindow::nfcStateChanged);
+ QObject::connect(&annotatedUrl, &AnnotatedUrl::tagError,
+ &mainWindow, &MainWindow::showTagError);
annotatedUrl.startDetection();
mainWindow.show();
diff --git a/examples/nfc/annotatedurl/mainwindow.cpp b/examples/nfc/annotatedurl/mainwindow.cpp
index e9863d03..6cac65c7 100644
--- a/examples/nfc/annotatedurl/mainwindow.cpp
+++ b/examples/nfc/annotatedurl/mainwindow.cpp
@@ -111,6 +111,18 @@ void MainWindow::nfcStateChanged(bool enabled)
m_infoLabel->setText(text);
}
+void MainWindow::showTagError(const QString &message)
+{
+ m_uriLabel->clear();
+ m_titleLabel->clear();
+ m_pixmap = QPixmap();
+ m_landscapeIconLabel->setVisible(false);
+ m_portraitIconLabel->setVisible(false);
+
+ m_infoLabel->setHidden(false);
+ m_infoLabel->setText(message);
+}
+
void MainWindow::mouseReleaseEvent(QMouseEvent * /*event*/)
{
QDesktopServices::openUrl(QUrl(m_uriLabel->text()));
diff --git a/examples/nfc/annotatedurl/mainwindow.h b/examples/nfc/annotatedurl/mainwindow.h
index 1770c39f..c5e80cb5 100644
--- a/examples/nfc/annotatedurl/mainwindow.h
+++ b/examples/nfc/annotatedurl/mainwindow.h
@@ -71,6 +71,7 @@ public:
public slots:
void displayAnnotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap);
void nfcStateChanged(bool enabled);
+ void showTagError(const QString &message);
protected:
void mouseReleaseEvent(QMouseEvent *event) override;