-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt_rfcomm.cpp
145 lines (125 loc) · 3.69 KB
/
bt_rfcomm.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <QBluetoothUuid>
#include <QtCore/qmetaobject.h>
#include <QBluetoothAddress>
#include "bt_rfcomm.h"
BTRfcomm::BTRfcomm(QObject *parent) :
QObject(parent),
m_socket(new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this)),
m_data()
{
connect(m_socket, SIGNAL(readyRead()),
this, SLOT(onSocketReadyRead()));
connect(m_socket, SIGNAL(connected()),
this, SLOT(onSocketConnected()));
connect(m_socket, SIGNAL(disconnected()),
this, SLOT(onSocketDisconnected()));
connect(m_socket, SIGNAL(error(QBluetoothSocket::SocketError)),
this, SLOT(onSocketError(QBluetoothSocket::SocketError)));
}
BTRfcomm::~BTRfcomm()
{
delete m_discoveryAgent;
delete m_socket;
}
void BTRfcomm::startDiscovery()
{
if (m_discoveryAgent)
{
delete m_discoveryAgent;
}
m_discoveryAgent = new QBluetoothServiceDiscoveryAgent(this);
m_discoveryAgent->setUuidFilter(QBluetoothUuid(QString(
"00001101-0000-1000-8000-00805F9B34FB"
)));
connect(m_discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
this, SLOT(onServiceDiscovered(QBluetoothServiceInfo)));
connect(m_discoveryAgent, SIGNAL(finished()),
this, SLOT(onServiceDiscoveryFinished()));
connect(m_discoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)),
this, SLOT(onServiceDiscoveryError(QBluetoothServiceDiscoveryAgent::Error)));
m_discoveryAgent->start();
}
void BTRfcomm::stopDiscovery()
{
if (m_discoveryAgent)
{
m_discoveryAgent->stop();
}
}
void BTRfcomm::onServiceDiscovered(const QBluetoothServiceInfo &service)
{
emit discovered(
service.device().name(),
service.device().address().toString()
);
}
void BTRfcomm::onServiceDiscoveryFinished()
{
emit discoveryFinished();
}
void BTRfcomm::onServiceDiscoveryError(QBluetoothServiceDiscoveryAgent::Error errorType)
{
QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothServiceDiscoveryAgent::Error>();
QString errorString = metaEnum.valueToKey(errorType);
emit discoveryError(errorString);
}
bool BTRfcomm::getServiceDiscoveryStatus()
{
if (m_discoveryAgent)
{
return m_discoveryAgent->isActive();
}
return false;
}
void BTRfcomm::connectDevice(QString address)
{
m_socket->connectToService(
QBluetoothAddress(address),
QBluetoothUuid(QString("00001101-0000-1000-8000-00805F9B34FB"))
);
}
void BTRfcomm::disconnectDevice()
{
m_socket->disconnectFromService();
}
void BTRfcomm::onSocketConnected()
{
emit connected(m_socket->peerAddress().toString());
}
void BTRfcomm::onSocketDisconnected()
{
emit disconnected();
}
void BTRfcomm::onSocketError(QBluetoothSocket::SocketError errorType)
{
QMetaEnum metaEnum = QMetaEnum::fromType<QBluetoothSocket::SocketError>();
QString errorString = metaEnum.valueToKey(errorType);
emit connectionError(errorString);
}
void BTRfcomm::onSocketReadyRead()
{
/* readLine() does not work as expected.
while (m_socket->canReadLine())
{
QByteArray line = m_socket->readLine();
emit lineReceived(QString::fromUtf8(line.constData(), line.length()));
}
*/
QByteArray data = m_socket->readAll();
m_data += QString::fromUtf8(data.constData());
while (m_data.contains("\r\n"))
{
int lineEndIndex = m_data.indexOf("\r\n");
QString line = m_data.left(lineEndIndex);
m_data.remove(0, lineEndIndex + 2);
emit lineReceived(line);
}
}
void BTRfcomm::sendLine(QString line)
{
if (m_socket->state() == QBluetoothSocket::ConnectedState)
{
QByteArray text = line.toUtf8() + '\r';
m_socket->write(text);
}
}