The QNearFieldManager class provides access to notifications for NFC events. 更多...
头: | #include <QNearFieldManager> |
qmake: | QT += nfc |
Since: | Qt 5.2 |
继承: | QObject |
enum | AdapterState { Offline, TurningOn, Online, TurningOff } |
enum | TargetAccessMode { NoTargetAccess, NdefReadTargetAccess, NdefWriteTargetAccess, TagTypeSpecificTargetAccess } |
flags | TargetAccessModes |
QNearFieldManager (QObject * parent = nullptr) | |
virtual | ~QNearFieldManager () |
bool | isAvailable () const |
bool | isSupported () const |
int | registerNdefMessageHandler (QObject * object , const char * method ) |
int | registerNdefMessageHandler (QNdefRecord::TypeNameFormat typeNameFormat , const QByteArray & type , QObject * object , const char * method ) |
int | registerNdefMessageHandler (const QNdefFilter & filter , QObject * object , const char * method ) |
void | setTargetAccessModes (QNearFieldManager::TargetAccessModes accessModes ) |
bool | startTargetDetection () |
void | stopTargetDetection () |
QNearFieldManager::TargetAccessModes | targetAccessModes () const |
bool | unregisterNdefMessageHandler (int handlerId ) |
void | adapterStateChanged (QNearFieldManager::AdapterState state ) |
void | targetDetected (QNearFieldTarget * target ) |
void | targetLost (QNearFieldTarget * target ) |
const QMetaObject | staticMetaObject |
The QNearFieldManager class provides access to notifications for NFC events.
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 () 函数。
Applications can connect to the targetDetected () 和 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 () 函数。
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 () 函数:
QNearFieldManager *manager = new QNearFieldManager(this); manager->registerNdefMessageHandler(this, SLOT(handleNdefMessage(QNdefMessage,QNearFieldTarget*)));
Automatically launching NDEF message handlers is supported on Android .
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'.
This enum describes the different states a NFC adapter can have.
常量 | 值 | 描述 |
---|---|---|
QNearFieldManager::Offline
|
1
|
The nfc adapter is offline. |
QNearFieldManager::TurningOn
|
2
|
The nfc adapter is turning on. |
QNearFieldManager::Online
|
3
|
The nfc adapter is online. |
QNearFieldManager::TurningOff
|
4
|
The nfc adapter is turning off. |
该枚举在 Qt 5.12 引入或被修改。
This enum describes the different access modes an application can have.
常量 | 值 | 描述 |
---|---|---|
QNearFieldManager::NoTargetAccess
|
0x00
|
The application cannot access NFC capabilities. |
QNearFieldManager::NdefReadTargetAccess
|
0x01
|
The application can read NDEF messages from targets by calling QNearFieldTarget::readNdefMessages (). |
QNearFieldManager::NdefWriteTargetAccess
|
0x02
|
The application can write NDEF messages to targets by calling QNearFieldTarget::writeNdefMessages (). |
QNearFieldManager::TagTypeSpecificTargetAccess
|
0x04
|
The application can access targets using raw commands by calling QNearFieldTarget::sendCommand (). |
The TargetAccessModes type is a typedef for QFlags <TargetAccessMode>. It stores an OR combination of TargetAccessMode values.
Constructs a new near field manager with parent .
[虚拟]
QNearFieldManager::
~QNearFieldManager
()
Destroys the near field manager.
[signal]
void
QNearFieldManager::
adapterStateChanged
(
QNearFieldManager::AdapterState
state
)
This signal is emitted whenever the state of the NFC adapter changed.
注意: Currently, this signal is only emitted on Android.
该函数在 Qt 5.12 引入。
返回
true
if the device has a NFC adapter and it is turned on; otherwise returns
false
.
另请参阅 isSupported ().
返回
true
if the underlying device has a NFC adapter; otherwise returns
false
.
该函数在 Qt 5.12 引入。
另请参阅 isAvailable ().
注册 object to receive notifications on method when a tag has been detected and has an NDEF message that matches a pre-registered message format. The method on object should have the prototype 'void targetDetected (const QNdefMessage &message, QNearFieldTarget *target)'.
Returns an identifier, which can be used to unregister the handler, on success; otherwise returns -1.
This function is used to register a QNearFieldManager instance to receive notifications when a NDEF message matching a pre-registered message format is received. See the section on Automatically launching NDEF message handlers .
注意: The target parameter of method may not be available on all platforms, in which case target 会为 0。
注册 object to receive notifications on method when a tag has been detected and has an NDEF record that matches typeNameFormat and type 。 method on object should have the prototype 'void targetDetected (const QNdefMessage &message, QNearFieldTarget *target)'.
Returns an identifier, which can be used to unregister the handler, on success; otherwise returns -1.
注意: The target parameter of method may not be available on all platforms, in which case target 会为 0。
注意: On platforms using neard registering message handlers is not supported.
注册 object to receive notifications on method when a tag has been detected and has an NDEF message that matches filter is detected. The method on object should have the prototype 'void targetDetected (const QNdefMessage &message, QNearFieldTarget *target)'.
Returns an identifier, which can be used to unregister the handler, on success; otherwise returns -1.
注意: The target parameter of method may not be available on all platforms, in which case target 会为 0。
Sets the requested target access modes to accessModes .
另请参阅 targetAccessModes ().
Starts detecting targets and returns true if target detection is successfully started; otherwise returns false. Causes the targetDetected () signal to be emitted when a target is within proximity.
注意: For platforms using neard: target detection will stop as soon as a tag has been detected.
另请参阅 stopTargetDetection ().
Stops detecting targets. The targetDetected () signal will no longer be emitted until another call to startTargetDetection () is made.
Returns current requested target access modes.
另请参阅 setTargetAccessModes ().
[signal]
void
QNearFieldManager::
targetDetected
(
QNearFieldTarget
*
target
)
This signal is emitted whenever a target is detected. The target parameter represents the detected target.
This signal will be emitted for all detected targets.
QNearFieldManager maintains ownership of target , however, it will not be destroyed until the QNearFieldManager destructor is called. Ownership may be transferred by calling setParent ().
不要删除 target from the slot connected to this signal, instead call deleteLater ().
注意: that if target is deleted before it moves out of proximity the targetLost () signal will not be emitted.
另请参阅 targetLost ().
[signal]
void
QNearFieldManager::
targetLost
(
QNearFieldTarget
*
target
)
This signal is emitted whenever a target moves out of proximity. The target parameter represents the lost target.
不要删除 target from the slot connected to this signal, instead use deleteLater ().
另请参阅 QNearFieldTarget::disconnected ().
Unregisters the target detect handler identified by handlerId .
返回 true 当成功时;否则返回 false。