-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcameradiscovery.cpp
156 lines (125 loc) · 3.92 KB
/
cameradiscovery.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
146
147
148
149
150
151
152
153
154
155
156
#include "cameradiscovery.h"
static const QBluetoothUuid BmdCameraService("291D567A-6D75-11E6-8B77-86F30CA893D3");
CameraDiscovery::CameraDiscovery(QObject *parent)
: QObject{parent}
{
m_discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
m_discoveryAgent->setLowEnergyDiscoveryTimeout(5000);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &CameraDiscovery::addCameraDevice);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceUpdated, this, &CameraDiscovery::addCameraDevice);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred, this, &CameraDiscovery::deviceScanError);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &CameraDiscovery::deviceScanFinished);
connect(m_discoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &CameraDiscovery::deviceScanFinished);
}
CameraDiscovery::~CameraDiscovery()
{
clearDevices();
}
void CameraDiscovery::clearDevices()
{
m_cameras.clear();
qDeleteAll(m_devices);
m_devices.clear();
emit countChanged();
emit devicesUpdated();
}
void CameraDiscovery::startDeviceDiscovery()
{
clearDevices();
if (m_discoveryAgent->isActive())
return;
m_discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
if (m_discoveryAgent->isActive()) {
m_discovering = true;
emit discoveringChanged();
}
emit discoveryStart();
}
void CameraDiscovery::stopDeviceDiscovery()
{
if (m_discoveryAgent->isActive())
m_discoveryAgent->stop();
}
bool CameraDiscovery::discovering()
{
return m_discovering;
}
void CameraDiscovery::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
{
emit controllerErrorChanged();
m_discovering = false;
emit discoveringChanged();
}
/**
* @brief CameraDevice::addCameraDevice
* @param info
*
* Store found BLE device if it is a BM Camera, by first checking if the CameraService is available.
* If not, ignore the BLE device.
*
*/
void CameraDiscovery::addCameraDevice(const QBluetoothDeviceInfo &info)
{
// BMPCC is BLE only so ignore anything else
if (info.coreConfigurations()!=QBluetoothDeviceInfo::LowEnergyCoreConfiguration)
return;
// Check if device has BM service 291d567a-6d75-11e6-8b77-86f30ca893d3 ?
auto uuids=info.serviceUuids();
if (!uuids.contains(BmdCameraService)) {
return;
}
qDebug() << "Found BM camera service!";
qDebug() << info.address() << info.name() << info.rssi() << info.isCached();
// #ifndef Q_OS_WIN32
// if (info.rssi()==0) {
// qDebug("Ignoring off-line device");
// return;
// }
// #endif
QBluetoothDeviceInfo *device=new QBluetoothDeviceInfo(info);
if (m_devices.contains(info.address().toString())) {
QBluetoothDeviceInfo *tmp=m_devices.take(info.address().toString());
delete tmp;
}
m_devices.insert(info.address().toString(), device);
emit countChanged();
emit devicesUpdated();
}
/**
* @brief CameraDevice::deviceScanFinished
*
*
*/
void CameraDiscovery::deviceScanFinished()
{
m_discovering = false;
QBluetoothDeviceInfo *info;
foreach (info, m_devices) {
QVariantMap dev;
dev.insert("address", info->address().toString());
dev.insert("name", info->name());
dev.insert("rssi", info->rssi());
m_cameras.append(dev);
}
emit discoveringChanged();
emit discoveryStop(m_cameras.count());
}
QString CameraDiscovery::getDefaultDevice()
{
if (!m_cameras.isEmpty()) {
return "";
}
return "";
}
QBluetoothDeviceInfo *CameraDiscovery::getBluetoothDevice(QString address)
{
return m_devices.value(address);
}
QVariantList CameraDiscovery::getDevices()
{
return m_cameras;
}
int CameraDiscovery::count() const
{
return m_devices.size();
}