1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Copyright (C) 2013 BlackBerry Limited. All rights reserved.
5 ** Contact: http://www.qt.io/licensing/
6 **
7 ** This file is part of the QtBluetooth module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** You may use this file under the terms of the BSD license as follows:
11 **
12 ** "Redistribution and use in source and binary forms, with or without
13 ** modification, are permitted provided that the following conditions are
14 ** met:
15 ** * Redistributions of source code must retain the above copyright
16 ** notice, this list of conditions and the following disclaimer.
17 ** * Redistributions in binary form must reproduce the above copyright
18 ** notice, this list of conditions and the following disclaimer in
19 ** the documentation and/or other materials provided with the
20 ** distribution.
21 ** * Neither the name of The Qt Company Ltd nor the names of its
22 ** contributors may be used to endorse or promote products derived
23 ** from this software without specific prior written permission.
24 **
25 **
26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "service.h"
43
44 #include <qbluetoothaddress.h>
45 #include <qbluetoothservicediscoveryagent.h>
46 #include <qbluetoothserviceinfo.h>
47 #include <qbluetoothlocaldevice.h>
48 #include <qbluetoothuuid.h>
49
50
ServiceDiscoveryDialog(const QString & name,const QBluetoothAddress & address,QWidget * parent)51 ServiceDiscoveryDialog::ServiceDiscoveryDialog(const QString &name,
52 const QBluetoothAddress &address, QWidget *parent)
53 : QDialog(parent), ui(new Ui_ServiceDiscovery)
54 {
55 ui->setupUi(this);
56
57 //Using default Bluetooth adapter
58 QBluetoothLocalDevice localDevice;
59 QBluetoothAddress adapterAddress = localDevice.address();
60
61 /*
62 * In case of multiple Bluetooth adapters it is possible to
63 * set which adapter will be used by providing MAC Address.
64 * Example code:
65 *
66 * QBluetoothAddress adapterAddress("XX:XX:XX:XX:XX:XX");
67 * discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress);
68 */
69
70 discoveryAgent = new QBluetoothServiceDiscoveryAgent(adapterAddress);
71
72 discoveryAgent->setRemoteAddress(address);
73
74 setWindowTitle(name);
75
76 connect(discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
77 this, SLOT(addService(QBluetoothServiceInfo)));
78 connect(discoveryAgent, SIGNAL(finished()), ui->status, SLOT(hide()));
79
80 discoveryAgent->start();
81 }
82
~ServiceDiscoveryDialog()83 ServiceDiscoveryDialog::~ServiceDiscoveryDialog()
84 {
85 delete discoveryAgent;
86 delete ui;
87 }
88
addService(const QBluetoothServiceInfo & info)89 void ServiceDiscoveryDialog::addService(const QBluetoothServiceInfo &info)
90 {
91 if (info.serviceName().isEmpty())
92 return;
93
94 QString line = info.serviceName();
95 if (!info.serviceDescription().isEmpty())
96 line.append("\n\t" + info.serviceDescription());
97 if (!info.serviceProvider().isEmpty())
98 line.append("\n\t" + info.serviceProvider());
99
100 ui->list->addItem(line);
101 }
102