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