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
491void QBluetoothSocket::setPreferredSecurityFlags(QBluetooth::SecurityFlags flags)
493#ifdef QT_OSX_BLUETOOTH
496 Q_D(QBluetoothSocketBase);
497 if (d->secFlags != flags)
502
503
504
505
506
507
508
509
510
511
512
513
514
515QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags()
const
519 return QBluetooth::Security::Secure;
521 Q_D(
const QBluetoothSocketBase);
527
528
529void QBluetoothSocket::setSocketState(QBluetoothSocket::SocketState state)
531 Q_D(QBluetoothSocketBase);
532 const SocketState old = d->state;
538 emit stateChanged(state);
539 if (state == QBluetoothSocket::SocketState::ConnectedState) {
541 }
else if ((old == QBluetoothSocket::SocketState::ConnectedState
542 || old == QBluetoothSocket::SocketState::ClosingState)
543 && state == QBluetoothSocket::SocketState::UnconnectedState) {
546 if (state == SocketState::ListeningState){
547#ifdef QT_OSX_BLUETOOTH
548 qCWarning(QT_BT) <<
"listening socket is not supported by IOBluetooth";
552 if (d->readNotifier) {
553 d->readNotifier->setEnabled(
false);
559
560
562bool QBluetoothSocket::canReadLine()
const
564 Q_D(
const QBluetoothSocketBase);
565 return d->canReadLine() || QIODevice::canReadLine();
569
570
571void QBluetoothSocket::setSocketError(QBluetoothSocket::SocketError error_)
573 Q_D(QBluetoothSocketBase);
574 d->socketError = error_;
575 emit errorOccurred(error_);
579
580
581
582
584void QBluetoothSocket::doDeviceDiscovery(
const QBluetoothServiceInfo &service, OpenMode openMode)
586 Q_D(QBluetoothSocketBase);
588 setSocketState(QBluetoothSocket::SocketState::ServiceLookupState);
589 qCDebug(QT_BT) <<
"Starting Bluetooth service discovery";
591 if(d->discoveryAgent) {
592 d->discoveryAgent->stop();
593 delete d->discoveryAgent;
596 d->discoveryAgent =
new QBluetoothServiceDiscoveryAgent(
this);
597 d->discoveryAgent->setRemoteAddress(service.device().address());
601 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered,
602 this, &QBluetoothSocket::serviceDiscovered);
603 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished,
604 this, &QBluetoothSocket::discoveryFinished);
606 d->openMode = openMode;
608 QList<QBluetoothUuid> filterUuids = service.serviceClassUuids();
609 if(!service.serviceUuid().isNull())
610 filterUuids.append(service.serviceUuid());
612 if (!filterUuids.isEmpty())
613 d->discoveryAgent->setUuidFilter(filterUuids);
616 Q_ASSERT(!d->discoveryAgent->uuidFilter().isEmpty());
618 qCDebug(QT_BT) <<
"UUID filter" << d->discoveryAgent->uuidFilter();
620 d->discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
623void QBluetoothSocket::serviceDiscovered(
const QBluetoothServiceInfo &service)
625 Q_D(QBluetoothSocketBase);
626 qCDebug(QT_BT) <<
"FOUND SERVICE!" << service;
627 if (service.protocolServiceMultiplexer() > 0 || service.serverChannel() > 0) {
628 connectToService(service, d->openMode);
629 d->discoveryAgent->deleteLater();
630 d->discoveryAgent =
nullptr;
631#ifdef QT_WINRT_BLUETOOTH
632 }
else if (!service.attribute(0xBEEF).isNull()
633 && !service.attribute(0xBEF0).isNull()) {
634 connectToService(service, d->openMode);
635 d->discoveryAgent->deleteLater();
636 d->discoveryAgent =
nullptr;
639 qCDebug(QT_BT) <<
"Could not find port/psm for potential remote service";
643void QBluetoothSocket::discoveryFinished()
645 qCDebug(QT_BT) <<
"Socket discovery finished";
646 Q_D(QBluetoothSocketBase);
647 if (d->discoveryAgent){
648 qCDebug(QT_BT) <<
"Didn't find any";
649 d->errorString = tr(
"Service cannot be found");
650 setSocketError(SocketError::ServiceNotFoundError);
651 setSocketState(QBluetoothSocket::SocketState::UnconnectedState);
652 d->discoveryAgent->deleteLater();
653 d->discoveryAgent =
nullptr;
657void QBluetoothSocket::abort()
659 if (state() == SocketState::UnconnectedState)
662 Q_D(QBluetoothSocketBase);
663 setOpenMode(QIODevice::NotOpen);
665 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
666 d->discoveryAgent->disconnect();
667 d->discoveryAgent->stop();
668 d->discoveryAgent =
nullptr;
671 setSocketState(SocketState::ClosingState);
675void QBluetoothSocket::disconnectFromService()
680QString QBluetoothSocket::localName()
const
682 Q_D(
const QBluetoothSocketBase);
683 return d->localName();
686QBluetoothAddress QBluetoothSocket::localAddress()
const
688 Q_D(
const QBluetoothSocketBase);
689 return d->localAddress();
692quint16 QBluetoothSocket::localPort()
const
694 Q_D(
const QBluetoothSocketBase);
695 return d->localPort();
698QString QBluetoothSocket::peerName()
const
700 Q_D(
const QBluetoothSocketBase);
701 return d->peerName();
704QBluetoothAddress QBluetoothSocket::peerAddress()
const
706 Q_D(
const QBluetoothSocketBase);
707 return d->peerAddress();
710quint16 QBluetoothSocket::peerPort()
const
712 Q_D(
const QBluetoothSocketBase);
713 return d->peerPort();
716qint64 QBluetoothSocket::writeData(
const char *data, qint64 maxSize)
718 Q_D(QBluetoothSocketBase);
720 if (!data || maxSize <= 0) {
721 d_ptr->errorString = tr(
"Invalid data/data size");
722 setSocketError(QBluetoothSocket::SocketError::OperationError);
726 return d->writeData(data, maxSize);
729qint64 QBluetoothSocket::readData(
char *data, qint64 maxSize)
731 Q_D(QBluetoothSocketBase);
732 return d->readData(data, maxSize);
735void QBluetoothSocket::close()
737 if (state() == SocketState::UnconnectedState)
740 Q_D(QBluetoothSocketBase);
741 setOpenMode(QIODevice::NotOpen);
743 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
744 d->discoveryAgent->disconnect();
745 d->discoveryAgent->stop();
746 d->discoveryAgent =
nullptr;
749 setSocketState(SocketState::ClosingState);
755
756
757
758
759
760
761
762
763
764
769bool QBluetoothSocket::setSocketDescriptor(
int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
770 SocketState socketState, OpenMode openMode)
772 Q_D(QBluetoothSocketBase);
773 return d->setSocketDescriptor(socketDescriptor, socketType, socketState, openMode);
777
778
779
781int QBluetoothSocket::socketDescriptor()
const
783 Q_D(
const QBluetoothSocketBase);
789#include "moc_qbluetoothsocket.cpp"