summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-06-08 12:18:58 +0200
committerIvan Solovev <ivan.solovev@qt.io>2021-06-09 14:46:17 +0200
commit723e824422c3ecf90b0e077cb376fabfc4e833ce (patch)
tree54b19c511452636bde1382ce50cae14c72a87d2f /examples
parenta6ca5f692a973649912c9340eaf1d61ebe7b1731 (diff)
AnnotatedUrl: handle adapter state
Extend the AnnotatedUrl example application to handle adapter state changes. This allows to correctly handle NFC enabling and disabling, while the application is running. Task-number: QTBUG-94033 Pick-to: 6.2 Change-Id: I9978436b98a772db256e12cfd41072c3b6286b88 Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'examples')
-rw-r--r--examples/nfc/annotatedurl/annotatedurl.cpp20
-rw-r--r--examples/nfc/annotatedurl/annotatedurl.h7
-rw-r--r--examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc12
-rw-r--r--examples/nfc/annotatedurl/main.cpp2
-rw-r--r--examples/nfc/annotatedurl/mainwindow.cpp6
-rw-r--r--examples/nfc/annotatedurl/mainwindow.h1
6 files changed, 43 insertions, 5 deletions
diff --git a/examples/nfc/annotatedurl/annotatedurl.cpp b/examples/nfc/annotatedurl/annotatedurl.cpp
index 5bb602e5..c1945083 100644
--- a/examples/nfc/annotatedurl/annotatedurl.cpp
+++ b/examples/nfc/annotatedurl/annotatedurl.cpp
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtNfc module.
@@ -72,6 +72,8 @@ AnnotatedUrl::AnnotatedUrl(QObject *parent)
this, &AnnotatedUrl::targetDetected);
connect(manager, &QNearFieldManager::targetLost,
this, &AnnotatedUrl::targetLost);
+ connect(manager, &QNearFieldManager::adapterStateChanged,
+ this, &AnnotatedUrl::handleAdapterStateChange);
}
AnnotatedUrl::~AnnotatedUrl()
@@ -83,10 +85,12 @@ void AnnotatedUrl::startDetection()
{
if (!manager->isEnabled()) {
qWarning() << "NFC not enabled";
+ emit nfcStateChanged(false);
return;
}
- manager->startTargetDetection(QNearFieldTarget::NdefAccess);
+ if (manager->startTargetDetection(QNearFieldTarget::NdefAccess))
+ emit nfcStateChanged(true);
}
void AnnotatedUrl::targetDetected(QNearFieldTarget *target)
@@ -111,6 +115,18 @@ void AnnotatedUrl::handlePolledNdefMessage(QNdefMessage message)
handleMessage(message, target);
}
+//! [handleAdapterState]
+void AnnotatedUrl::handleAdapterStateChange(QNearFieldManager::AdapterState state)
+{
+ if (state == QNearFieldManager::AdapterState::Online) {
+ startDetection();
+ } else if (state == QNearFieldManager::AdapterState::Offline) {
+ manager->stopTargetDetection();
+ emit nfcStateChanged(false);
+ }
+}
+//! [handleAdapterState]
+
//! [handleMessage 1]
void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *target)
{
diff --git a/examples/nfc/annotatedurl/annotatedurl.h b/examples/nfc/annotatedurl/annotatedurl.h
index 59ada47a..4c0bdbe0 100644
--- a/examples/nfc/annotatedurl/annotatedurl.h
+++ b/examples/nfc/annotatedurl/annotatedurl.h
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtNfc module.
@@ -52,12 +52,12 @@
#define ANNOTATEDURL_H
#include <QtNfc/QNdefMessage>
+#include <QtNfc/QNearFieldManager>
#include <QtCore/QObject>
#include <QtCore/QUrl>
QT_FORWARD_DECLARE_CLASS(QPixmap)
-QT_FORWARD_DECLARE_CLASS(QNearFieldManager)
QT_FORWARD_DECLARE_CLASS(QNearFieldTarget)
//! [0]
@@ -73,12 +73,15 @@ public:
signals:
void annotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap);
+ void nfcStateChanged(bool enabled);
public slots:
void targetDetected(QNearFieldTarget *target);
void targetLost(QNearFieldTarget *target);
void handleMessage(const QNdefMessage &message, QNearFieldTarget *target);
void handlePolledNdefMessage(QNdefMessage message);
+ void handleAdapterStateChange(QNearFieldManager::AdapterState state);
+
private:
QNearFieldManager *manager;
};
diff --git a/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc b/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc
index c3c3d525..b2004839 100644
--- a/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc
+++ b/examples/nfc/annotatedurl/doc/src/annotatedurl.qdoc
@@ -1,6 +1,6 @@
/****************************************************************************
**
-** Copyright (C) 2017 The Qt Company Ltd.
+** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the documentation of the Qt local connectivty modules.
@@ -82,6 +82,16 @@ corresponding signal is emitted so that the UI can handle it.
\snippet annotatedurl/annotatedurl.cpp handleMessage 4
+\section1 Adapter State Handling
+
+On Android the adapter state changes can be detected by connecting to the
+\l QNearFieldManager::adapterStateChanged() signal. This allows stopping the
+detection when the NFC adapter is disabled, and restarting it when the adapter
+is enabled again. This approach is implemented in the
+\c {AnnotatedUrl::handleAdapterStateChange} slot.
+
+\snippet annotatedurl/annotatedurl.cpp handleAdapterState
+
\section1 Automatic Application Startup
Android supports automatic application startup when the NDEF tag is touched.
diff --git a/examples/nfc/annotatedurl/main.cpp b/examples/nfc/annotatedurl/main.cpp
index d0f68eb2..8134b002 100644
--- a/examples/nfc/annotatedurl/main.cpp
+++ b/examples/nfc/annotatedurl/main.cpp
@@ -66,6 +66,8 @@ int main(int argc, char *argv[])
QObject::connect(&annotatedUrl, &AnnotatedUrl::annotatedUrl,
&mainWindow, &MainWindow::displayAnnotatedUrl);
+ QObject::connect(&annotatedUrl, &AnnotatedUrl::nfcStateChanged,
+ &mainWindow, &MainWindow::nfcStateChanged);
annotatedUrl.startDetection();
mainWindow.show();
diff --git a/examples/nfc/annotatedurl/mainwindow.cpp b/examples/nfc/annotatedurl/mainwindow.cpp
index 707c03eb..03f4ee09 100644
--- a/examples/nfc/annotatedurl/mainwindow.cpp
+++ b/examples/nfc/annotatedurl/mainwindow.cpp
@@ -75,6 +75,12 @@ void MainWindow::displayAnnotatedUrl(const QUrl &url, const QString &title, cons
ui->m_image->setPixmap(pixmap);
}
+void MainWindow::nfcStateChanged(bool enabled)
+{
+ const QString text = enabled ? "Touch a tag" : "Enable NFC";
+ ui->m_help->setText(text);
+}
+
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if (ui->centralWidget->rect().contains(event->pos()))
diff --git a/examples/nfc/annotatedurl/mainwindow.h b/examples/nfc/annotatedurl/mainwindow.h
index 36ad9944..28962977 100644
--- a/examples/nfc/annotatedurl/mainwindow.h
+++ b/examples/nfc/annotatedurl/mainwindow.h
@@ -75,6 +75,7 @@ public:
public slots:
void displayAnnotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap);
+ void nfcStateChanged(bool enabled);
protected:
void mouseReleaseEvent(QMouseEvent *event) override;