Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
doc_src_qtbluetooth.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
5#include <QtBluetooth/QBluetoothLocalDevice>
7#include <QtCore/QCoreApplication>
8#include <QtCore/QDebug>
9#include <QtCore/QFile>
10#include <QtCore/QObject>
11#include <QtBluetooth/QBluetoothDeviceDiscoveryAgent>
12#include <QtBluetooth/QBluetoothServiceDiscoveryAgent>
13
14#include <QtBluetooth/QLowEnergyController>
15#include <QtBluetooth/QLowEnergyService>
16#include <QtBluetooth/QLowEnergyCharacteristic>
17
21
22class MyClass : public QObject
23{
25public:
27 void localDevice();
30 void objectPush();
31 void btleSharedData();
33
34public slots:
36 void serviceDiscovered(const QBluetoothServiceInfo &service);
38};
39
43QString localDeviceName;
44
45// Check if Bluetooth is available on this device
46if (localDevice.isValid()) {
47
48 // Turn Bluetooth on
49 localDevice.powerOn();
50
51 // Read local device name
52 localDeviceName = localDevice.name();
53
54 // Make it visible to others
56
57 // Get connected devices
58 QList<QBluetoothAddress> remotes;
59 remotes = localDevice.connectedDevices();
60}
62
63
64}
65
68{
69
70 // Create a discovery agent and connect to its signals
74
75 // Start a discovery
76 discoveryAgent->start();
77
78 //...
79}
80
81// In your local slot, read information about the found devices
83{
84 qDebug() << "Found new device:" << device.name() << '(' << device.address().toString() << ')';
85}
87
90{
91
92 // Create a discovery agent and connect to its signals
96
97 // Start a discovery
98 discoveryAgent->start();
99
100 //...
101}
102
103// In your local slot, read information about the found devices
105{
106 qDebug() << "Found new service:" << service.serviceName()
107 << '(' << service.device().address().toString() << ')';
108}
110
114
116{
117 QBluetoothDeviceInfo remoteDevice;
118
120 QLowEnergyService *first, *second;
121 QLowEnergyController control(remoteDevice);
122 control.connectToDevice();
123
124 // waiting for connection
125
129 Q_ASSERT(first->state() == second->state());
130
131 first->discoverDetails();
132
134 Q_ASSERT(first->state() == second->state());
136}
137
139{
140 QBluetoothDeviceInfo remoteDevice;
141 QLowEnergyService *service;
142 QLowEnergyController *control = QLowEnergyController::createCentral(remoteDevice, this);
143 control->connectToDevice();
144
145
147 if (!service)
148 return;
149
150 service->discoverDetails();
151
152 //... wait until discovered
153
155 //PreCondition: service details already discovered
156 QLowEnergyCharacteristic batteryLevel = service->characteristic(
158 if (!batteryLevel.isValid())
159 return;
160
161 QLowEnergyDescriptor notification = batteryLevel.descriptor(
163 if (!notification.isValid())
164 return;
165
166 // establish hook into notifications
169
170 // enable notification
171 service->writeDescriptor(notification, QByteArray::fromHex("0100"));
172
173 // disable notification
174 //service->writeDescriptor(notification, QByteArray::fromHex("0000"));
175
176 // wait until descriptorWritten() signal is emitted
177 // to confirm successful write
179}
180
181
182
183int main(int argc, char** argv)
184{
185 QCoreApplication app(argc, argv);
186 MyClass cl;
187
188 return app.exec();
189}
190
191#include "doc_src_qtbluetooth.moc"
IOBluetoothDevice * device
void enableCharNotifications()
void characteristicChanged(const QLowEnergyCharacteristic &, const QByteArray &)
[service_discovery]
void startDeviceDiscovery()
[device_discovery]
void serviceDiscovered(const QBluetoothServiceInfo &service)
void deviceDiscovered(const QBluetoothDeviceInfo &device)
void startServiceDiscovery()
[device_discovery]
void objectPush()
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
void start()
Starts Bluetooth device discovery, if it is not already started.
\inmodule QtBluetooth
\inmodule QtBluetooth
void start(DiscoveryMode mode=MinimalDiscovery)
Starts service discovery.
\inmodule QtBluetooth
\inmodule QtCore
Definition qbytearray.h:57
static QByteArray fromHex(const QByteArray &hexEncoded)
Returns a decoded copy of the hex encoded array hexEncoded.
\inmodule QtCore
\inmodule QtBluetooth
void connectToDevice()
Connects to the remote Bluetooth Low Energy device.
static QLowEnergyController * createCentral(const QBluetoothDeviceInfo &remoteDevice, QObject *parent=nullptr)
Returns a new object of this class that is in the \l CentralRole and has the parent object parent.
QLowEnergyService * createServiceObject(const QBluetoothUuid &service, QObject *parent=nullptr)
Creates an instance of the service represented by serviceUuid.
\inmodule QtBluetooth
bool isValid() const
Returns true if the QLowEnergyDescriptor object is valid, otherwise returns false.
\inmodule QtBluetooth
QLowEnergyService::ServiceState state() const
Returns the current state of the service.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
int main()
[0]
#define qDebug
[1]
Definition qlogging.h:164
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLint first
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_OBJECT
#define slots
QApplication app(argc, argv)
[0]