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 demonstration applications 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#ifndef DEVICE_H
52#define DEVICE_H
53
54#include <qbluetoothlocaldevice.h>
55#include <QObject>
56#include <QVariant>
57#include <QList>
58#include <QBluetoothServiceDiscoveryAgent>
59#include <QBluetoothDeviceDiscoveryAgent>
60#include <QLowEnergyController>
61#include <QBluetoothServiceInfo>
62#include "deviceinfo.h"
63#include "serviceinfo.h"
64#include "characteristicinfo.h"
65
66QT_FORWARD_DECLARE_CLASS (QBluetoothDeviceInfo)
67QT_FORWARD_DECLARE_CLASS (QBluetoothServiceInfo)
68
69class Device: public QObject
70{
71 Q_OBJECT
72 Q_PROPERTY(QVariant devicesList READ getDevices NOTIFY devicesUpdated)
73 Q_PROPERTY(QVariant servicesList READ getServices NOTIFY servicesUpdated)
74 Q_PROPERTY(QVariant characteristicList READ getCharacteristics NOTIFY characteristicsUpdated)
75 Q_PROPERTY(QString update READ getUpdate WRITE setUpdate NOTIFY updateChanged)
76 Q_PROPERTY(bool useRandomAddress READ isRandomAddress WRITE setRandomAddress NOTIFY randomAddressChanged)
77 Q_PROPERTY(bool state READ state NOTIFY stateChanged)
78 Q_PROPERTY(bool controllerError READ hasControllerError)
79public:
80 Device();
81 ~Device();
82 QVariant getDevices();
83 QVariant getServices();
84 QVariant getCharacteristics();
85 QString getUpdate();
86 bool state();
87 bool hasControllerError() const;
88
89 bool isRandomAddress() const;
90 void setRandomAddress(bool newValue);
91
92public slots:
93 void startDeviceDiscovery();
94 void scanServices(const QString &address);
95
96 void connectToService(const QString &uuid);
97 void disconnectFromDevice();
98
99private slots:
100 // QBluetoothDeviceDiscoveryAgent related
101 void addDevice(const QBluetoothDeviceInfo&);
102 void deviceScanFinished();
103 void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error);
104
105 // QLowEnergyController realted
106 void addLowEnergyService(const QBluetoothUuid &uuid);
107 void deviceConnected();
108 void errorReceived(QLowEnergyController::Error);
109 void serviceScanDone();
110 void deviceDisconnected();
111
112 // QLowEnergyService related
113 void serviceDetailsDiscovered(QLowEnergyService::ServiceState newState);
114
115Q_SIGNALS:
116 void devicesUpdated();
117 void servicesUpdated();
118 void characteristicsUpdated();
119 void updateChanged();
120 void stateChanged();
121 void disconnected();
122 void randomAddressChanged();
123
124private:
125 void setUpdate(const QString &message);
126 QBluetoothDeviceDiscoveryAgent *discoveryAgent;
127 DeviceInfo currentDevice;
128 QList<QObject *> devices;
129 QList<QObject *> m_services;
130 QList<QObject *> m_characteristics;
131 QString m_previousAddress;
132 QString m_message;
133 bool connected = false;
134 QLowEnergyController *controller = nullptr;
135 bool m_deviceScanState = false;
136 bool randomAddress = false;
137};
138
139#endif // DEVICE_H
140

source code of qtconnectivity/examples/bluetooth/lowenergyscanner/device.h