-
Notifications
You must be signed in to change notification settings - Fork 10
/
NFCManager.h
82 lines (64 loc) · 2.01 KB
/
NFCManager.h
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
#ifndef NFCMANAGER_H
#define NFCMANAGER_H
#include <QObject>
#include <qqml.h>
#include <QtNfc/qnearfieldtarget.h>
class QNearFieldManager;
class QNdefMessage;
class QNdefNfcTextRecord;
struct Record {
Q_GADGET
Q_PROPERTY(int seconds MEMBER seconds)
Q_PROPERTY(QString dishName MEMBER dishName)
public:
int seconds = 0;
QString dishName = "";
bool parseNdefMessage(const QNdefNfcTextRecord &record);
QNdefMessage generateNdefMessage() const;
};
Q_DECLARE_METATYPE(Record)
class NFCManager : public QObject
{
Q_OBJECT
Q_PROPERTY(bool hasTagInRange READ hasTagInRange NOTIFY hasTagInRangeChanged)
Q_PROPERTY(ActionType actionType READ actionType WRITE setActionType NOTIFY actionTypeChanged)
Q_PROPERTY(Record record READ record NOTIFY recordChanged)
QML_ELEMENT
public:
explicit NFCManager(QObject *parent = nullptr);
enum ActionType
{
None = 0,
Reading,
Writing
};
Q_ENUM(ActionType)
bool hasTagInRange() const;
ActionType actionType() const;
Record record() const;
public slots:
void startReading();
void stopDetecting();
void saveRecord(const QString &dishName, int seconds);
signals:
void hasTagInRangeChanged(bool hasTagInRange);
void actionTypeChanged(ActionType actionType);
void recordChanged(const Record &record);
void wroteSuccessfully();
void nfcError(const QString &error);
private slots:
void setActionType(ActionType actionType);
void setHasTagInRange(bool hasTagInRange);
void onTargetDetected(QNearFieldTarget *target);
void onTargetLost(QNearFieldTarget *target);
void onNdefMessageRead(const QNdefMessage &message);
void onNdefMessageWritten();
void handleTargetError(QNearFieldTarget::Error error, const QNearFieldTarget::RequestId &id);
private:
bool m_hasTagInRange = false;
ActionType m_actionType = ActionType::None;
Record m_record;
QNearFieldManager *m_manager;
QNearFieldTarget::RequestId m_request;
};
#endif // NFCMANAGER_H