-
Notifications
You must be signed in to change notification settings - Fork 4
/
bluezadapter.cpp
125 lines (94 loc) · 3.73 KB
/
bluezadapter.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
#include "bluezadapter.h"
#include <QtXml/QtXml>
BluezAdapter::BluezAdapter(QObject *parent) : QObject(parent)
{
qDebug() << "BluezAdapter::BluezAdapter";
}
void BluezAdapter::setAdapterPath(const QString &path)
{
qDebug() << "BluezAdapter::setAdapterPath:" << path;
m_adapterPath = path;
m_deviceInterface = new QDBusInterface("org.bluez", m_adapterPath, "org.bluez.Adapter1", QDBusConnection::systemBus());
}
void BluezAdapter::startDiscovery()
{
qDebug() << "BluezAdapter::startDiscovery";
if (!m_deviceInterface && !m_adapterPath.isEmpty()) {
setAdapterPath(m_adapterPath);
}
QDBusMessage reply = m_deviceInterface->call("StartDiscovery");
qDebug() << "BluezAdapter::StartDiscovery:" << reply.errorMessage();
}
void BluezAdapter::stopDiscovery()
{
qDebug() << "BluezAdapter::stopDiscovery";
if (!m_deviceInterface && !m_adapterPath.isEmpty()) {
setAdapterPath(m_adapterPath);
}
QDBusMessage reply = m_deviceInterface->call("StopDiscovery");
qDebug() << "BluezAdapter::StopDiscovery:" << reply.errorMessage();
}
QString BluezAdapter::matchDevice(const QString &match)
{
qDebug() << "BluezAdapter::matchDevice:looking for:" << match;
QDBusInterface adapterIntro("org.bluez", m_adapterPath, "org.freedesktop.DBus.Introspectable", QDBusConnection::systemBus(), 0);
QDBusReply<QString> xml = adapterIntro.call("Introspect");
QDomDocument doc;
doc.setContent(xml.value());
QDomNodeList nodes = doc.elementsByTagName("node");
for (int x = 0; x < nodes.count(); x++)
{
QDomElement node = nodes.at(x).toElement();
QString nodeName = node.attribute("name");
if (nodeName.startsWith("dev_")) {
QString path = m_adapterPath + "/" + nodeName;
QDBusInterface devInterface("org.bluez", path, "org.bluez.Device1", QDBusConnection::systemBus(), 0);
qDebug() << "Found:" << devInterface.property("Name");
if (devInterface.property("Name").toString().contains(match)) {
return path;
}
}
}
return QString();
}
bool BluezAdapter::deviceIsValid(const QString &match)
{
QDBusInterface adapterIntro("org.bluez", m_adapterPath, "org.freedesktop.DBus.Introspectable", QDBusConnection::systemBus(), 0);
QDBusReply<QString> xml = adapterIntro.call("Introspect");
QDomDocument doc;
doc.setContent(xml.value());
QDomNodeList nodes = doc.elementsByTagName("node");
for (int x = 0; x < nodes.count(); x++)
{
QDomElement node = nodes.at(x).toElement();
QString nodeName = node.attribute("name");
if (nodeName.startsWith("dev_")) {
QString path = m_adapterPath + "/" + nodeName;
if (path == match)
return true;
}
}
return false;
}
QString BluezAdapter::deviceName(const QString &search)
{
qDebug() << "BluezAdapter::deviceName:looking for:" << search;
QDBusInterface adapterIntro("org.bluez", m_adapterPath, "org.freedesktop.DBus.Introspectable", QDBusConnection::systemBus(), 0);
QDBusReply<QString> xml = adapterIntro.call("Introspect");
QDomDocument doc;
doc.setContent(xml.value());
QDomNodeList nodes = doc.elementsByTagName("node");
for (int x = 0; x < nodes.count(); x++)
{
QDomElement node = nodes.at(x).toElement();
QString nodeName = node.attribute("name");
if (nodeName.startsWith("dev_")) {
QString path = m_adapterPath + "/" + nodeName;
if (path == search) {
QDBusInterface devInterface("org.bluez", path, "org.bluez.Device1", QDBusConnection::systemBus(), 0);
return devInterface.property("Name").toString();
}
}
}
return QString();
}