-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfc.cpp
88 lines (77 loc) · 2.53 KB
/
nfc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "nfc.hpp"
#include <QDebug>
#include <QtNfc>
#include <QGuiApplication>
nfc* nfc::instance = NULL;
nfc& nfc::getInstance()
{
if (nfc::instance == NULL)
nfc::instance = new nfc(QGuiApplication::instance());
return *(nfc::instance);
}
nfc::nfc(QObject *parent) : QObject(parent)
{
nfc::instance = this;
manager = new QNearFieldManager(this);
if (!manager->isAvailable()) {
qWarning() << "NFC not available";
return;
}
filter.appendRecord(QNdefRecord::Mime, "nfc-test/msg");
manager->setTargetAccessModes(QNearFieldManager::NdefReadTargetAccess); // doesn't help
manager->registerNdefMessageHandler(filter, this, SLOT(handleMessage(QNdefMessage,QNearFieldTarget*)));
qDebug() << __PRETTY_FUNCTION__ << "NFC available (test style)";
connect(manager, SIGNAL(targetDetected(QNearFieldTarget*)),
this, SLOT(targetDetected(QNearFieldTarget*)));
connect(manager, SIGNAL(targetLost(QNearFieldTarget*)),
this, SLOT(targetLost(QNearFieldTarget*)));
}
nfc::~nfc()
{
manager->stopTargetDetection();
delete manager;
manager = NULL;
}
void nfc::startNfc()
{
qDebug() << "Started nfc detection";
manager->startTargetDetection();
}
void nfc::stopNfc()
{
qDebug() << __PRETTY_FUNCTION__ << "Nfc stop detection";
manager->stopTargetDetection();
}
void nfc::targetDetected(QNearFieldTarget *target)
{
qDebug() << __PRETTY_FUNCTION__ << "target detected:" << target;
if (!target)
return;
connect(target, SIGNAL(ndefMessageRead(QNdefMessage)),
this, SLOT(handlePolledNdefMessage(QNdefMessage)), Qt::UniqueConnection);
target->readNdefMessages();
}
void nfc::targetLost(QNearFieldTarget *target)
{
qDebug() << __PRETTY_FUNCTION__ << "target lost:" << target;
if (target)
target->deleteLater();
}
void nfc::handlePolledNdefMessage(QNdefMessage message)
{
QNearFieldTarget *target = qobject_cast<QNearFieldTarget *>(sender());
qDebug() << __PRETTY_FUNCTION__ << "New event";
handleMessage(message, target);
qDebug() << __PRETTY_FUNCTION__ << "New event end";
}
void nfc::handleMessage(const QNdefMessage &message, QNearFieldTarget *target)
{
Q_UNUSED(target);
qDebug() << "message : " << message.toByteArray() << message.size() << target;
foreach (const QNdefRecord &record, message) {
qDebug() << "NFC id=>" << record.id();
qDebug() << "NFC type=>" << record.type();
qDebug() << "NFC payload=>" << record.payload();
qDebug() << "NFC typenameformat=>" << record.typeNameFormat();
}
}