11#include <QtBluetooth/QBluetoothLocalDevice>
12#include <QtCore/QLoggingCategory>
15#if QT_CONFIG(bluez) && !defined(QT_BLUEZ_NO_BTLE)
16#include "bluez/bluez5_helper_p.h"
17#include "qlowenergycontroller_bluezdbus_p.h"
18#include "qlowenergycontroller_bluez_p.h"
19#elif defined(QT_ANDROID_BLUETOOTH)
20#include "qlowenergycontroller_android_p.h"
21#include "android/androidutils_p.h"
22#elif defined(QT_WINRT_BLUETOOTH)
23#include "qtbluetoothglobal_p.h"
24#include "qlowenergycontroller_winrt_p.h"
25#elif defined(Q_OS_DARWIN)
26#include "qlowenergycontroller_darwin_p.h"
35QT_IMPL_METATYPE_EXTERN_TAGGED(QLowEnergyController::Error, QLowEnergyController__Error)
36QT_IMPL_METATYPE_EXTERN_TAGGED(QLowEnergyController::ControllerState,
37 QLowEnergyController__ControllerState)
38QT_IMPL_METATYPE_EXTERN_TAGGED(QLowEnergyController::RemoteAddressType,
39 QLowEnergyController__RemoteAddressType)
40QT_IMPL_METATYPE_EXTERN_TAGGED(QLowEnergyController::Role, QLowEnergyController__Role)
42Q_DECLARE_LOGGING_CATEGORY(QT_BT)
43#if defined(QT_ANDROID_BLUETOOTH)
44Q_DECLARE_LOGGING_CATEGORY(QT_BT_ANDROID)
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
140
141
142
143
144
145
146
147
148
149
150
151
152
153
156
157
158
159
160
161
162
163
164
165
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
191
192
193
194
195
196
197
198
199
200
201
202
205
206
207
208
209
210
211
212
213
214
215
218
219
220
221
222
223
224
225
226
229
230
231
232
233
234
235
236
237
240
241
242
243
244
245
246
249
250
251
252
253
254
255
256
259
260
261
262
263
264
265
266
267
270
271
272
273
274
275
276
277
278
279
282
283
284
285
286
287
288
289
290
291
294void registerQLowEnergyControllerMetaType()
296 static bool initDone =
false;
298 qRegisterMetaType<QLowEnergyController::ControllerState>();
299 qRegisterMetaType<QLowEnergyController::Error>();
300 qRegisterMetaType<QLowEnergyConnectionParameters>();
301 qRegisterMetaType<QLowEnergyCharacteristic>();
302 qRegisterMetaType<QLowEnergyDescriptor>();
307static QLowEnergyControllerPrivate *privateController(
308 QLowEnergyController::Role role,
309 const QBluetoothAddress& localDevice = QBluetoothAddress{})
311#if QT_CONFIG(bluez) && !defined(QT_BLUEZ_NO_BTLE)
325 QString adapterPathWithPeripheralSupport;
326 if (role == QLowEnergyController::PeripheralRole
327 && bluetoothdVersion() >= QVersionNumber(5, 56)
328 && !qEnvironmentVariableIsSet(
"QT_BLUETOOTH_USE_KERNEL_PERIPHERAL")) {
329 adapterPathWithPeripheralSupport = adapterWithDBusPeripheralInterface(localDevice);
332 if (role == QLowEnergyController::CentralRole
333 && bluetoothdVersion() >= QVersionNumber(5, 42)) {
334 qCDebug(QT_BT) <<
"Using BlueZ LE DBus API for central";
335 return new QLowEnergyControllerPrivateBluezDBus();
336 }
else if (!adapterPathWithPeripheralSupport.isEmpty()) {
337 qCDebug(QT_BT) <<
"Using BlueZ LE DBus API for peripheral";
338 return new QLowEnergyControllerPrivateBluezDBus(adapterPathWithPeripheralSupport);
340 qCDebug(QT_BT) <<
"Using BlueZ kernel ATT interface for"
341 << (role == QLowEnergyController::CentralRole ?
"central" :
"peripheral");
342 return new QLowEnergyControllerPrivateBluez();
344#elif defined(QT_ANDROID_BLUETOOTH)
346 Q_UNUSED(localDevice);
347 return new QLowEnergyControllerPrivateAndroid();
348#elif defined(QT_WINRT_BLUETOOTH)
350 Q_UNUSED(localDevice);
351 return new QLowEnergyControllerPrivateWinRT();
352#elif defined(Q_OS_DARWIN)
354 Q_UNUSED(localDevice);
355 return new QLowEnergyControllerPrivateDarwin();
358 Q_UNUSED(localDevice);
359 return new QLowEnergyControllerPrivateCommon();
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378QLowEnergyController::QLowEnergyController(
379 const QBluetoothDeviceInfo &remoteDevice,
380 const QBluetoothAddress &localDevice,
384 d_ptr = privateController(CentralRole);
386 Q_D(QLowEnergyController);
388 d->role = CentralRole;
389 d->deviceUuid = remoteDevice.deviceUuid();
390 d->remoteDevice = remoteDevice.address();
392 if (localDevice.isNull())
393 d->localAdapter = QBluetoothLocalDevice().address();
395 d->localAdapter = localDevice;
397 d->addressType = QLowEnergyController::PublicAddress;
398 d->remoteName = remoteDevice.name();
403
404
405
406
407
408
409
410
411
412QLowEnergyController *QLowEnergyController::createCentral(
const QBluetoothDeviceInfo &remoteDevice,
415 return new QLowEnergyController(remoteDevice, QBluetoothAddress(), parent);
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434QLowEnergyController *QLowEnergyController::createCentral(
const QBluetoothDeviceInfo &remoteDevice,
435 const QBluetoothAddress &localDevice,
438 return new QLowEnergyController(remoteDevice, localDevice, parent);
443
444
445
446
447
448
449
450
451
452
453QLowEnergyController *QLowEnergyController::createPeripheral(QObject *parent)
455 return new QLowEnergyController(QBluetoothAddress(), parent);
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475QLowEnergyController *QLowEnergyController::createPeripheral(
const QBluetoothAddress &localDevice,
478 return new QLowEnergyController(localDevice, parent);
481QLowEnergyController::QLowEnergyController(
const QBluetoothAddress &localDevice, QObject *parent)
484 d_ptr = privateController(PeripheralRole, localDevice);
486 Q_D(QLowEnergyController);
488 d->role = PeripheralRole;
490 if (localDevice.isNull())
491 d->localAdapter = QBluetoothLocalDevice().address();
493 d->localAdapter = localDevice;
499
500
501QLowEnergyController::~QLowEnergyController()
503 disconnectFromDevice();
508
509
510
511
512
513
514
515
516
517QBluetoothAddress QLowEnergyController::localAddress()
const
519 return d_ptr->localAdapter;
523
524
525
526
527
528
529
530QBluetoothAddress QLowEnergyController::remoteAddress()
const
532 return d_ptr->remoteDevice;
536
537
538
539
540
541
542
543
544
545
546
547QBluetoothUuid QLowEnergyController::remoteDeviceUuid()
const
549 return d_ptr->deviceUuid;
553
554
555
556
557
558QString QLowEnergyController::remoteName()
const
560 return d_ptr->remoteName;
564
565
566
567
568QLowEnergyController::ControllerState QLowEnergyController::state()
const
574
575
576
577
578
579QLowEnergyController::RemoteAddressType QLowEnergyController::remoteAddressType()
const
581 return d_ptr->addressType;
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601void QLowEnergyController::setRemoteAddressType(
602 QLowEnergyController::RemoteAddressType type)
604 d_ptr->addressType = type;
608
609
610
611
612
613
614
615
616
617
618
619
620
621void QLowEnergyController::connectToDevice()
623 Q_D(QLowEnergyController);
625 if (role() != CentralRole) {
626 qCWarning(QT_BT) <<
"Connection can only be established while in central role";
630 if (!d->isValidLocalAdapter()) {
631 qCWarning(QT_BT) <<
"connectToDevice() LE controller has invalid adapter";
632 d->setError(QLowEnergyController::InvalidBluetoothAdapterError);
636 if (state() != QLowEnergyController::UnconnectedState)
639 d->connectToDevice();
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659void QLowEnergyController::disconnectFromDevice()
661 Q_D(QLowEnergyController);
663 if (state() == QLowEnergyController::UnconnectedState)
666 d->invalidateServices();
667 d->disconnectFromDevice();
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686void QLowEnergyController::discoverServices()
688 Q_D(QLowEnergyController);
690 if (d->role != CentralRole) {
691 qCWarning(QT_BT) <<
"Cannot discover services in peripheral role";
694 if (d->state != QLowEnergyController::ConnectedState)
697 d->setState(QLowEnergyController::DiscoveringState);
698 d->discoverServices();
702
703
704
705
706
707
708
709QList<QBluetoothUuid> QLowEnergyController::services()
const
711 return d_ptr->serviceList.keys();
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738QLowEnergyService *QLowEnergyController::createServiceObject(
739 const QBluetoothUuid &serviceUuid, QObject *parent)
741 Q_D(QLowEnergyController);
743 QLowEnergyService *service =
nullptr;
745 ServiceDataMap::const_iterator it = d->serviceList.constFind(serviceUuid);
746 if (it != d->serviceList.constEnd()) {
747 const QSharedPointer<QLowEnergyServicePrivate> &serviceData = it.value();
749 service =
new QLowEnergyService(serviceData, parent);
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776void QLowEnergyController::startAdvertising(
const QLowEnergyAdvertisingParameters ¶meters,
777 const QLowEnergyAdvertisingData &advertisingData,
778 const QLowEnergyAdvertisingData &scanResponseData)
780 Q_D(QLowEnergyController);
781 if (role() != PeripheralRole) {
782 qCWarning(QT_BT) <<
"Cannot start advertising in central role" << state();
785 if (state() != UnconnectedState) {
786 qCWarning(QT_BT) <<
"Cannot start advertising in state" << state();
789 d->startAdvertising(parameters, advertisingData, scanResponseData);
793
794
795
796
797
798
799
800
801void QLowEnergyController::stopAdvertising()
803 Q_D(QLowEnergyController);
804 if (state() != AdvertisingState) {
805 qCDebug(QT_BT) <<
"stopAdvertising called in state" << state();
808 d->stopAdvertising();
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826QLowEnergyService *QLowEnergyController::addService(
const QLowEnergyServiceData &service,
829 if (role() != PeripheralRole) {
830 qCWarning(QT_BT) <<
"Services can only be added in the peripheral role";
833 if (state() != UnconnectedState) {
834 qCWarning(QT_BT) <<
"Services can only be added in unconnected state";
837 if (!service.isValid()) {
838 qCWarning(QT_BT) <<
"Not adding invalid service";
842#if defined(QT_ANDROID_BLUETOOTH)
843 if (!ensureAndroidPermission(QBluetoothPermission::Access)) {
844 qCWarning(QT_BT_ANDROID) <<
"addService() failed due to missing permissions";
849 Q_D(QLowEnergyController);
850 QLowEnergyService *newService = d->addServiceHelper(service);
852 newService->setParent(parent);
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879void QLowEnergyController::requestConnectionUpdate(
const QLowEnergyConnectionParameters ¶meters)
883 case DiscoveredState:
884 case DiscoveringState:
885 d_ptr->requestConnectionUpdate(parameters);
888 qCWarning(QT_BT) <<
"Connection update request only possible in connected state";
893
894
895QLowEnergyController::Error QLowEnergyController::error()
const
901
902
903
904QString QLowEnergyController::errorString()
const
906 return d_ptr->errorString;
910
911
912
913
914
915
916
917QLowEnergyController::Role QLowEnergyController::role()
const
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944int QLowEnergyController::mtu()
const
950
951
952
953
954
955
956
957
958
959
960
961
962
963void QLowEnergyController::readRssi()
965 if (role() != CentralRole) {
966 qCWarning(QT_BT,
"Invalid role (peripheral), cannot read RSSI");
967 return d_ptr->setError(RssiReadError);
971 case UnconnectedState:
972 case ConnectingState:
974 qCWarning(QT_BT,
"Cannot read RSSI while not in 'Connected' state, connect first");
975 return d_ptr->setError(RssiReadError);
983#include "moc_qlowenergycontroller.cpp"