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);
626void QBluetoothSocket::serviceDiscovered(
const QBluetoothServiceInfo &service)
628 Q_D(QBluetoothSocketBase);
629 qCDebug(QT_BT) <<
"FOUND SERVICE!" << service;
630 if (service.protocolServiceMultiplexer() > 0 || service.serverChannel() > 0) {
631 connectToService(service, d->openMode);
632 d->discoveryAgent->deleteLater();
633 d->discoveryAgent =
nullptr;
634#ifdef QT_WINRT_BLUETOOTH
635 }
else if (!service.attribute(0xBEEF).isNull()
636 && !service.attribute(0xBEF0).isNull()) {
637 connectToService(service, d->openMode);
638 d->discoveryAgent->deleteLater();
639 d->discoveryAgent =
nullptr;
642 qCDebug(QT_BT) <<
"Could not find port/psm for potential remote service";
646void QBluetoothSocket::discoveryFinished()
648 qCDebug(QT_BT) <<
"Socket discovery finished";
649 Q_D(QBluetoothSocketBase);
650 if (d->discoveryAgent){
651 qCDebug(QT_BT) <<
"Didn't find any";
652 d->errorString = tr(
"Service cannot be found");
653 setSocketError(SocketError::ServiceNotFoundError);
654 setSocketState(QBluetoothSocket::SocketState::UnconnectedState);
655 d->discoveryAgent->deleteLater();
656 d->discoveryAgent =
nullptr;
660void QBluetoothSocket::abort()
662 if (state() == SocketState::UnconnectedState)
665 Q_D(QBluetoothSocketBase);
666 setOpenMode(QIODevice::NotOpen);
668 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
669 d->discoveryAgent->disconnect();
670 d->discoveryAgent->stop();
671 d->discoveryAgent =
nullptr;
674 setSocketState(SocketState::ClosingState);
678void QBluetoothSocket::disconnectFromService()
683QString QBluetoothSocket::localName()
const
685 Q_D(
const QBluetoothSocketBase);
686 return d->localName();
689QBluetoothAddress QBluetoothSocket::localAddress()
const
691 Q_D(
const QBluetoothSocketBase);
692 return d->localAddress();
695quint16 QBluetoothSocket::localPort()
const
697 Q_D(
const QBluetoothSocketBase);
698 return d->localPort();
701QString QBluetoothSocket::peerName()
const
703 Q_D(
const QBluetoothSocketBase);
704 return d->peerName();
707QBluetoothAddress QBluetoothSocket::peerAddress()
const
709 Q_D(
const QBluetoothSocketBase);
710 return d->peerAddress();
713quint16 QBluetoothSocket::peerPort()
const
715 Q_D(
const QBluetoothSocketBase);
716 return d->peerPort();
719qint64 QBluetoothSocket::writeData(
const char *data, qint64 maxSize)
721 Q_D(QBluetoothSocketBase);
723 if (!data || maxSize <= 0) {
724 d_ptr->errorString = tr(
"Invalid data/data size");
725 setSocketError(QBluetoothSocket::SocketError::OperationError);
729 return d->writeData(data, maxSize);
732qint64 QBluetoothSocket::readData(
char *data, qint64 maxSize)
734 Q_D(QBluetoothSocketBase);
735 return d->readData(data, maxSize);
738void QBluetoothSocket::close()
740 if (state() == SocketState::UnconnectedState)
743 Q_D(QBluetoothSocketBase);
744 setOpenMode(QIODevice::NotOpen);
746 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
747 d->discoveryAgent->disconnect();
748 d->discoveryAgent->stop();
749 d->discoveryAgent =
nullptr;
752 setSocketState(SocketState::ClosingState);
758
759
760
761
762
763
764
765
766
767
772bool QBluetoothSocket::setSocketDescriptor(
int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
773 SocketState socketState, OpenMode openMode)
775 Q_D(QBluetoothSocketBase);
776 return d->setSocketDescriptor(socketDescriptor, socketType, socketState, openMode);
780
781
782
784int QBluetoothSocket::socketDescriptor()
const
786 Q_D(
const QBluetoothSocketBase);
792#include "moc_qbluetoothsocket.cpp"
Combined button and popup list for selecting options.