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>
This commit is contained in:
Alex Blasche 2014-12-08 11:19:16 +01:00
parent 994934b36b
commit 0267147014
6 changed files with 111 additions and 27 deletions

View File

@ -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]

View File

@ -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

View File

@ -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)));

View File

@ -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" }
]

View File

@ -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";

View File

@ -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