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