Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
4
//! [include]
5
#
include
<
QtBluetooth
/
QBluetoothLocalDevice
>
6
//! [include]
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
18
//! [namespace]
19
QT_USE_NAMESPACE
20
//! [namespace]
21
22
class
MyClass :
public
QObject
23
{
24
Q_OBJECT
25
public
:
26
MyClass() : QObject() {}
27
void
localDevice();
28
void
startDeviceDiscovery();
29
void
startServiceDiscovery();
30
void
objectPush();
31
void
btleSharedData();
32
void
enableCharNotifications();
33
34
public
slots:
35
void
deviceDiscovered(
const
QBluetoothDeviceInfo &device);
36
void
serviceDiscovered(
const
QBluetoothServiceInfo &service);
37
void
characteristicChanged(
const
QLowEnergyCharacteristic& ,
const
QByteArray&);
38
};
39
40
void
MyClass
::localDevice() {
41
//! [turningon]
42
QBluetoothLocalDevice localDevice;
43
QString localDeviceName;
44
45
// Check if Bluetooth is available on this device
46
if
(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
55
localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
56
57
// Get connected devices
58
QList<QBluetoothAddress> remotes;
59
remotes = localDevice.connectedDevices();
60
}
61
//! [turningon]
62
63
64
}
65
66
//! [device_discovery]
67
void
MyClass
::startDeviceDiscovery()
68
{
69
70
// Create a discovery agent and connect to its signals
71
QBluetoothDeviceDiscoveryAgent *discoveryAgent =
new
QBluetoothDeviceDiscoveryAgent(
this
);
72
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
73
this
, SLOT(deviceDiscovered(QBluetoothDeviceInfo)));
74
75
// Start a discovery
76
discoveryAgent->start();
77
78
//...
79
}
80
81
// In your local slot, read information about the found devices
82
void
MyClass
::deviceDiscovered(
const
QBluetoothDeviceInfo &device)
83
{
84
qDebug() <<
"Found new device:"
<< device.name() <<
'('
<< device.address().toString() <<
')'
;
85
}
86
//! [device_discovery]
87
88
//! [service_discovery]
89
void
MyClass
::startServiceDiscovery()
90
{
91
92
// Create a discovery agent and connect to its signals
93
QBluetoothServiceDiscoveryAgent *discoveryAgent =
new
QBluetoothServiceDiscoveryAgent(
this
);
94
connect(discoveryAgent, SIGNAL(serviceDiscovered(QBluetoothServiceInfo)),
95
this
, SLOT(serviceDiscovered(QBluetoothServiceInfo)));
96
97
// Start a discovery
98
discoveryAgent->start();
99
100
//...
101
}
102
103
// In your local slot, read information about the found devices
104
void
MyClass
::serviceDiscovered(
const
QBluetoothServiceInfo &service)
105
{
106
qDebug() <<
"Found new service:"
<< service.serviceName()
107
<<
'('
<< service.device().address().toString() <<
')'
;
108
}
109
//! [service_discovery]
110
111
void
MyClass
::characteristicChanged(
const
QLowEnergyCharacteristic &,
const
QByteArray &)
112
{
113
}
114
115
void
MyClass
::btleSharedData()
116
{
117
QBluetoothDeviceInfo remoteDevice;
118
119
//! [data_share_qlowenergyservice]
120
QLowEnergyService *first, *second;
121
QLowEnergyController control(remoteDevice);
122
control.connectToDevice();
123
124
// waiting for connection
125
126
first = control.createServiceObject(QBluetoothUuid::ServiceClassUuid::BatteryService);
127
second = control.createServiceObject(QBluetoothUuid::ServiceClassUuid::BatteryService);
128
Q_ASSERT(first->state() == QLowEnergyService::RemoteService);
129
Q_ASSERT(first->state() == second->state());
130
131
first->discoverDetails();
132
133
Q_ASSERT(first->state() == QLowEnergyService::RemoteServiceDiscovering);
134
Q_ASSERT(first->state() == second->state());
135
//! [data_share_qlowenergyservice]
136
}
137
138
void
MyClass
::enableCharNotifications()
139
{
140
QBluetoothDeviceInfo remoteDevice;
141
QLowEnergyService *service;
142
QLowEnergyController *control = QLowEnergyController::createCentral(remoteDevice,
this
);
143
control->connectToDevice();
144
145
146
service = control->createServiceObject(QBluetoothUuid::ServiceClassUuid::BatteryService,
this
);
147
if
(!service)
148
return
;
149
150
service->discoverDetails();
151
152
//... wait until discovered
153
154
//! [enable_btle_notifications]
155
//PreCondition: service details already discovered
156
QLowEnergyCharacteristic batteryLevel = service->characteristic(
157
QBluetoothUuid::CharacteristicType::BatteryLevel);
158
if
(!batteryLevel.isValid())
159
return
;
160
161
QLowEnergyDescriptor notification = batteryLevel.descriptor(
162
QBluetoothUuid::DescriptorType::ClientCharacteristicConfiguration);
163
if
(!notification.isValid())
164
return
;
165
166
// establish hook into notifications
167
connect(service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic,QByteArray)),
168
this
, SLOT(characteristicChanged(QLowEnergyCharacteristic,QByteArray)));
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
178
//! [enable_btle_notifications]
179
}
180
181
182
183
int
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"
MyClass
[5]
Definition
doc_src_properties.cpp:68
main
int main(int argc, char *argv[])
[ctor_close]
Definition
src_corelib_animation_qpropertyanimation.cpp:100
qtconnectivity
src
bluetooth
doc
snippets
doc_src_qtbluetooth.cpp
Generated on
for Qt by
1.18.0