IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Annotated URL Example

An example showing reading from formatted NFC Data Exchange Format (NDEF) messages.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Annotated URL Example▲

The Annotated URL example displays the contents of specifically formatted NFC Data Exchange Format (NDEF) messages read from an NFC Tag. The NDEF message should contain a URI record, an optional image/* MIME record, and one or more localized Text records.

This is the initial state of the example:

Image non disponible

In this example the NFC Tag used contains a text record. The UI gets updated accordingly to:

Image non disponible

AnnotatedUrl Class Definition▲

The AnnotatedUrl class wraps the QNearFieldManager, the class providing the NFC Tag detection functionality. NDEF messages are read by the https:// and forwarded to a handler contained in the AnnotatedUrl class. After parsing the NDEF message the class emits the signal AnnotatedUrl::annotatedUrl(const https:// &url, const https:// &title, const https:// &pixmap). The UI reacts to the signal displaying the contents of the NDEF message.

 
Sélectionnez
class AnnotatedUrl : public QObject
{
    Q_OBJECT

public:
    explicit AnnotatedUrl(QObject *parent = 0);
    ~AnnotatedUrl();

signals:
    void annotatedUrl(const QUrl &url, const QString &title, const QPixmap &pixmap);

public slots:
    void targetDetected(QNearFieldTarget *target);
    void targetLost(QNearFieldTarget *target);
    void handleMessage(const QNdefMessage &message, QNearFieldTarget *target);
    void handlePolledNdefMessage(QNdefMessage message);
private:
    QNearFieldManager *manager;
};

AnnotatedUrl Handler Implementation▲

NFC messages read by the QNearFieldManager are forwarded to AnnotatedUrl::handleMessage. The callback signature details can be read in https://.

 
Sélectionnez
void AnnotatedUrl::handleMessage(const QNdefMessage &message, QNearFieldTarget *target)
{

Because NFC messages are composed of several NDEF records, looping through all of the records allows the extraction of the 3 parameters to be displayed in the UI: the Uri, the Title and the Pixmap:

 
Sélectionnez
    for (const QNdefRecord &record : message) {
        if (record.isRecordType<QNdefNfcTextRecord>()) {
            QNdefNfcTextRecord textRecord(record);

            title = textRecord.text();
            QLocale locale(textRecord.locale());
        } else if (record.isRecordType<QNdefNfcUriRecord>()) {
            QNdefNfcUriRecord uriRecord(record);

            url = uriRecord.uri();
        } else if (record.typeNameFormat() == QNdefRecord::Mime &&
                   record.type().startsWith("image/")) {
            pixmap = QPixmap::fromImage(QImage::fromData(record.payload()));
        }

Finally after having extracted the parameters of the NFC message the corresponding signal is emitted so that the UI can handle it.

 
Sélectionnez
    }

    emit annotatedUrl(url, title, pixmap);
}

Running the Example▲

To run the example from http://, open the Welcome mode and select the example from Examples. For more information, visit http://.

List of Files▲

Files:

List of Files▲

  • annotatedurl/annotatedurl.cpp

  • annotatedurl/annotatedurl.h

  • annotatedurl/annotatedurl.pro

  • annotatedurl/main.cpp

  • annotatedurl/mainwindow.cpp

  • annotatedurl/mainwindow.h

  • annotatedurl/mainwindow.ui

See Also▲

See also https://

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.