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(QBluetoothSocket *qq)
245 if (bluetoothdVersion() >= QVersionNumber(5, 46)) {
246 qCDebug(QT_BT) <<
"Using Bluetooth dbus socket implementation";
247 return new QBluetoothSocketPrivateBluezDBus(qq);
249 qCDebug(QT_BT) <<
"Using Bluetooth raw socket implementation";
250 return new QBluetoothSocketPrivateBluez(qq);
252#elif defined(QT_ANDROID_BLUETOOTH)
253 return new QBluetoothSocketPrivateAndroid(qq);
254#elif defined(QT_WINRT_BLUETOOTH)
255 return new QBluetoothSocketPrivateWinRT(qq);
256#elif defined(QT_OSX_BLUETOOTH)
257 return new QBluetoothSocketPrivateDarwin(qq);
258#elif defined(QT_HARMONY_BLUETOOTH)
259 return new QBluetoothSocketPrivateOhos(qq);
261 return new QBluetoothSocketPrivateDummy(qq);
266
267
268QBluetoothSocket::QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent)
269 : QBluetoothSocket(parent)
271 Q_D(QBluetoothSocketBase);
272 d->ensureNativeSocket(socketType);
276
277
278QBluetoothSocket::QBluetoothSocket(QObject *parent)
281 d_ptr = createSocketPrivate(
this);
287
288
289QBluetoothSocket::QBluetoothSocket(QBluetoothSocketBasePrivate *dPrivate,
290 QBluetoothServiceInfo::Protocol socketType,
297 Q_D(QBluetoothSocketBase);
298 d->ensureNativeSocket(socketType);
304
305
306QBluetoothSocket::~QBluetoothSocket()
313
314
315bool QBluetoothSocket::isSequential()
const
321
322
323
324
325qint64 QBluetoothSocket::bytesAvailable()
const
327 Q_D(
const QBluetoothSocketBase);
328 return QIODevice::bytesAvailable() + d->bytesAvailable();
332
333
334
335qint64 QBluetoothSocket::bytesToWrite()
const
337 Q_D(
const QBluetoothSocketBase);
338 return d->bytesToWrite();
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361void QBluetoothSocket::connectToService(
const QBluetoothServiceInfo &service, OpenMode openMode)
363 Q_D(QBluetoothSocketBase);
364 d->connectToService(service, openMode);
368
369
370
371
372
373
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398void QBluetoothSocket::connectToService(
const QBluetoothAddress &address,
const QBluetoothUuid &uuid, OpenMode openMode)
400 Q_D(QBluetoothSocketBase);
401 d->connectToService(address, uuid, openMode);
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426void QBluetoothSocket::connectToService(
const QBluetoothAddress &address, quint16 port, OpenMode openMode)
428 Q_D(QBluetoothSocketBase);
429 d->connectToService(address, port, openMode);
433
434
435
436
437
438
439QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType()
const
441 Q_D(
const QBluetoothSocketBase);
442 return d->socketType;
446
447
448QBluetoothSocket::SocketState QBluetoothSocket::state()
const
450 Q_D(
const QBluetoothSocketBase);
455
456
457QBluetoothSocket::SocketError QBluetoothSocket::error()
const
459 Q_D(
const QBluetoothSocketBase);
460 return d->socketError;
464
465
466QString QBluetoothSocket::errorString()
const
468 Q_D(
const QBluetoothSocketBase);
469 return d->errorString;
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503void QBluetoothSocket::setPreferredSecurityFlags(QBluetooth::SecurityFlags flags)
505#ifdef QT_OSX_BLUETOOTH
508 Q_D(QBluetoothSocketBase);
509 if (d->secFlags != flags)
514
515
516
517
518
519
520
521
522
523
524
525
526
527QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags()
const
531 return QBluetooth::Security::Secure;
533 Q_D(
const QBluetoothSocketBase);
539
540
541void QBluetoothSocket::setSocketState(QBluetoothSocket::SocketState state)
543 Q_D(QBluetoothSocketBase);
544 const SocketState old = d->state;
550 emit stateChanged(state);
551 if (state == QBluetoothSocket::SocketState::ConnectedState) {
553 }
else if ((old == QBluetoothSocket::SocketState::ConnectedState
554 || old == QBluetoothSocket::SocketState::ClosingState)
555 && state == QBluetoothSocket::SocketState::UnconnectedState) {
558 if (state == SocketState::ListeningState){
559#ifdef QT_OSX_BLUETOOTH
560 qCWarning(QT_BT) <<
"listening socket is not supported by IOBluetooth";
564 if (d->readNotifier) {
565 d->readNotifier->setEnabled(
false);
571
572
574bool QBluetoothSocket::canReadLine()
const
576 Q_D(
const QBluetoothSocketBase);
577 return d->canReadLine() || QIODevice::canReadLine();
581
582
583void QBluetoothSocket::setSocketError(QBluetoothSocket::SocketError error_)
585 Q_D(QBluetoothSocketBase);
586 d->socketError = error_;
587 emit errorOccurred(error_);
591
592
593
594
596void QBluetoothSocket::doDeviceDiscovery(
const QBluetoothServiceInfo &service, OpenMode openMode)
598 Q_D(QBluetoothSocketBase);
600 setSocketState(QBluetoothSocket::SocketState::ServiceLookupState);
601 qCDebug(QT_BT) <<
"Starting Bluetooth service discovery";
603 if(d->discoveryAgent) {
604 d->discoveryAgent->stop();
605 delete d->discoveryAgent;
608 d->discoveryAgent =
new QBluetoothServiceDiscoveryAgent(
this);
609 d->discoveryAgent->setRemoteAddress(service.device().address());
613 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered,
614 this, &QBluetoothSocket::serviceDiscovered);
615 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished,
616 this, &QBluetoothSocket::discoveryFinished);
618 d->openMode = openMode;
620 QList<QBluetoothUuid> filterUuids = service.serviceClassUuids();
621 if(!service.serviceUuid().isNull())
622 filterUuids.append(service.serviceUuid());
624 if (!filterUuids.isEmpty())
625 d->discoveryAgent->setUuidFilter(filterUuids);
628 Q_ASSERT(!d->discoveryAgent->uuidFilter().isEmpty());
630 qCDebug(QT_BT) <<
"UUID filter" << d->discoveryAgent->uuidFilter();
632 d->discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
636void QBluetoothSocket::serviceDiscovered(
const QBluetoothServiceInfo &service)
638 Q_D(QBluetoothSocketBase);
639 qCDebug(QT_BT) <<
"FOUND SERVICE!" << service;
640 if (service.protocolServiceMultiplexer() > 0 || service.serverChannel() > 0) {
641 connectToService(service, d->openMode);
642 d->discoveryAgent->deleteLater();
643 d->discoveryAgent =
nullptr;
644#ifdef QT_WINRT_BLUETOOTH
645 }
else if (!service.attribute(0xBEEF).isNull()
646 && !service.attribute(0xBEF0).isNull()) {
647 connectToService(service, d->openMode);
648 d->discoveryAgent->deleteLater();
649 d->discoveryAgent =
nullptr;
652 qCDebug(QT_BT) <<
"Could not find port/psm for potential remote service";
657void QBluetoothSocket::discoveryFinished()
659 qCDebug(QT_BT) <<
"Socket discovery finished";
660 Q_D(QBluetoothSocketBase);
661 if (d->discoveryAgent){
662 qCDebug(QT_BT) <<
"Didn't find any";
663 d->errorString = tr(
"Service cannot be found");
664 setSocketError(SocketError::ServiceNotFoundError);
665 setSocketState(QBluetoothSocket::SocketState::UnconnectedState);
666 d->discoveryAgent->deleteLater();
667 d->discoveryAgent =
nullptr;
671void QBluetoothSocket::abort()
673 if (state() == SocketState::UnconnectedState)
676 Q_D(QBluetoothSocketBase);
677 setOpenMode(QIODevice::NotOpen);
679 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
680 d->discoveryAgent->disconnect();
681 d->discoveryAgent->stop();
682 d->discoveryAgent =
nullptr;
685 setSocketState(SocketState::ClosingState);
689void QBluetoothSocket::disconnectFromService()
694QString QBluetoothSocket::localName()
const
696 Q_D(
const QBluetoothSocketBase);
697 return d->localName();
700QBluetoothAddress QBluetoothSocket::localAddress()
const
702 Q_D(
const QBluetoothSocketBase);
703 return d->localAddress();
706quint16 QBluetoothSocket::localPort()
const
708 Q_D(
const QBluetoothSocketBase);
709 return d->localPort();
712QString QBluetoothSocket::peerName()
const
714 Q_D(
const QBluetoothSocketBase);
715 return d->peerName();
718QBluetoothAddress QBluetoothSocket::peerAddress()
const
720 Q_D(
const QBluetoothSocketBase);
721 return d->peerAddress();
724quint16 QBluetoothSocket::peerPort()
const
726 Q_D(
const QBluetoothSocketBase);
727 return d->peerPort();
730qint64 QBluetoothSocket::writeData(
const char *data, qint64 maxSize)
732 Q_D(QBluetoothSocketBase);
734 if (!data || maxSize <= 0) {
735 d_ptr->errorString = tr(
"Invalid data/data size");
736 setSocketError(QBluetoothSocket::SocketError::OperationError);
740 return d->writeData(data, maxSize);
743qint64 QBluetoothSocket::readData(
char *data, qint64 maxSize)
745 Q_D(QBluetoothSocketBase);
746 return d->readData(data, maxSize);
749void QBluetoothSocket::close()
751 if (state() == SocketState::UnconnectedState)
754 Q_D(QBluetoothSocketBase);
755 setOpenMode(QIODevice::NotOpen);
757 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
758 d->discoveryAgent->disconnect();
759 d->discoveryAgent->stop();
760 d->discoveryAgent =
nullptr;
763 setSocketState(SocketState::ClosingState);
769
770
771
772
773
774
775
776
777
778
783bool QBluetoothSocket::setSocketDescriptor(
int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
784 SocketState socketState, OpenMode openMode)
786 Q_D(QBluetoothSocketBase);
787 return d->setSocketDescriptor(socketDescriptor, socketType, socketState, openMode);
791
792
793
795int QBluetoothSocket::socketDescriptor()
const
797 Q_D(
const QBluetoothSocketBase);
803#include "moc_qbluetoothsocket.cpp"
Combined button and popup list for selecting options.