| 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 examples 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 "devicefinder.h" |
| 52 | #include "devicehandler.h" |
| 53 | #include "deviceinfo.h" |
| 54 | |
| 55 | DeviceFinder::DeviceFinder(DeviceHandler *handler, QObject *parent): |
| 56 | BluetoothBaseClass(parent), |
| 57 | m_deviceHandler(handler) |
| 58 | { |
| 59 | //! [devicediscovery-1] |
| 60 | m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); |
| 61 | m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000); |
| 62 | |
| 63 | connect(sender: m_deviceDiscoveryAgent, signal: &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, receiver: this, slot: &DeviceFinder::addDevice); |
| 64 | connect(sender: m_deviceDiscoveryAgent, signal: static_cast<void (QBluetoothDeviceDiscoveryAgent::*)(QBluetoothDeviceDiscoveryAgent::Error)>(&QBluetoothDeviceDiscoveryAgent::error), |
| 65 | receiver: this, slot: &DeviceFinder::scanError); |
| 66 | |
| 67 | connect(sender: m_deviceDiscoveryAgent, signal: &QBluetoothDeviceDiscoveryAgent::finished, receiver: this, slot: &DeviceFinder::scanFinished); |
| 68 | connect(sender: m_deviceDiscoveryAgent, signal: &QBluetoothDeviceDiscoveryAgent::canceled, receiver: this, slot: &DeviceFinder::scanFinished); |
| 69 | //! [devicediscovery-1] |
| 70 | |
| 71 | |
| 72 | #ifdef SIMULATOR |
| 73 | m_demoTimer.setSingleShot(true); |
| 74 | m_demoTimer.setInterval(2000); |
| 75 | connect(&m_demoTimer, &QTimer::timeout, this, &DeviceFinder::scanFinished); |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | DeviceFinder::~DeviceFinder() |
| 80 | { |
| 81 | qDeleteAll(c: m_devices); |
| 82 | m_devices.clear(); |
| 83 | } |
| 84 | |
| 85 | void DeviceFinder::startSearch() |
| 86 | { |
| 87 | clearMessages(); |
| 88 | m_deviceHandler->setDevice(nullptr); |
| 89 | qDeleteAll(c: m_devices); |
| 90 | m_devices.clear(); |
| 91 | |
| 92 | emit devicesChanged(); |
| 93 | |
| 94 | #ifdef SIMULATOR |
| 95 | m_demoTimer.start(); |
| 96 | #else |
| 97 | //! [devicediscovery-2] |
| 98 | m_deviceDiscoveryAgent->start(method: QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); |
| 99 | //! [devicediscovery-2] |
| 100 | #endif |
| 101 | emit scanningChanged(); |
| 102 | setInfo(tr(s: "Scanning for devices..." )); |
| 103 | } |
| 104 | |
| 105 | //! [devicediscovery-3] |
| 106 | void DeviceFinder::addDevice(const QBluetoothDeviceInfo &device) |
| 107 | { |
| 108 | // If device is LowEnergy-device, add it to the list |
| 109 | if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) { |
| 110 | m_devices.append(t: new DeviceInfo(device)); |
| 111 | setInfo(tr(s: "Low Energy device found. Scanning more..." )); |
| 112 | //! [devicediscovery-3] |
| 113 | emit devicesChanged(); |
| 114 | //! [devicediscovery-4] |
| 115 | } |
| 116 | //... |
| 117 | } |
| 118 | //! [devicediscovery-4] |
| 119 | |
| 120 | void DeviceFinder::scanError(QBluetoothDeviceDiscoveryAgent::Error error) |
| 121 | { |
| 122 | if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError) |
| 123 | setError(tr(s: "The Bluetooth adaptor is powered off." )); |
| 124 | else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError) |
| 125 | setError(tr(s: "Writing or reading from the device resulted in an error." )); |
| 126 | else |
| 127 | setError(tr(s: "An unknown error has occurred." )); |
| 128 | } |
| 129 | |
| 130 | void DeviceFinder::scanFinished() |
| 131 | { |
| 132 | #ifdef SIMULATOR |
| 133 | // Only for testing |
| 134 | for (int i = 0; i < 4; i++) |
| 135 | m_devices.append(new DeviceInfo(QBluetoothDeviceInfo())); |
| 136 | #endif |
| 137 | |
| 138 | if (m_devices.isEmpty()) |
| 139 | setError(tr(s: "No Low Energy devices found." )); |
| 140 | else |
| 141 | setInfo(tr(s: "Scanning done." )); |
| 142 | |
| 143 | emit scanningChanged(); |
| 144 | emit devicesChanged(); |
| 145 | } |
| 146 | |
| 147 | void DeviceFinder::connectToService(const QString &address) |
| 148 | { |
| 149 | m_deviceDiscoveryAgent->stop(); |
| 150 | |
| 151 | DeviceInfo *currentDevice = nullptr; |
| 152 | for (QObject *entry : qAsConst(t&: m_devices)) { |
| 153 | auto device = qobject_cast<DeviceInfo *>(object: entry); |
| 154 | if (device && device->getAddress() == address ) { |
| 155 | currentDevice = device; |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (currentDevice) |
| 161 | m_deviceHandler->setDevice(currentDevice); |
| 162 | |
| 163 | clearMessages(); |
| 164 | } |
| 165 | |
| 166 | bool DeviceFinder::scanning() const |
| 167 | { |
| 168 | #ifdef SIMULATOR |
| 169 | return m_demoTimer.isActive(); |
| 170 | #else |
| 171 | return m_deviceDiscoveryAgent->isActive(); |
| 172 | #endif |
| 173 | } |
| 174 | |
| 175 | QVariant DeviceFinder::devices() |
| 176 | { |
| 177 | return QVariant::fromValue(value: m_devices); |
| 178 | } |
| 179 | |