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

source code of qtconnectivity/examples/bluetooth/btscanner/service.cpp