1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtBluetooth module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of The Qt Company Ltd nor the names of its
21 ** contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include "device.h"
42 #include "service.h"
43
44 #include <qbluetoothaddress.h>
45 #include <qbluetoothdevicediscoveryagent.h>
46 #include <qbluetoothlocaldevice.h>
47 #include <QMenu>
48 #include <QDebug>
49
DeviceDiscoveryDialog(QWidget * parent)50 DeviceDiscoveryDialog::DeviceDiscoveryDialog(QWidget *parent)
51 : QDialog(parent), localDevice(new QBluetoothLocalDevice),
52 ui(new Ui_DeviceDiscovery)
53 {
54 ui->setupUi(this);
55
56 /*
57 * In case of multiple Bluetooth adapters it is possible to set adapter
58 * which will be used. Example code:
59 *
60 * QBluetoothAddress address("XX:XX:XX:XX:XX:XX");
61 * discoveryAgent = new QBluetoothDeviceDiscoveryAgent(address);
62 *
63 **/
64
65 discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
66
67 connect(ui->inquiryType, SIGNAL(toggled(bool)), this, SLOT(setGeneralUnlimited(bool)));
68 connect(ui->scan, SIGNAL(clicked()), this, SLOT(startScan()));
69
70 connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
71 this, SLOT(addDevice(QBluetoothDeviceInfo)));
72 connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));
73
74 connect(ui->list, SIGNAL(itemActivated(QListWidgetItem*)),
75 this, SLOT(itemActivated(QListWidgetItem*)));
76
77 connect(localDevice, SIGNAL(hostModeStateChanged(QBluetoothLocalDevice::HostMode)),
78 this, SLOT(hostModeStateChanged(QBluetoothLocalDevice::HostMode)));
79
80 hostModeStateChanged(localDevice->hostMode());
81 // add context menu for devices to be able to pair device
82 ui->list->setContextMenuPolicy(Qt::CustomContextMenu);
83 connect(ui->list, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(displayPairingMenu(QPoint)));
84 connect(localDevice, SIGNAL(pairingFinished(QBluetoothAddress,QBluetoothLocalDevice::Pairing))
85 , this, SLOT(pairingDone(QBluetoothAddress,QBluetoothLocalDevice::Pairing)));
86
87 }
88
~DeviceDiscoveryDialog()89 DeviceDiscoveryDialog::~DeviceDiscoveryDialog()
90 {
91 delete discoveryAgent;
92 }
93
addDevice(const QBluetoothDeviceInfo & info)94 void DeviceDiscoveryDialog::addDevice(const QBluetoothDeviceInfo &info)
95 {
96 QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
97 QList<QListWidgetItem *> items = ui->list->findItems(label, Qt::MatchExactly);
98 if (items.empty()) {
99 QListWidgetItem *item = new QListWidgetItem(label);
100 QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(info.address());
101 if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired )
102 item->setTextColor(QColor(Qt::green));
103 else
104 item->setTextColor(QColor(Qt::black));
105
106 ui->list->addItem(item);
107 }
108
109 }
110
startScan()111 void DeviceDiscoveryDialog::startScan()
112 {
113 discoveryAgent->start();
114 ui->scan->setEnabled(false);
115 ui->inquiryType->setEnabled(false);
116 }
117
scanFinished()118 void DeviceDiscoveryDialog::scanFinished()
119 {
120 ui->scan->setEnabled(true);
121 ui->inquiryType->setEnabled(true);
122 }
123
setGeneralUnlimited(bool unlimited)124 void DeviceDiscoveryDialog::setGeneralUnlimited(bool unlimited)
125 {
126 if (unlimited)
127 discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry);
128 else
129 discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::LimitedInquiry);
130 }
131
itemActivated(QListWidgetItem * item)132 void DeviceDiscoveryDialog::itemActivated(QListWidgetItem *item)
133 {
134 QString text = item->text();
135
136 int index = text.indexOf(' ');
137
138 if (index == -1)
139 return;
140
141 QBluetoothAddress address(text.left(index));
142 QString name(text.mid(index + 1));
143
144 ServiceDiscoveryDialog d(name, address);
145 d.exec();
146 }
147
on_discoverable_clicked(bool clicked)148 void DeviceDiscoveryDialog::on_discoverable_clicked(bool clicked)
149 {
150 if (clicked)
151 localDevice->setHostMode(QBluetoothLocalDevice::HostDiscoverable);
152 else
153 localDevice->setHostMode(QBluetoothLocalDevice::HostConnectable);
154 }
155
on_power_clicked(bool clicked)156 void DeviceDiscoveryDialog::on_power_clicked(bool clicked)
157 {
158 if (clicked)
159 localDevice->powerOn();
160 else
161 localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff);
162 }
163
hostModeStateChanged(QBluetoothLocalDevice::HostMode mode)164 void DeviceDiscoveryDialog::hostModeStateChanged(QBluetoothLocalDevice::HostMode mode)
165 {
166 if (mode != QBluetoothLocalDevice::HostPoweredOff)
167 ui->power->setChecked(true);
168 else
169 ui->power->setChecked( false);
170
171 if (mode == QBluetoothLocalDevice::HostDiscoverable)
172 ui->discoverable->setChecked(true);
173 else
174 ui->discoverable->setChecked(false);
175
176 bool on = !(mode == QBluetoothLocalDevice::HostPoweredOff);
177
178
179 ui->scan->setEnabled(on);
180 ui->discoverable->setEnabled(on);
181 }
displayPairingMenu(const QPoint & pos)182 void DeviceDiscoveryDialog::displayPairingMenu(const QPoint &pos)
183 {
184 if (ui->list->count() == 0)
185 return;
186 QMenu menu(this);
187 QAction *pairAction = menu.addAction("Pair");
188 QAction *removePairAction = menu.addAction("Remove Pairing");
189 QAction *chosenAction = menu.exec(ui->list->viewport()->mapToGlobal(pos));
190 QListWidgetItem *currentItem = ui->list->currentItem();
191
192 QString text = currentItem->text();
193 int index = text.indexOf(' ');
194 if (index == -1)
195 return;
196
197 QBluetoothAddress address (text.left(index));
198 if (chosenAction == pairAction) {
199 localDevice->requestPairing(address, QBluetoothLocalDevice::Paired);
200 } else if (chosenAction == removePairAction) {
201 localDevice->requestPairing(address, QBluetoothLocalDevice::Unpaired);
202 }
203 }
pairingDone(const QBluetoothAddress & address,QBluetoothLocalDevice::Pairing pairing)204 void DeviceDiscoveryDialog::pairingDone(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
205 {
206 QList<QListWidgetItem *> items = ui->list->findItems(address.toString(), Qt::MatchContains);
207
208 if (pairing == QBluetoothLocalDevice::Paired || pairing == QBluetoothLocalDevice::AuthorizedPaired ) {
209 for (int var = 0; var < items.count(); ++var) {
210 QListWidgetItem *item = items.at(var);
211 item->setTextColor(QColor(Qt::green));
212 }
213 } else {
214 for (int var = 0; var < items.count(); ++var) {
215 QListWidgetItem *item = items.at(var);
216 item->setTextColor(QColor(Qt::red));
217 }
218 }
219 }
220