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"
22#include <QtCore/QLoggingCategory>
23#include <QSocketNotifier>
27Q_DECLARE_LOGGING_CATEGORY(QT_BT)
30
31
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
68
69
70
71
72
73
74
75
76
77
78
79
80
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
103
104
105
106
107
108
111
112
113
114
115
116
119
120
121
122
123
124
125
128
129
130
131
132
133
136
137
138
139
140
141
142
143
144
145
146
149
150
151
152
153
154
155
156
157
160
161
162
163
164
165
166
167
170
171
172
173
174
175
176
177
180
181
182
183
184
185
186
187
190
191
192
193
194
195
196
197
200
201
202
203
206
207
208
209
212
213
214
215
216
219
220
221
222
225
226
227
228
230static QBluetoothSocketBasePrivate *createSocketPrivate()
233 if (bluetoothdVersion() >= QVersionNumber(5, 46)) {
234 qCDebug(QT_BT) <<
"Using Bluetooth dbus socket implementation";
235 return new QBluetoothSocketPrivateBluezDBus();
237 qCDebug(QT_BT) <<
"Using Bluetooth raw socket implementation";
238 return new QBluetoothSocketPrivateBluez();
240#elif defined(QT_ANDROID_BLUETOOTH)
241 return new QBluetoothSocketPrivateAndroid();
242#elif defined(QT_WINRT_BLUETOOTH)
243 return new QBluetoothSocketPrivateWinRT();
244#elif defined(QT_OSX_BLUETOOTH)
245 return new QBluetoothSocketPrivateDarwin();
247 return new QBluetoothSocketPrivateDummy();
252
253
254QBluetoothSocket::QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent)
257 d_ptr = createSocketPrivate();
260 Q_D(QBluetoothSocketBase);
261 d->ensureNativeSocket(socketType);
263 setOpenMode(QIODevice::NotOpen);
267
268
269QBluetoothSocket::QBluetoothSocket(QObject *parent)
272 d_ptr = createSocketPrivate();
274 setOpenMode(QIODevice::NotOpen);
280
281
282QBluetoothSocket::QBluetoothSocket(QBluetoothSocketBasePrivate *dPrivate,
283 QBluetoothServiceInfo::Protocol socketType,
290 Q_D(QBluetoothSocketBase);
291 d->ensureNativeSocket(socketType);
293 setOpenMode(QIODevice::NotOpen);
299
300
301QBluetoothSocket::~QBluetoothSocket()
308
309
310bool QBluetoothSocket::isSequential()
const
316
317
318
319
320qint64 QBluetoothSocket::bytesAvailable()
const
322 Q_D(
const QBluetoothSocketBase);
323 return QIODevice::bytesAvailable() + d->bytesAvailable();
327
328
329
330qint64 QBluetoothSocket::bytesToWrite()
const
332 Q_D(
const QBluetoothSocketBase);
333 return d->bytesToWrite();
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356void QBluetoothSocket::connectToService(
const QBluetoothServiceInfo &service, OpenMode openMode)
358 Q_D(QBluetoothSocketBase);
359 d->connectToService(service, openMode);
363
364
365
366
367
368
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393void QBluetoothSocket::connectToService(
const QBluetoothAddress &address,
const QBluetoothUuid &uuid, OpenMode openMode)
395 Q_D(QBluetoothSocketBase);
396 d->connectToService(address, uuid, openMode);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417void QBluetoothSocket::connectToService(
const QBluetoothAddress &address, quint16 port, OpenMode openMode)
419 Q_D(QBluetoothSocketBase);
420 d->connectToService(address, port, openMode);
424
425
426
427
428
429
430QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType()
const
432 Q_D(
const QBluetoothSocketBase);
433 return d->socketType;
437
438
439QBluetoothSocket::SocketState QBluetoothSocket::state()
const
441 Q_D(
const QBluetoothSocketBase);
446
447
448QBluetoothSocket::SocketError QBluetoothSocket::error()
const
450 Q_D(
const QBluetoothSocketBase);
451 return d->socketError;
455
456
457QString QBluetoothSocket::errorString()
const
459 Q_D(
const QBluetoothSocketBase);
460 return d->errorString;
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494void QBluetoothSocket::setPreferredSecurityFlags(QBluetooth::SecurityFlags flags)
496#ifdef QT_OSX_BLUETOOTH
499 Q_D(QBluetoothSocketBase);
500 if (d->secFlags != flags)
505
506
507
508
509
510
511
512
513
514
515
516
517
518QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags()
const
522 return QBluetooth::Security::Secure;
524 Q_D(
const QBluetoothSocketBase);
530
531
532void QBluetoothSocket::setSocketState(QBluetoothSocket::SocketState state)
534 Q_D(QBluetoothSocketBase);
535 const SocketState old = d->state;
541 emit stateChanged(state);
542 if (state == QBluetoothSocket::SocketState::ConnectedState) {
544 }
else if ((old == QBluetoothSocket::SocketState::ConnectedState
545 || old == QBluetoothSocket::SocketState::ClosingState)
546 && state == QBluetoothSocket::SocketState::UnconnectedState) {
549 if (state == SocketState::ListeningState){
550#ifdef QT_OSX_BLUETOOTH
551 qCWarning(QT_BT) <<
"listening socket is not supported by IOBluetooth";
555 if (d->readNotifier) {
556 d->readNotifier->setEnabled(
false);
562
563
565bool QBluetoothSocket::canReadLine()
const
567 Q_D(
const QBluetoothSocketBase);
568 return d->canReadLine() || QIODevice::canReadLine();
572
573
574void QBluetoothSocket::setSocketError(QBluetoothSocket::SocketError error_)
576 Q_D(QBluetoothSocketBase);
577 d->socketError = error_;
578 emit errorOccurred(error_);
582
583
584
585
587void QBluetoothSocket::doDeviceDiscovery(
const QBluetoothServiceInfo &service, OpenMode openMode)
589 Q_D(QBluetoothSocketBase);
591 setSocketState(QBluetoothSocket::SocketState::ServiceLookupState);
592 qCDebug(QT_BT) <<
"Starting Bluetooth service discovery";
594 if(d->discoveryAgent) {
595 d->discoveryAgent->stop();
596 delete d->discoveryAgent;
599 d->discoveryAgent =
new QBluetoothServiceDiscoveryAgent(
this);
600 d->discoveryAgent->setRemoteAddress(service.device().address());
604 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered,
605 this, &QBluetoothSocket::serviceDiscovered);
606 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished,
607 this, &QBluetoothSocket::discoveryFinished);
609 d->openMode = openMode;
611 QList<QBluetoothUuid> filterUuids = service.serviceClassUuids();
612 if(!service.serviceUuid().isNull())
613 filterUuids.append(service.serviceUuid());
615 if (!filterUuids.isEmpty())
616 d->discoveryAgent->setUuidFilter(filterUuids);
619 Q_ASSERT(!d->discoveryAgent->uuidFilter().isEmpty());
621 qCDebug(QT_BT) <<
"UUID filter" << d->discoveryAgent->uuidFilter();
623 d->discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
627void QBluetoothSocket::serviceDiscovered(
const QBluetoothServiceInfo &service)
629 Q_D(QBluetoothSocketBase);
630 qCDebug(QT_BT) <<
"FOUND SERVICE!" << service;
631 if (service.protocolServiceMultiplexer() > 0 || service.serverChannel() > 0) {
632 connectToService(service, d->openMode);
633 d->discoveryAgent->deleteLater();
634 d->discoveryAgent =
nullptr;
635#ifdef QT_WINRT_BLUETOOTH
636 }
else if (!service.attribute(0xBEEF).isNull()
637 && !service.attribute(0xBEF0).isNull()) {
638 connectToService(service, d->openMode);
639 d->discoveryAgent->deleteLater();
640 d->discoveryAgent =
nullptr;
643 qCDebug(QT_BT) <<
"Could not find port/psm for potential remote service";
648void QBluetoothSocket::discoveryFinished()
650 qCDebug(QT_BT) <<
"Socket discovery finished";
651 Q_D(QBluetoothSocketBase);
652 if (d->discoveryAgent){
653 qCDebug(QT_BT) <<
"Didn't find any";
654 d->errorString = tr(
"Service cannot be found");
655 setSocketError(SocketError::ServiceNotFoundError);
656 setSocketState(QBluetoothSocket::SocketState::UnconnectedState);
657 d->discoveryAgent->deleteLater();
658 d->discoveryAgent =
nullptr;
662void QBluetoothSocket::abort()
664 if (state() == SocketState::UnconnectedState)
667 Q_D(QBluetoothSocketBase);
668 setOpenMode(QIODevice::NotOpen);
670 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
671 d->discoveryAgent->disconnect();
672 d->discoveryAgent->stop();
673 d->discoveryAgent =
nullptr;
676 setSocketState(SocketState::ClosingState);
680void QBluetoothSocket::disconnectFromService()
685QString QBluetoothSocket::localName()
const
687 Q_D(
const QBluetoothSocketBase);
688 return d->localName();
691QBluetoothAddress QBluetoothSocket::localAddress()
const
693 Q_D(
const QBluetoothSocketBase);
694 return d->localAddress();
697quint16 QBluetoothSocket::localPort()
const
699 Q_D(
const QBluetoothSocketBase);
700 return d->localPort();
703QString QBluetoothSocket::peerName()
const
705 Q_D(
const QBluetoothSocketBase);
706 return d->peerName();
709QBluetoothAddress QBluetoothSocket::peerAddress()
const
711 Q_D(
const QBluetoothSocketBase);
712 return d->peerAddress();
715quint16 QBluetoothSocket::peerPort()
const
717 Q_D(
const QBluetoothSocketBase);
718 return d->peerPort();
721qint64 QBluetoothSocket::writeData(
const char *data, qint64 maxSize)
723 Q_D(QBluetoothSocketBase);
725 if (!data || maxSize <= 0) {
726 d_ptr->errorString = tr(
"Invalid data/data size");
727 setSocketError(QBluetoothSocket::SocketError::OperationError);
731 return d->writeData(data, maxSize);
734qint64 QBluetoothSocket::readData(
char *data, qint64 maxSize)
736 Q_D(QBluetoothSocketBase);
737 return d->readData(data, maxSize);
740void QBluetoothSocket::close()
742 if (state() == SocketState::UnconnectedState)
745 Q_D(QBluetoothSocketBase);
746 setOpenMode(QIODevice::NotOpen);
748 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
749 d->discoveryAgent->disconnect();
750 d->discoveryAgent->stop();
751 d->discoveryAgent =
nullptr;
754 setSocketState(SocketState::ClosingState);
760
761
762
763
764
765
766
767
768
769
774bool QBluetoothSocket::setSocketDescriptor(
int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
775 SocketState socketState, OpenMode openMode)
777 Q_D(QBluetoothSocketBase);
778 return d->setSocketDescriptor(socketDescriptor, socketType, socketState, openMode);
782
783
784
786int QBluetoothSocket::socketDescriptor()
const
788 Q_D(
const QBluetoothSocketBase);
794#include "moc_qbluetoothsocket.cpp"
Combined button and popup list for selecting options.