QNearFieldManager Class

The QNearFieldManager class provides access to notifications for NFC events. More...

Header: #include <QNearFieldManager>
qmake: QT += nfc
Since: Qt 5.2
Inherits: QObject

This class was introduced in Qt 5.2.

Public Types

enum class AdapterState { Offline, TurningOn, Online, TurningOff }
enum TargetAccessMode { NoTargetAccess, NdefReadTargetAccess, NdefWriteTargetAccess, TagTypeSpecificTargetAccess }

Detailed Description

NFC Forum devices support two modes of communications. The first mode, peer-to-peer communications, is used to communicate between two NFC Forum devices. The second mode, master/slave communications, is used to communicate between an NFC Forum device and an NFC Forum Tag or Contactless Card. The targetDetected() signal is emitted when a target device enters communications range. Communications can be initiated from the slot connected to this signal.

NFC Forum devices generally operate as the master in master/slave communications. Some devices are also capable of operating as the slave, so called Card Emulation mode. In this mode the local NFC device emulates a NFC Forum Tag or Contactless Card.

NFC Forum Tags can contain one or more messages in a standardized format. These messages are encapsulated by the QNdefMessage class. Use the registerNdefMessageHandler() functions to register message handlers with particular criteria. Handlers can be unregistered with the unregisterNdefMessageHandler() function.

Applications can connect to the targetDetected() and targetLost() signals to get notified when an NFC Forum Tag enters or leaves proximity. Before these signals are emitted target detection must be started with the startTargetDetection() function. Target detection can be stopped with the stopTargetDetection() function. Before a detected target can be accessed it is necessary to request access rights. This must be done before the target device is touched. The setTargetAccessModes() function is used to set the types of access the application wants to perform on the detected target. When access is no longer required the target access modes should be set to NoTargetAccess as other applications may be blocked from accessing targets. The current target access modes can be retried with the targetAccessModes() function.

Automatically launching NDEF message handlers

On some platforms it is possible to pre-register an application to receive NDEF messages matching a given criteria. This is useful to get the system to automatically launch your application when a matching NDEF message is received. This removes the need to have the user manually launch NDEF handling applications, prior to touching a tag, or to have those applications always running and using system resources.

The process of registering the handler is different for each platform. Please refer to the platform documentation on how such a registration may be done. If the application has been registered as an NDEF message handler, the application only needs to call the registerNdefMessageHandler() function:

 QNearFieldManager *manager = new QNearFieldManager(this);
 manager->registerNdefMessageHandler(this,
             SLOT(handleNdefMessage(QNdefMessage,QNearFieldTarget*)));

Automatically launching NDEF message handlers is supported on Android.

NFC on Linux

The Linux NFC project provides software to support NFC on Linux platforms. The neard daemon will allow access to the supported hardware via DBus interfaces. QtNfc requires neard version 0.14 which can be built from source or installed via the appropriate Linux package manager. Not all API features are currently supported. To allow QtNfc to access the DBus interfaces the neard daemon has to be running. In case of problems debug output can be enabled by enabling categorized logging for 'qt.nfc.neard'.

Member Type Documentation

enum class QNearFieldManager::AdapterState

This enum describes the different states a NFC adapter can have.

ConstantValueDescription
QNearFieldManager::AdapterState::Offline1The nfc adapter is offline.
QNearFieldManager::AdapterState::TurningOn2The nfc adapter is turning on.
QNearFieldManager::AdapterState::Online3The nfc adapter is online.
QNearFieldManager::AdapterState::TurningOff4The nfc adapter is turning off.

This enum was introduced or modified in Qt 5.12.

enum QNearFieldManager::TargetAccessMode

This enum describes the different access modes an application can have.

ConstantValueDescription
QNearFieldManager::NoTargetAccess0x00The application cannot access NFC capabilities.
QNearFieldManager::NdefReadTargetAccess0x01The application can read NDEF messages from targets by calling QNearFieldTarget::readNdefMessages().
QNearFieldManager::NdefWriteTargetAccess0x02The application can write NDEF messages to targets by calling QNearFieldTarget::writeNdefMessages().
QNearFieldManager::TagTypeSpecificTargetAccess0x04The application can access targets using raw commands by calling QNearFieldTarget::sendCommand().