NDEF Editor Example#

An example of reading and writing NFC Data Exchange Format (NDEF) messages to NFC Forum Tags.

The NDEF Editor example reads and writes NFC Data Exchange Format (NDEF) messages to NFC Forum Tags. NDEF messages can be composed by adding records of supported types. Additionally, NDEF messages can be loaded/saved from/into a file located in the file system of the device where the application is running.

../_images/ndefeditor.png

NFC Tag detection#

The MainWindow class is able to detect if an NFC Tag is in the range for read/write operations. It can also detect if the connection has been lost. This is achieved by connecting the MainWindow class private handlers to the targetDetected and targetLost signals.

m_manager = QNearFieldManager(self)
connect(m_manager, QNearFieldManager.targetDetected,
        self, MainWindow::targetDetected)
connect(m_manager, QNearFieldManager.targetLost,
        self, MainWindow::targetLost)

When Read or Write button is pressed, the detection of NFC tags is started by calling the startTargetDetection method.

m_manager.startTargetDetection(QNearFieldTarget.NdefAccess)

Once the target is detected, the MainWindow connects the following signals to its internal private slots: ndefMessageRead , NdefReadError , requestCompleted , NdefWriteError and error .

def targetDetected(self, target):

    switch (m_touchAction) {
    NoAction: = case()
        break
    ReadNdef: = case()
        connect(target, QNearFieldTarget.ndefMessageRead, self, MainWindow.ndefMessageRead)
        connect(target, QNearFieldTarget.error, self, MainWindow.targetError)
        m_request = target.readNdefMessages()
        if (not m_request.isValid()) // cannot read messages
            targetError(QNearFieldTarget.NdefReadError, m_request)
        break
    WriteNdef: = case()
        connect(target, QNearFieldTarget.requestCompleted, self, MainWindow.ndefMessageWritten)
        connect(target, QNearFieldTarget.error, self, MainWindow.targetError)
        m_request = target.writeNdefMessages(QList<QNdefMessage>() << ndefMessage())
        if (not m_request.isValid()) // cannot write messages
            targetError(QNearFieldTarget.NdefWriteError, m_request)
        break

If during the process of reading or writing to an NFC Tag the connection is lost, the MainWindow reacts to this event by scheduling the target deletion (using deleteLater ).

def targetLost(self, target):

    target.deleteLater()

Record creation#

The main window of the NDEF Editor example manages the composition and creation of NFC records. The UI contains a QScrollArea , which is used to dynamically add the record editors. The following methods of the MainWindow class provide an interface towards each of the record editing classes managing the different types of records.

def addNfcTextRecord():
def addNfcUriRecord():
def addMimeImageRecord():
def addEmptyRecord():

The following sections explain each of the record editing classes.

Record editing classes#

TextRecordEditor#

The TextRecordEditor is a QWidget that allows to edit the contents of the NDEF Text record. A new instance of this class is created for each text record.

class TextRecordEditor(QWidget):

    Q_OBJECT
# public
    TextRecordEditor = explicit(QWidget parent = 0)
    ~TextRecordEditor()
    def setRecord(textRecord):
    record = QNdefNfcTextRecord()
# private
    Ui::TextRecordEditor ui

UriRecordEditor#

The UriRecordEditor is a QWidget that allows to edit the contents of the NDEF Uri record. A new instance of this class is created for each uri record.

class UriRecordEditor(QWidget):

    Q_OBJECT
# public
    UriRecordEditor = explicit(QWidget parent = 0)
    ~UriRecordEditor()
    def setRecord(uriRecord):
    record = QNdefNfcUriRecord()
# private
    Ui::UriRecordEditor ui

MimeImageRecordEditor#

The MimeImageRecordEditor is a QWidget that allows to edit the contents of the NDEF MIME record. In this example MIME record can be used to store an icon. A new instance of this class is created for each MIME record.

class MimeImageRecordEditor(QWidget):

    Q_OBJECT
# public
    MimeImageRecordEditor = explicit(QWidget parent = 0)
    ~MimeImageRecordEditor()
    def setRecord(record):
    record = QNdefRecord()
slots: = public()
    def handleScreenOrientationChange(orientation):
protected:
    def resizeEvent(arg__0):
# private
    def updatePixmap():
    Ui::MimeImageRecordEditor ui
    m_record = QNdefRecord()
    m_pixmap = QPixmap()
    m_imageSelected = False()
    m_screenRotated = False()
slots: = private()
    def on_mimeImageOpen_clicked():

Running the Example#

To run the example from Qt Creator , open the Welcome mode and select the example from Examples. For more information, visit Building and Running an Example.

See also

Qt NFC

Example project @ code.qt.io