7#include "qbluetoothsocket_bluez_p.h"
8#include "qbluetoothsocket_bluezdbus_p.h"
9#include "bluez/bluez5_helper_p.h"
10#elif defined(QT_ANDROID_BLUETOOTH)
11#include "qbluetoothsocket_android_p.h"
12#elif defined(QT_WINRT_BLUETOOTH)
13#include "qbluetoothsocket_winrt_p.h"
14#elif defined(QT_OSX_BLUETOOTH)
15#include "qbluetoothsocket_macos_p.h"
16#elif defined(QT_HARMONY_BLUETOOTH)
17#include "qbluetoothsocket_ohos_p.h"
24#include <QtCore/QLoggingCategory>
25#include <QSocketNotifier>
29Q_DECLARE_LOGGING_CATEGORY(QT_BT)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
70
71
72
73
74
75
76
77
78
79
80
81
82
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
105
106
107
108
109
110
113
114
115
116
117
118
119
120
121
122
123
124
125
128
129
130
131
132
133
134
137
138
139
140
141
142
145
146
147
148
149
150
151
152
153
154
155
158
159
160
161
162
163
164
165
166
169
170
171
172
173
174
175
176
179
180
181
182
183
184
185
186
189
190
191
192
193
194
195
196
197
198
199
202
203
204
205
206
207
208
209
212
213
214
215
218
219
220
221
224
225
226
227
228
231
232
233
234
237
238
239
240
242static QBluetoothSocketBasePrivate *createSocketPrivate()
245 if (bluetoothdVersion() >= QVersionNumber(5, 46)) {
246 qCDebug(QT_BT) <<
"Using Bluetooth dbus socket implementation";
247 return new QBluetoothSocketPrivateBluezDBus();
249 qCDebug(QT_BT) <<
"Using Bluetooth raw socket implementation";
250 return new QBluetoothSocketPrivateBluez();
252#elif defined(QT_ANDROID_BLUETOOTH)
253 return new QBluetoothSocketPrivateAndroid();
254#elif defined(QT_WINRT_BLUETOOTH)
255 return new QBluetoothSocketPrivateWinRT();
256#elif defined(QT_OSX_BLUETOOTH)
257 return new QBluetoothSocketPrivateDarwin();
258#elif defined(QT_HARMONY_BLUETOOTH)
259 return new QBluetoothSocketPrivateOhos();
261 return new QBluetoothSocketPrivateDummy();
266
267
268QBluetoothSocket::QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent)
271 d_ptr = createSocketPrivate();
274 Q_D(QBluetoothSocketBase);
275 d->ensureNativeSocket(socketType);
277 setOpenMode(QIODevice::NotOpen);
281
282
283QBluetoothSocket::QBluetoothSocket(QObject *parent)
286 d_ptr = createSocketPrivate();
288 setOpenMode(QIODevice::NotOpen);
294
295
296QBluetoothSocket::QBluetoothSocket(QBluetoothSocketBasePrivate *dPrivate,
297 QBluetoothServiceInfo::Protocol socketType,
304 Q_D(QBluetoothSocketBase);
305 d->ensureNativeSocket(socketType);
307 setOpenMode(QIODevice::NotOpen);
313
314
315QBluetoothSocket::~QBluetoothSocket()
322
323
324bool QBluetoothSocket::isSequential()
const
330
331
332
333
334qint64 QBluetoothSocket::bytesAvailable()
const
336 Q_D(
const QBluetoothSocketBase);
337 return QIODevice::bytesAvailable() + d->bytesAvailable();
341
342
343
344qint64 QBluetoothSocket::bytesToWrite()
const
346 Q_D(
const QBluetoothSocketBase);
347 return d->bytesToWrite();
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370void QBluetoothSocket::connectToService(
const QBluetoothServiceInfo &service, OpenMode openMode)
372 Q_D(QBluetoothSocketBase);
373 d->connectToService(service, openMode);
377
378
379
380
381
382
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407void QBluetoothSocket::connectToService(
const QBluetoothAddress &address,
const QBluetoothUuid &uuid, OpenMode openMode)
409 Q_D(QBluetoothSocketBase);
410 d->connectToService(address, uuid, openMode);
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435void QBluetoothSocket::connectToService(
const QBluetoothAddress &address, quint16 port, OpenMode openMode)
437 Q_D(QBluetoothSocketBase);
438 d->connectToService(address, port, openMode);
442
443
444
445
446
447
448QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType()
const
450 Q_D(
const QBluetoothSocketBase);
451 return d->socketType;
455
456
457QBluetoothSocket::SocketState QBluetoothSocket::state()
const
459 Q_D(
const QBluetoothSocketBase);
464
465
466QBluetoothSocket::SocketError QBluetoothSocket::error()
const
468 Q_D(
const QBluetoothSocketBase);
469 return d->socketError;
473
474
475QString QBluetoothSocket::errorString()
const
477 Q_D(
const QBluetoothSocketBase);
478 return d->errorString;
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512void QBluetoothSocket::setPreferredSecurityFlags(QBluetooth::SecurityFlags flags)
514#ifdef QT_OSX_BLUETOOTH
517 Q_D(QBluetoothSocketBase);
518 if (d->secFlags != flags)
523
524
525
526
527
528
529
530
531
532
533
534
535
536QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags()
const
540 return QBluetooth::Security::Secure;
542 Q_D(
const QBluetoothSocketBase);
548
549
550void QBluetoothSocket::setSocketState(QBluetoothSocket::SocketState state)
552 Q_D(QBluetoothSocketBase);
553 const SocketState old = d->state;
559 emit stateChanged(state);
560 if (state == QBluetoothSocket::SocketState::ConnectedState) {
562 }
else if ((old == QBluetoothSocket::SocketState::ConnectedState
563 || old == QBluetoothSocket::SocketState::ClosingState)
564 && state == QBluetoothSocket::SocketState::UnconnectedState) {
567 if (state == SocketState::ListeningState){
568#ifdef QT_OSX_BLUETOOTH
569 qCWarning(QT_BT) <<
"listening socket is not supported by IOBluetooth";
573 if (d->readNotifier) {
574 d->readNotifier->setEnabled(
false);
580
581
583bool QBluetoothSocket::canReadLine()
const
585 Q_D(
const QBluetoothSocketBase);
586 return d->canReadLine() || QIODevice::canReadLine();
590
591
592void QBluetoothSocket::setSocketError(QBluetoothSocket::SocketError error_)
594 Q_D(QBluetoothSocketBase);
595 d->socketError = error_;
596 emit errorOccurred(error_);
600
601
602
603
605void QBluetoothSocket::doDeviceDiscovery(
const QBluetoothServiceInfo &service, OpenMode openMode)
607 Q_D(QBluetoothSocketBase);
609 setSocketState(QBluetoothSocket::SocketState::ServiceLookupState);
610 qCDebug(QT_BT) <<
"Starting Bluetooth service discovery";
612 if(d->discoveryAgent) {
613 d->discoveryAgent->stop();
614 delete d->discoveryAgent;
617 d->discoveryAgent =
new QBluetoothServiceDiscoveryAgent(
this);
618 d->discoveryAgent->setRemoteAddress(service.device().address());
622 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered,
623 this, &QBluetoothSocket::serviceDiscovered);
624 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished,
625 this, &QBluetoothSocket::discoveryFinished);
627 d->openMode = openMode;
629 QList<QBluetoothUuid> filterUuids = service.serviceClassUuids();
630 if(!service.serviceUuid().isNull())
631 filterUuids.append(service.serviceUuid());
633 if (!filterUuids.isEmpty())
634 d->discoveryAgent->setUuidFilter(filterUuids);
637 Q_ASSERT(!d->discoveryAgent->uuidFilter().isEmpty());
639 qCDebug(QT_BT) <<
"UUID filter" << d->discoveryAgent->uuidFilter();
641 d->discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
645void QBluetoothSocket::serviceDiscovered(
const QBluetoothServiceInfo &service)
647 Q_D(QBluetoothSocketBase);
648 qCDebug(QT_BT) <<
"FOUND SERVICE!" << service;
649 if (service.protocolServiceMultiplexer() > 0 || service.serverChannel() > 0) {
650 connectToService(service, d->openMode);
651 d->discoveryAgent->deleteLater();
652 d->discoveryAgent =
nullptr;
653#ifdef QT_WINRT_BLUETOOTH
654 }
else if (!service.attribute(0xBEEF).isNull()
655 && !service.attribute(0xBEF0).isNull()) {
656 connectToService(service, d->openMode);
657 d->discoveryAgent->deleteLater();
658 d->discoveryAgent =
nullptr;
661 qCDebug(QT_BT) <<
"Could not find port/psm for potential remote service";
666void QBluetoothSocket::discoveryFinished()
668 qCDebug(QT_BT) <<
"Socket discovery finished";
669 Q_D(QBluetoothSocketBase);
670 if (d->discoveryAgent){
671 qCDebug(QT_BT) <<
"Didn't find any";
672 d->errorString = tr(
"Service cannot be found");
673 setSocketError(SocketError::ServiceNotFoundError);
674 setSocketState(QBluetoothSocket::SocketState::UnconnectedState);
675 d->discoveryAgent->deleteLater();
676 d->discoveryAgent =
nullptr;
680void QBluetoothSocket::abort()
682 if (state() == SocketState::UnconnectedState)
685 Q_D(QBluetoothSocketBase);
686 setOpenMode(QIODevice::NotOpen);
688 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
689 d->discoveryAgent->disconnect();
690 d->discoveryAgent->stop();
691 d->discoveryAgent =
nullptr;
694 setSocketState(SocketState::ClosingState);
698void QBluetoothSocket::disconnectFromService()
703QString QBluetoothSocket::localName()
const
705 Q_D(
const QBluetoothSocketBase);
706 return d->localName();
709QBluetoothAddress QBluetoothSocket::localAddress()
const
711 Q_D(
const QBluetoothSocketBase);
712 return d->localAddress();
715quint16 QBluetoothSocket::localPort()
const
717 Q_D(
const QBluetoothSocketBase);
718 return d->localPort();
721QString QBluetoothSocket::peerName()
const
723 Q_D(
const QBluetoothSocketBase);
724 return d->peerName();
727QBluetoothAddress QBluetoothSocket::peerAddress()
const
729 Q_D(
const QBluetoothSocketBase);
730 return d->peerAddress();
733quint16 QBluetoothSocket::peerPort()
const
735 Q_D(
const QBluetoothSocketBase);
736 return d->peerPort();
739qint64 QBluetoothSocket::writeData(
const char *data, qint64 maxSize)
741 Q_D(QBluetoothSocketBase);
743 if (!data || maxSize <= 0) {
744 d_ptr->errorString = tr(
"Invalid data/data size");
745 setSocketError(QBluetoothSocket::SocketError::OperationError);
749 return d->writeData(data, maxSize);
752qint64 QBluetoothSocket::readData(
char *data, qint64 maxSize)
754 Q_D(QBluetoothSocketBase);
755 return d->readData(data, maxSize);
758void QBluetoothSocket::close()
760 if (state() == SocketState::UnconnectedState)
763 Q_D(QBluetoothSocketBase);
764 setOpenMode(QIODevice::NotOpen);
766 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
767 d->discoveryAgent->disconnect();
768 d->discoveryAgent->stop();
769 d->discoveryAgent =
nullptr;
772 setSocketState(SocketState::ClosingState);
778
779
780
781
782
783
784
785
786
787
792bool QBluetoothSocket::setSocketDescriptor(
int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
793 SocketState socketState, OpenMode openMode)
795 Q_D(QBluetoothSocketBase);
796 return d->setSocketDescriptor(socketDescriptor, socketType, socketState, openMode);
800
801
802
804int QBluetoothSocket::socketDescriptor()
const
806 Q_D(
const QBluetoothSocketBase);
812#include "moc_qbluetoothsocket.cpp"
Combined button and popup list for selecting options.