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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Copyright (C) 2013 BlackBerry Limited. All rights reserved.
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qdeclarativebluetoothdiscoverymodel_p.h"
#include <QPixmap>
#include <QtCore/QLoggingCategory>
#include <QtBluetooth/QBluetoothDeviceInfo>
#include <QtBluetooth/QBluetoothAddress>
#include "qdeclarativebluetoothservice_p.h"
/*!
\qmltype BluetoothDiscoveryModel
\instantiates QDeclarativeBluetoothDiscoveryModel
\inqmlmodule QtBluetooth
\since 5.2
\brief Enables searching for the Bluetooth devices and services in
range.
BluetoothDiscoveryModel provides a model of connectable services. The
contents of the model can be filtered by UUID allowing discovery to be
limited to a single service such as a game.
The model roles provided by BluetoothDiscoveryModel are
\c service, \c name, \c remoteAddress and \c deviceName. The meaning of the roles
changes based on the current \l discoveryMode.
\table
\header
\li Model role
\li Device Discovery
\li Service Discovery
\row
\li \c name
\li The device's name and address.
\li The service name and the name of the device offering the service. If the device name is empty the devices address will be used.
\row
\li \c deviceName
\li The name of the device.
\li The name of the device offering the service.
\row
\li \c service
\li The role is undefined in this mode.
\li The \l BluetoothService object describing the discovered service.
\row
\li \c remoteAddress
\li The address of the found device.
\li The address of the device offering the service.
\endtable
\sa QBluetoothServiceDiscoveryAgent
*/
Q_DECLARE_LOGGING_CATEGORY(QT_BT_QML)
class QDeclarativeBluetoothDiscoveryModelPrivate
{
public:
QDeclarativeBluetoothDiscoveryModelPrivate()
: m_serviceAgent(0),
m_deviceAgent(0),
m_error(QDeclarativeBluetoothDiscoveryModel::NoError),
m_discoveryMode(QDeclarativeBluetoothDiscoveryModel::MinimalServiceDiscovery),
m_running(false),
m_runningRequested(true),
m_componentCompleted(false)
{
}
~QDeclarativeBluetoothDiscoveryModelPrivate()
{
if (m_deviceAgent)
delete m_deviceAgent;
if (m_serviceAgent)
delete m_serviceAgent;
qDeleteAll(m_services);
}
QBluetoothServiceDiscoveryAgent *m_serviceAgent;
QBluetoothDeviceDiscoveryAgent *m_deviceAgent;
QDeclarativeBluetoothDiscoveryModel::Error m_error;
QList<QDeclarativeBluetoothService *> m_services;
QList<QBluetoothDeviceInfo> m_devices;
QDeclarativeBluetoothDiscoveryModel::DiscoveryMode m_discoveryMode;
QString m_uuid;
bool m_running;
bool m_runningRequested;
bool m_componentCompleted;
QString m_remoteAddress;
};
QDeclarativeBluetoothDiscoveryModel::QDeclarativeBluetoothDiscoveryModel(QObject *parent) :
QAbstractListModel(parent),
d(new QDeclarativeBluetoothDiscoveryModelPrivate)
{
QHash<int, QByteArray> roleNames;
roleNames = QAbstractItemModel::roleNames();
roleNames.insert(Name, "name");
roleNames.insert(ServiceRole, "service");
roleNames.insert(RemoteAddress, "remoteAddress");
roleNames.insert(DeviceName, "deviceName");
setRoleNames(roleNames);
}
QDeclarativeBluetoothDiscoveryModel::~QDeclarativeBluetoothDiscoveryModel()
{
delete d;
}
void QDeclarativeBluetoothDiscoveryModel::componentComplete()
{
d->m_componentCompleted = true;
if (d->m_runningRequested)
setRunning(true);
}
void QDeclarativeBluetoothDiscoveryModel::errorDiscovery(QBluetoothServiceDiscoveryAgent::Error error)
{
d->m_error = static_cast<QDeclarativeBluetoothDiscoveryModel::Error>(error);
emit errorChanged();
}
void QDeclarativeBluetoothDiscoveryModel::errorDeviceDiscovery(QBluetoothDeviceDiscoveryAgent::Error error)
{
d->m_error = static_cast<QDeclarativeBluetoothDiscoveryModel::Error>(error);
emit errorChanged();
//QBluetoothDeviceDiscoveryAgent::finished() signal is not emitted in case of an error
//Note that this behavior is different from QBluetoothServiceDiscoveryAgent.
//This reset the models running flag.
setRunning(false);
}
void QDeclarativeBluetoothDiscoveryModel::clearModel()
{
beginResetModel();
qDeleteAll(d->m_services);
d->m_services.clear();
d->m_devices.clear();
endResetModel();
}
/*!
\qmlproperty enumeration BluetoothDiscoveryModel::error
This property holds the last error reported during discovery.
\table
\header \li Property \li Description
\row \li \c BluetoothDiscoveryModel.NoError
\li No error occurred.
\row \li \c BluetoothDiscoveryModel.InputOutputError
\li An IO failure occurred during device discovery
\row \li \c BluetoothDiscoveryModel.PoweredOffError
\li The bluetooth device is not powered on.
\row \li \c BluetoothDiscoveryModel.UnknownError
\li An unknown error occurred.
\endtable
This property is read-only.
*/
QDeclarativeBluetoothDiscoveryModel::Error QDeclarativeBluetoothDiscoveryModel::error() const
{
return d->m_error;
}
int QDeclarativeBluetoothDiscoveryModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
if (discoveryMode() == DeviceDiscovery)
return d->m_devices.count();
else
return d->m_services.count();
}
QVariant QDeclarativeBluetoothDiscoveryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0)
return QVariant();
if (discoveryMode() != DeviceDiscovery) {
if (index.row() >= d->m_services.count()){
qCWarning(QT_BT_QML) << "index out of bounds";
return QVariant();
}
QDeclarativeBluetoothService *service = d->m_services.value(index.row());
switch (role) {
case Name: {
QString label = service->deviceName();
if (label.isEmpty())
label += service->deviceAddress();
else
label+= QStringLiteral(":");
label += QStringLiteral(" ") + service->serviceName();
return label;
}
case ServiceRole:
return QVariant::fromValue(service);
case DeviceName:
return service->deviceName();
case RemoteAddress:
return service->deviceAddress();
}
} else {
if (index.row() >= d->m_devices.count()) {
qCWarning(QT_BT_QML) << "index out of bounds";
return QVariant();
}
QBluetoothDeviceInfo device = d->m_devices.value(index.row());
switch (role) {
case Name:
return (device.name() + QStringLiteral(" (") + device.address().toString() + QStringLiteral(")"));
case ServiceRole:
break;
case DeviceName:
return device.name();
case RemoteAddress:
return device.address().toString();
}
}
return QVariant();
}
/*!
\qmlsignal BluetoothDiscoveryModel::serviceDiscovered(BluetoothService service)
This handler is called when a new service is discovered. The \a service
parameter contains the service details.
\sa BluetoothService
*/
void QDeclarativeBluetoothDiscoveryModel::serviceDiscovered(const QBluetoothServiceInfo &service)
{
//qDebug() << "service discovered";
QDeclarativeBluetoothService *bs = new QDeclarativeBluetoothService(service, this);
QDeclarativeBluetoothService *current = 0;
for (int i = 0; i < d->m_services.count(); i++) {
current = d->m_services.at(i);
if (bs->deviceAddress() == current->deviceAddress()
&& bs->serviceName() == current->serviceName()
&& bs->serviceUuid() == current->serviceUuid()) {
delete bs;
return;
}
}
beginInsertRows(QModelIndex(),d->m_services.count(), d->m_services.count());
d->m_services.append(bs);
endInsertRows();
emit serviceDiscovered(bs);
}
/*!
\qmlsignal BluetoothDiscoveryModel::deviceDiscovered(string device)
This handler is called when a new device is discovered. \a device contains
the Bluetooth address of the discovred device.
*/
void QDeclarativeBluetoothDiscoveryModel::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
//qDebug() << "Device discovered" << device.address().toString() << device.name();
beginInsertRows(QModelIndex(),d->m_devices.count(), d->m_devices.count());
d->m_devices.append(device);
endInsertRows();
emit deviceDiscovered(device.address().toString());
}
void QDeclarativeBluetoothDiscoveryModel::finishedDiscovery()
{
setRunning(false);
}
/*!
\qmlproperty enumeration BluetoothDiscoveryModel::discoveryMode
Sets the discovery mode. The discovery mode has to be set before the discovery is started
\table
\header \li Property \li Description
\row \li \c BluetoothDiscoveryModel.FullServiceDiscovery
\li Starts a full discovery of all services of all devices in range.
\row \li \c BluetoothDiscoveryModel.MinimalServiceDiscovery
\li (Default) Starts a minimal discovery of all services of all devices in range. A minimal discovery is
faster but only guarantees the device and UUID information to be correct.
\row \li \c BluetoothDiscoveryModel.DeviceDiscovery
\li Discovers only devices in range. The service role will be 0 for any model item.
\endtable
*/
QDeclarativeBluetoothDiscoveryModel::DiscoveryMode QDeclarativeBluetoothDiscoveryModel::discoveryMode() const
{
return d->m_discoveryMode;
}
void QDeclarativeBluetoothDiscoveryModel::setDiscoveryMode(DiscoveryMode discovery)
{
d->m_discoveryMode = discovery;
emit discoveryModeChanged();
}
/*!
\qmlproperty bool BluetoothDiscoveryModel::running
This property starts or stops discovery. A restart of the discovery process
requires setting this property to \c false and subsequemtly to \c true again.
*/
bool QDeclarativeBluetoothDiscoveryModel::running() const
{
return d->m_running;
}
void QDeclarativeBluetoothDiscoveryModel::setRunning(bool running)
{
if (!d->m_componentCompleted) {
d->m_runningRequested = running;
return;
}
if (d->m_running == running)
return;
d->m_running = running;
if (!running) {
if (d->m_deviceAgent)
d->m_deviceAgent->stop();
if (d->m_serviceAgent)
d->m_serviceAgent->stop();
} else {
clearModel();
d->m_error = NoError;
if (d->m_discoveryMode == DeviceDiscovery) {
if (!d->m_deviceAgent) {
d->m_deviceAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(d->m_deviceAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
connect(d->m_deviceAgent, SIGNAL(finished()), this, SLOT(finishedDiscovery()));
connect(d->m_deviceAgent, SIGNAL(canceled()), this, SLOT(finishedDiscovery()));
connect(d->m_deviceAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(errorDeviceDiscovery(QBluetoothDeviceDiscoveryAgent::Error)));
}
d->m_deviceAgent->start();
} else {
if (!d->m_serviceAgent) {
d->m_serviceAgent = new QBluetoothServiceDiscoveryAgent(this);
connect(d->m_serviceAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)), this, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
connect(d->m_serviceAgent, SIGNAL(finished()), this, SLOT(finishedDiscovery()));
connect(d->m_serviceAgent, SIGNAL(canceled()), this, SLOT(finishedDiscovery()));
connect(d->m_serviceAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)), this, SLOT(errorDiscovery(QBluetoothServiceDiscoveryAgent::Error)));
}
d->m_serviceAgent->setRemoteAddress(QBluetoothAddress(d->m_remoteAddress));
if (!d->m_uuid.isEmpty())
d->m_serviceAgent->setUuidFilter(QBluetoothUuid(d->m_uuid));
if (discoveryMode() == FullServiceDiscovery) {
//qDebug() << "Full Discovery";
d->m_serviceAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
} else {
//qDebug() << "Minimal Discovery";
d->m_serviceAgent->start(QBluetoothServiceDiscoveryAgent::MinimalDiscovery);
}
}
}
Q_EMIT runningChanged();
}
/*!
\qmlproperty string BluetoothDiscoveryModel::uuidFilter
This property holds an optional UUID filter. A UUID can be used to return only
matching services. 16 bit, 32 bit or 128 bit UUIDs can be used. The string format
is same as the format of QUuid.
\sa QBluetoothUuid
\sa QUuid
*/
QString QDeclarativeBluetoothDiscoveryModel::uuidFilter() const
{
return d->m_uuid;
}
void QDeclarativeBluetoothDiscoveryModel::setUuidFilter(QString uuid)
{
if (uuid == d->m_uuid)
return;
QBluetoothUuid qbuuid(uuid);
if (qbuuid.isNull()) {
qCWarning(QT_BT_QML) << "Invalid UUID providded " << uuid;
return;
}
d->m_uuid = uuid;
emit uuidFilterChanged();
}
/*!
\qmlproperty string BluetoothDiscoveryModel::remoteAddress
This property holds an optional bluetooth address for a remote bluetooth device.
Only services on this remote device will be discovered. It has no effect if
an invalid bluetooth address was set or if the property was set after the discovery
was started.
The property is ignored if device discovery is selected.
*/
QString QDeclarativeBluetoothDiscoveryModel::remoteAddress()
{
return d->m_remoteAddress;
}
void QDeclarativeBluetoothDiscoveryModel::setRemoteAddress(QString address)
{
d->m_remoteAddress = address;
Q_EMIT remoteAddressChanged();
}
|