Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
nfc-overview.qdoc
Go to the documentation of this file.
1// Copyright (C) 2013 Aaron McCarthy <mccarthy.aaron@gmail.com>
2// Copyright (C) 2017 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4
5/*!
6\ingroup technology-apis
7\title Qt NFC Overview
8\page qtnfc-overview.html
9\brief Provides access to NFC enabled devices.
10\ingroup explanations-networkingandconnectivity
11
12With the Qt NFC API typical use cases are:
13
14\list
15 \li Detecting NFC tags.
16 \li Reading and writing NDEF messages.
17 \li Registering NDEF message handlers.
18 \li Sharing files and messages.
19\endlist
20
21The following sections describe how to use Qt NFC C++ classes and QML types for the above use cases.
22
23\note On Android, the detection of new NFC tags only works in foreground applications. Android
24services do not support this because of API limitations on the Android side. The only way to use a
25\l{https://developer.android.com/reference/android/nfc/Tag}{Tag} in a service is to provide an
26\l{https://developer.android.com/guide/components/aidl}{AIDL} interface accepting the Tag and forward
27it to Qt as shown in the following example.
28
29\code
30 public void setTag(Tag pTag) {
31 Intent newIntent = new Intent();
32 newIntent.putExtra(NfcAdapter.EXTRA_TAG, pTag);
33 QtNative.onNewIntent(newIntent);
34 }
35\endcode
36
37\section1 C++ Overview
38
39The C++ API provides access to the full feature set of the Qt NFC API. This section introduces the
40major features available to developers.
41
42\section2 Detecting NFC Tags
43
44The \l QNearFieldManager class is responsible for the detection of new NFC tags coming
45into range of the device. The \l QNearFieldManager::targetDetected() and
46\l QNearFieldManager::targetLost() signals are emitted when
47a tag comes into or leaves the range. The passed \l QNearFieldTarget parameter acts
48as primary interaction point for each detected tag. The detection does not actually start though until
49\l QNearFieldManager::startTargetDetection() has been called.
50
51\code
52m_manager = new QNearFieldManager(this);
53connect(m_manager, &QNearFieldManager::targetDetected,
54 this, &MainWindow::targetDetected);
55connect(m_manager, &QNearFieldManager::targetLost,
56 this, &MainWindow::targetLost);
57m_manager->startTargetDetection(QNearFieldTarget::NdefAccess);
58\endcode
59
60Finally the detection can be stopped:
61
62\code
63m_manager->stopTargetDetection();
64\endcode
65
66Although each \l QNearFieldTarget instance is owned by its related \l QNearFieldManager
67instance it can be beneficial to manually delete each instance. Otherwise they would continue to
68exist until the \l QNearFieldManager instance is deleted. The best way to do that would be in response
69to the \l QNearFieldManager::targetLost() signal:
70
71\code
72void MainWindow::targetLost(QNearFieldTarget *target)
73{
74 target->deleteLater();
75}
76\endcode
77
78\note The target object should only be deleted via deleteLater() if it is deleted inside the slot.
79
80\section2 Connecting NFC Tags
81
82All functions of \l QNearFieldTarget that require a connection will create one by its own.
83An active connection will prevent other instances to create a connection because only one
84connection at the time is allowed.
85
86Qt 5 disconnected the tag at the end of the functions to allow other instances to connect.
87QNearFieldManager::setKeepConnection() allowed to change this behavior.
88
89Since Qt 6, \l QNearFieldTarget keeps the connection by default. The connection is only closed
90when the \l QNearFieldTarget is destroyed or \l QNearFieldManager::disconnect() is called.
91
92\section2 Reading and Writing NDEF Messages
93
94The \l QNearFieldTarget instance returned by \l QNearFieldManager::targetDetected() signal
95is used to interact with the tag. Reading and writing a message is an asynchronous operation.
96The \l QNearFieldTarget::RequestId class associates individual operations and their results.
97
98\code
99void MainWindow::targetDetected(QNearFieldTarget *target)
100{
101 switch (m_touchAction) {
102 case NoAction:
103 break;
104 case ReadNdef:
105 connect(target, &QNearFieldTarget::ndefMessageRead, this, &MainWindow::ndefMessageRead);
106 connect(target, &QNearFieldTarget::error, this, &MainWindow::targetError);
107
108 m_request = target->readNdefMessages();
109 if (!m_request.isValid()) // cannot read messages
110 targetError(QNearFieldTarget::NdefReadError, m_request);
111 break;
112 case WriteNdef:
113 connect(target, &QNearFieldTarget::requestCompleted, this, &MainWindow::ndefMessageWritten);
114 connect(target, &QNearFieldTarget::error, this, &MainWindow::targetError);
115
116 m_request = target->writeNdefMessages(QList<QNdefMessage>() << ndefMessage());
117 if (!m_request.isValid()) // cannot write messages
118 targetError(QNearFieldTarget::NdefWriteError, m_request);
119 break;
120 }
121}
122\endcode
123
124Once the \l QNearFieldTarget::readNdefMessages() request was successfully processed, the
125\l QNearFieldTarget::ndefMessageRead() signal is emitted. Each returned \l QNdefMessage
126may consist of zero or more \l QNdefRecord entries, which can be identified by their type.
127For more information about processing of records, see the \l QNdefRecord class documentation.
128As the above code demonstrates, writing of NDEF messages is triggered via
129\l QNearFieldTarget::writeNdefMessages(). The successful completion of the write operation
130is indicated by the emission of the \l QNearFieldTarget::requestCompleted() signal with the
131corresponding request id. Any type of error during read or write is indicated via
132\l QNearFieldTarget::error().
133*/