Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbluetoothsocket.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// Copyright (C) 2016 BlackBerry Limited. All rights reserved.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
6#if QT_CONFIG(bluez)
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"
18#else
20#endif
21
23
24#include <QtCore/QLoggingCategory>
25#include <QSocketNotifier>
26
28
29Q_DECLARE_LOGGING_CATEGORY(QT_BT)
30
31/*!
32 \class QBluetoothSocket
33 \inmodule QtBluetooth
34 \brief The QBluetoothSocket class enables connection to a Bluetooth device
35 running a bluetooth server.
36
37 \since 5.2
38
39 QBluetoothSocket supports two socket types, \l {QBluetoothServiceInfo::L2capProtocol}{L2CAP} and
40 \l {QBluetoothServiceInfo::RfcommProtocol}{RFCOMM}.
41
42 \l {QBluetoothServiceInfo::L2capProtocol}{L2CAP} is a low level datagram-oriented Bluetooth socket.
43 Android does not support \l {QBluetoothServiceInfo::L2capProtocol}{L2CAP} for socket
44 connections, and the HarmonyOS backend implements RFCOMM only.
45
46 \l {QBluetoothServiceInfo::RfcommProtocol}{RFCOMM} is a reliable, stream-oriented socket. RFCOMM
47 sockets emulate an RS-232 serial port.
48
49 To create a connection to a Bluetooth service, create a socket of the appropriate type and call
50 connectToService() passing the Bluetooth address and port number. QBluetoothSocket will emit
51 the connected() signal when the connection is established.
52
53 If the \l {QBluetoothServiceInfo::Protocol}{Protocol} is not supported on a platform, calling
54 \l connectToService() will emit a \l {QBluetoothSocket::SocketError::UnsupportedProtocolError}{UnsupportedProtocolError} error.
55
56 \note QBluetoothSocket does not support synchronous read and write operations. Functions such
57 as \l waitForReadyRead() and \l waitForBytesWritten() are not implemented. I/O operations should be
58 performed using \l readyRead(), \l read() and \l write().
59
60 On iOS, this class cannot be used because the platform does not expose
61 an API which may permit access to QBluetoothSocket related features.
62
63 \note On macOS Monterey (12) the socket data flow is paused when a
64 modal dialogue is executing, or an event tracking mode is entered (for
65 example by long-pressing a Window close button). This issue has been
66 fixed in macOS Sequoia (15).
67*/
68
69/*!
70 \enum QBluetoothSocket::SocketState
71
72 This enum describes the state of the Bluetooth socket.
73
74 \value UnconnectedState Socket is not connected.
75 \value ServiceLookupState Socket is querying connection parameters.
76 \value ConnectingState Socket is attempting to connect to a device.
77 \value ConnectedState Socket is connected to a device.
78 \value BoundState Socket is bound to a local address and port.
79 \value ClosingState Socket is connected and will be closed once all pending data is
80 written to the socket.
81 \value ListeningState Socket is listening for incoming connections.
82*/
83
84/*!
85 \enum QBluetoothSocket::SocketError
86
87 This enum describes Bluetooth socket error types.
88
89 \value UnknownSocketError An unknown error has occurred.
90 \value NoSocketError No error. Used for testing.
91 \value HostNotFoundError Could not find the remote host.
92 \value ServiceNotFoundError Could not find the service UUID on remote host.
93 \value NetworkError Attempt to read or write from socket returned an error
94 \value UnsupportedProtocolError The \l {QBluetoothServiceInfo::Protocol}{Protocol} is not
95 supported on this platform.
96 \value OperationError An operation was attempted while the socket was in a state
97 that did not permit it.
98 \value [since 5.10] RemoteHostClosedError The remote host closed the connection.
99 \value [since 6.4] MissingPermissionsError The operating system requests
100 permissions which were not
101 granted by the user.
102*/
103
104/*!
105 \fn void QBluetoothSocket::connected()
106
107 This signal is emitted when a connection is established.
108
109 \sa QBluetoothSocket::SocketState::ConnectedState, stateChanged()
110*/
111
112/*!
113 \fn void QBluetoothSocket::disconnected()
114
115 This signal is emitted when the socket is disconnected.
116
117 \note On HarmonyOS, this backend receives incoming data through the
118 system's \c sppRead subscription, which delivers neither an error nor an
119 end of stream. A disconnect initiated by the remote device is therefore
120 not reported on its own: it is noticed only when a local write fails, or
121 when Bluetooth is switched off. Until then the socket stays in
122 \l {QBluetoothSocket::SocketState::}{ConnectedState}.
123
124 \sa QBluetoothSocket::SocketState::UnconnectedState, stateChanged()
125*/
126
127/*!
128 \fn void QBluetoothSocket::errorOccurred(QBluetoothSocket::SocketError error)
129
130 This signal is emitted when an \a error occurs.
131
132 \sa error()
133 \since 6.2
134*/
135
136/*!
137 \fn QBluetoothSocket::stateChanged(QBluetoothSocket::SocketState state)
138
139 This signal is emitted when the socket state changes to \a state.
140
141 \sa connected(), disconnected(), state(), QBluetoothSocket::SocketState
142*/
143
144/*!
145 \fn void QBluetoothSocket::abort()
146
147 Aborts the current connection and resets the socket. Unlike disconnectFromService(), this
148 function immediately closes the socket, discarding any pending data in the write buffer.
149
150 \note On Android, aborting the socket requires asynchronous interaction with Android threads.
151 Therefore the associated \l disconnected() and \l stateChanged() signals are delayed
152 until the threads have finished the closure.
153
154 \sa disconnectFromService(), close()
155*/
156
157/*!
158 \fn void QBluetoothSocket::close()
159
160 Disconnects the socket's connection with the device.
161
162 \note On Android, closing the socket requires asynchronous interaction with Android threads.
163 Therefore the associated \l disconnected() and \l stateChanged() signals are delayed
164 until the threads have finished the closure.
165
166*/
167
168/*!
169 \fn void QBluetoothSocket::disconnectFromService()
170
171 Attempts to close the socket. If there is pending data waiting to be written QBluetoothSocket
172 will enter ClosingState and wait until all data has been written. Eventually, it will enter
173 UnconnectedState and emit the disconnected() signal.
174
175 \sa connectToService()
176*/
177
178/*!
179 \fn QString QBluetoothSocket::localName() const
180
181 Returns the name of the local device.
182
183 Although some platforms may differ the socket must generally be connected to guarantee
184 the return of a valid name. In particular, this is true when dealing with platforms
185 that support multiple local Bluetooth adapters.
186*/
187
188/*!
189 \fn QBluetoothAddress QBluetoothSocket::localAddress() const
190
191 Returns the address of the local device.
192
193 Although some platforms may differ the socket must generally be connected to guarantee
194 the return of a valid address. In particular, this is true when dealing with platforms
195 that support multiple local Bluetooth adapters.
196
197 On HarmonyOS, this function always returns a null address, because the
198 platform exposes no local socket endpoint.
199*/
200
201/*!
202 \fn quint16 QBluetoothSocket::localPort() const
203
204 Returns the port number of the local socket if available, otherwise returns 0.
205 Although some platforms may differ the socket must generally be connected to guarantee
206 the return of a valid port number.
207
208 On Android, \macos and HarmonyOS, this feature is not supported and returns 0.
209*/
210
211/*!
212 \fn QString QBluetoothSocket::peerName() const
213
214 Returns the name of the peer device.
215*/
216
217/*!
218 \fn QBluetoothAddress QBluetoothSocket::peerAddress() const
219
220 Returns the address of the peer device.
221*/
222
223/*!
224 \fn quint16 QBluetoothSocket::peerPort() const
225
226 Return the port number of the peer socket if available, otherwise returns 0.
227 On Android and HarmonyOS, this feature is not supported.
228*/
229
230/*!
231 \fn qint64 QBluetoothSocket::readData(char *data, qint64 maxSize)
232
233 \reimp
234*/
235
236/*!
237 \fn qint64 QBluetoothSocket::writeData(const char *data, qint64 maxSize)
238
239 \reimp
240*/
241
242static QBluetoothSocketBasePrivate *createSocketPrivate()
243{
244#if QT_CONFIG(bluez)
245 if (bluetoothdVersion() >= QVersionNumber(5, 46)) {
246 qCDebug(QT_BT) << "Using Bluetooth dbus socket implementation";
247 return new QBluetoothSocketPrivateBluezDBus();
248 } else {
249 qCDebug(QT_BT) << "Using Bluetooth raw socket implementation";
250 return new QBluetoothSocketPrivateBluez();
251 }
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();
260#else
261 return new QBluetoothSocketPrivateDummy();
262#endif
263}
264
265/*!
266 Constructs a Bluetooth socket of \a socketType type, with \a parent.
267*/
268QBluetoothSocket::QBluetoothSocket(QBluetoothServiceInfo::Protocol socketType, QObject *parent)
269: QIODevice(parent)
270{
271 d_ptr = createSocketPrivate();
272 d_ptr->q_ptr = this;
273
274 Q_D(QBluetoothSocketBase);
275 d->ensureNativeSocket(socketType);
276
277 setOpenMode(QIODevice::NotOpen);
278}
279
280/*!
281 Constructs a Bluetooth socket with \a parent.
282*/
283QBluetoothSocket::QBluetoothSocket(QObject *parent)
284 : QIODevice(parent)
285{
286 d_ptr = createSocketPrivate();
287 d_ptr->q_ptr = this;
288 setOpenMode(QIODevice::NotOpen);
289}
290
291#if QT_CONFIG(bluez)
292
293/*!
294 \internal
295*/
296QBluetoothSocket::QBluetoothSocket(QBluetoothSocketBasePrivate *dPrivate,
297 QBluetoothServiceInfo::Protocol socketType,
298 QObject *parent)
299 : QIODevice(parent)
300{
301 d_ptr = dPrivate;
302 d_ptr->q_ptr = this;
303
304 Q_D(QBluetoothSocketBase);
305 d->ensureNativeSocket(socketType);
306
307 setOpenMode(QIODevice::NotOpen);
308}
309
310#endif
311
312/*!
313 Destroys the Bluetooth socket.
314*/
315QBluetoothSocket::~QBluetoothSocket()
316{
317 delete d_ptr;
318 d_ptr = nullptr;
319}
320
321/*!
322 \reimp
323*/
324bool QBluetoothSocket::isSequential() const
325{
326 return true;
327}
328
329/*!
330 Returns the number of incoming bytes that are waiting to be read.
331
332 \sa bytesToWrite(), read()
333*/
334qint64 QBluetoothSocket::bytesAvailable() const
335{
336 Q_D(const QBluetoothSocketBase);
337 return QIODevice::bytesAvailable() + d->bytesAvailable();
338}
339
340/*!
341 Returns the number of bytes that are waiting to be written. The bytes are written when control
342 goes back to the event loop.
343*/
344qint64 QBluetoothSocket::bytesToWrite() const
345{
346 Q_D(const QBluetoothSocketBase);
347 return d->bytesToWrite();
348}
349
350/*!
351 Attempts to connect to the service described by \a service.
352
353 The socket is opened in the given \a openMode. The \l socketType() is ignored
354 if \a service specifies a differing \l QBluetoothServiceInfo::socketProtocol().
355
356 The socket first enters ConnectingState and attempts to connect to the device providing
357 \a service. If a connection is established, QBluetoothSocket enters ConnectedState and
358 emits connected().
359
360 At any point, the socket can emit errorOccurred() to signal that an error occurred.
361
362 Note that most platforms require a pairing prior to connecting to the remote device. Otherwise
363 the connection process may fail.
364
365 On Android, only RFCOMM connections are possible. This function ignores any socket protocol indicator
366 and assumes RFCOMM.
367
368 \sa state(), disconnectFromService()
369*/
370void QBluetoothSocket::connectToService(const QBluetoothServiceInfo &service, OpenMode openMode)
371{
372 Q_D(QBluetoothSocketBase);
373 d->connectToService(service, openMode);
374}
375
376/*!
377 \fn void QBluetoothSocket::connectToService(const QBluetoothAddress &address, QBluetoothUuid::ServiceClassUuid uuid, OpenMode mode = ReadWrite)
378
379 \internal
380
381 Exists to avoid QTBUG-65831.
382*/
383
384/*!
385 Attempts to make a connection to the service identified by \a uuid on the device with address
386 \a address.
387
388 The socket is opened in the given \a openMode.
389
390 For BlueZ, the socket first enters the \l ServiceLookupState and queries the connection parameters for
391 \a uuid. If the service parameters are successfully retrieved the socket enters
392 ConnectingState, and attempts to connect to \a address. If a connection is established,
393 QBluetoothSocket enters \l ConnectedState and emits connected().
394
395 On Android, the service connection can directly be established
396 using the UUID of the remote service. Therefore the platform does not require
397 the \l ServiceLookupState and \l socketType() is always set to
398 \l QBluetoothServiceInfo::RfcommProtocol.
399
400 At any point, the socket can emit errorOccurred() to signal that an error occurred.
401
402 Note that most platforms require a pairing prior to connecting to the remote device. Otherwise
403 the connection process may fail.
404
405 \sa state(), disconnectFromService()
406*/
407void QBluetoothSocket::connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode)
408{
409 Q_D(QBluetoothSocketBase);
410 d->connectToService(address, uuid, openMode);
411}
412
413/*!
414 Attempts to make a connection with \a address on the given \a port.
415
416 The socket is opened in the given \a openMode.
417
418 The socket first enters ConnectingState, and attempts to connect to \a address. If a
419 connection is established, QBluetoothSocket enters ConnectedState and emits connected().
420
421 At any point, the socket can emit errorOccurred() to signal that an error occurred.
422
423 On Android and BlueZ (version 5.46 or above), a connection to a service can not be established using a port.
424 Calling this function will emit a \l {QBluetoothSocket::SocketError::ServiceNotFoundError}{ServiceNotFoundError}.
425
426 On HarmonyOS a service is identified by its UUID and the platform exposes
427 no RFCOMM channel number, so this function emits a
428 \l {QBluetoothSocket::SocketError::UnsupportedProtocolError}{UnsupportedProtocolError}.
429
430 Note that most platforms require a pairing prior to connecting to the remote device. Otherwise
431 the connection process may fail.
432
433 \sa state(), disconnectFromService()
434*/
435void QBluetoothSocket::connectToService(const QBluetoothAddress &address, quint16 port, OpenMode openMode)
436{
437 Q_D(QBluetoothSocketBase);
438 d->connectToService(address, port, openMode);
439}
440
441/*!
442 Returns the socket type. The socket automatically adjusts to the protocol
443 offered by the remote service.
444
445 Android only support \l{QBluetoothServiceInfo::RfcommProtocol}{RFCOMM}
446 based sockets.
447*/
448QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType() const
449{
450 Q_D(const QBluetoothSocketBase);
451 return d->socketType;
452}
453
454/*!
455 Returns the current state of the socket.
456*/
457QBluetoothSocket::SocketState QBluetoothSocket::state() const
458{
459 Q_D(const QBluetoothSocketBase);
460 return d->state;
461}
462
463/*!
464 Returns the last error.
465*/
466QBluetoothSocket::SocketError QBluetoothSocket::error() const
467{
468 Q_D(const QBluetoothSocketBase);
469 return d->socketError;
470}
471
472/*!
473 Returns a user displayable text string for the error.
474 */
475QString QBluetoothSocket::errorString() const
476{
477 Q_D(const QBluetoothSocketBase);
478 return d->errorString;
479}
480
481/*!
482 Sets the preferred security parameter for the connection attempt to
483 \a flags. This value is incorporated when calling \l connectToService().
484 Therefore it is required to reconnect to change this parameter for an
485 existing connection.
486
487 \include qbluetoothserver.cpp bluetooth_security_bluez
488
489 On Linux with the BlueZ D-Bus backend and on Windows, this flag is not
490 supported and ignored.
491
492 On \macos, this value is ignored as the platform does not permit access
493 to the security parameter of the socket. By default the platform prefers
494 secure/encrypted connections though and therefore this function always
495 returns \l QBluetooth::Security::Secure.
496
497 Android only supports two levels of security (secure and non-secure).
498 If this flag is set to \l QBluetooth::Security::NoSecurity the socket
499 object will not employ any authentication or encryption. Any other
500 security flag combination will trigger a secure Bluetooth connection.
501 This flag is set to \l QBluetooth::Security::Secure by default.
502
503 \note A secure connection requires a pairing between the two devices. On
504 some platforms, the pairing is automatically initiated during the establishment
505 of the connection. Other platforms require the application to manually trigger
506 the pairing before attempting to connect.
507
508 \sa preferredSecurityFlags()
509
510 \since 5.6
511*/
512void QBluetoothSocket::setPreferredSecurityFlags(QBluetooth::SecurityFlags flags)
513{
514#ifdef QT_OSX_BLUETOOTH
515 return; // not supported on macOS.
516#endif
517 Q_D(QBluetoothSocketBase);
518 if (d->secFlags != flags)
519 d->secFlags = flags;
520}
521
522/*!
523 Returns the security parameters used for the initial connection
524 attempt.
525
526 The security parameters may be renegotiated between the two parties
527 during or after the connection has been established. If such a change happens
528 it is not reflected in the value of this flag.
529
530 On \macos, this flag is always set to \l QBluetooth::Security::Secure.
531
532 \sa setPreferredSecurityFlags()
533
534 \since 5.6
535*/
536QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags() const
537{
538#if QT_OSX_BLUETOOTH
539 // not supported on macOS - platform always uses encryption
540 return QBluetooth::Security::Secure;
541#else
542 Q_D(const QBluetoothSocketBase);
543 return d->secFlags;
544#endif // QT_OSX_BLUETOOTH
545}
546
547/*!
548 Sets the socket state to \a state.
549*/
550void QBluetoothSocket::setSocketState(QBluetoothSocket::SocketState state)
551{
552 Q_D(QBluetoothSocketBase);
553 const SocketState old = d->state;
554 if (state == old)
555 return;
556
557 d->state = state;
558 if(old != d->state)
559 emit stateChanged(state);
560 if (state == QBluetoothSocket::SocketState::ConnectedState) {
561 emit connected();
562 } else if ((old == QBluetoothSocket::SocketState::ConnectedState
563 || old == QBluetoothSocket::SocketState::ClosingState)
564 && state == QBluetoothSocket::SocketState::UnconnectedState) {
565 emit disconnected();
566 }
567 if (state == SocketState::ListeningState){
568#ifdef QT_OSX_BLUETOOTH
569 qCWarning(QT_BT) << "listening socket is not supported by IOBluetooth";
570#endif
571 // TODO: look at this, is this really correct?
572 // if we're a listening socket we can't handle connects?
573 if (d->readNotifier) {
574 d->readNotifier->setEnabled(false);
575 }
576 }
577}
578
579/*!
580 Returns true if you can read at least one line from the device
581 */
582
583bool QBluetoothSocket::canReadLine() const
584{
585 Q_D(const QBluetoothSocketBase);
586 return d->canReadLine() || QIODevice::canReadLine();
587}
588
589/*!
590 Sets the type of error that last occurred to \a error_.
591*/
592void QBluetoothSocket::setSocketError(QBluetoothSocket::SocketError error_)
593{
594 Q_D(QBluetoothSocketBase);
595 d->socketError = error_;
596 emit errorOccurred(error_);
597}
598
599/*!
600 Start device discovery for \a service and open the socket with \a openMode. If the socket
601 is created with a service uuid device address, use service discovery to find the
602 port number to connect to.
603*/
604
605void QBluetoothSocket::doDeviceDiscovery(const QBluetoothServiceInfo &service, OpenMode openMode)
606{
607 Q_D(QBluetoothSocketBase);
608
609 setSocketState(QBluetoothSocket::SocketState::ServiceLookupState);
610 qCDebug(QT_BT) << "Starting Bluetooth service discovery";
611
612 if(d->discoveryAgent) {
613 d->discoveryAgent->stop();
614 delete d->discoveryAgent;
615 }
616
617 d->discoveryAgent = new QBluetoothServiceDiscoveryAgent(this);
618 d->discoveryAgent->setRemoteAddress(service.device().address());
619
620 //qDebug() << "Got agent";
621
622 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::serviceDiscovered,
623 this, &QBluetoothSocket::serviceDiscovered);
624 connect(d->discoveryAgent, &QBluetoothServiceDiscoveryAgent::finished,
625 this, &QBluetoothSocket::discoveryFinished);
626
627 d->openMode = openMode;
628
629 QList<QBluetoothUuid> filterUuids = service.serviceClassUuids();
630 if(!service.serviceUuid().isNull())
631 filterUuids.append(service.serviceUuid());
632
633 if (!filterUuids.isEmpty())
634 d->discoveryAgent->setUuidFilter(filterUuids);
635
636 // we have to ID the service somehow
637 Q_ASSERT(!d->discoveryAgent->uuidFilter().isEmpty());
638
639 qCDebug(QT_BT) << "UUID filter" << d->discoveryAgent->uuidFilter();
640
641 d->discoveryAgent->start(QBluetoothServiceDiscoveryAgent::FullDiscovery);
642}
643
644/*! \internal */
645void QBluetoothSocket::serviceDiscovered(const QBluetoothServiceInfo &service)
646{
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;
659#endif
660 } else {
661 qCDebug(QT_BT) << "Could not find port/psm for potential remote service";
662 }
663}
664
665/*! \internal */
666void QBluetoothSocket::discoveryFinished()
667{
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;
677 }
678}
679
680void QBluetoothSocket::abort()
681{
682 if (state() == SocketState::UnconnectedState)
683 return;
684
685 Q_D(QBluetoothSocketBase);
686 setOpenMode(QIODevice::NotOpen);
687
688 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
689 d->discoveryAgent->disconnect();
690 d->discoveryAgent->stop();
691 d->discoveryAgent = nullptr;
692 }
693
694 setSocketState(SocketState::ClosingState);
695 d->abort();
696}
697
698void QBluetoothSocket::disconnectFromService()
699{
700 close();
701}
702
703QString QBluetoothSocket::localName() const
704{
705 Q_D(const QBluetoothSocketBase);
706 return d->localName();
707}
708
709QBluetoothAddress QBluetoothSocket::localAddress() const
710{
711 Q_D(const QBluetoothSocketBase);
712 return d->localAddress();
713}
714
715quint16 QBluetoothSocket::localPort() const
716{
717 Q_D(const QBluetoothSocketBase);
718 return d->localPort();
719}
720
721QString QBluetoothSocket::peerName() const
722{
723 Q_D(const QBluetoothSocketBase);
724 return d->peerName();
725}
726
727QBluetoothAddress QBluetoothSocket::peerAddress() const
728{
729 Q_D(const QBluetoothSocketBase);
730 return d->peerAddress();
731}
732
733quint16 QBluetoothSocket::peerPort() const
734{
735 Q_D(const QBluetoothSocketBase);
736 return d->peerPort();
737}
738
739qint64 QBluetoothSocket::writeData(const char *data, qint64 maxSize)
740{
741 Q_D(QBluetoothSocketBase);
742
743 if (!data || maxSize <= 0) {
744 d_ptr->errorString = tr("Invalid data/data size");
745 setSocketError(QBluetoothSocket::SocketError::OperationError);
746 return -1;
747 }
748
749 return d->writeData(data, maxSize);
750}
751
752qint64 QBluetoothSocket::readData(char *data, qint64 maxSize)
753{
754 Q_D(QBluetoothSocketBase);
755 return d->readData(data, maxSize);
756}
757
758void QBluetoothSocket::close()
759{
760 if (state() == SocketState::UnconnectedState)
761 return;
762
763 Q_D(QBluetoothSocketBase);
764 setOpenMode(QIODevice::NotOpen);
765
766 if (state() == SocketState::ServiceLookupState && d->discoveryAgent) {
767 d->discoveryAgent->disconnect();
768 d->discoveryAgent->stop();
769 d->discoveryAgent = nullptr;
770 }
771
772 setSocketState(SocketState::ClosingState);
773
774 d->close();
775}
776
777/*!
778 \fn bool QBluetoothSocket::setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, SocketState socketState, OpenMode openMode)
779
780 Sets the socket to use \a socketDescriptor with a type of \a socketType,
781 which is in state \a socketState, and mode \a openMode.
782
783 The socket descriptor is owned by the QBluetoothSocket instance and may
784 be closed once finished.
785
786 Returns \c true on success.
787*/
788
789// ### Qt 7 consider making this function private. The qbluetoothsocket_bluez backend is the
790// the only backend providing publicly accessible support for this. Other backends implement
791// similarly named, but private, overload
792bool QBluetoothSocket::setSocketDescriptor(int socketDescriptor, QBluetoothServiceInfo::Protocol socketType,
793 SocketState socketState, OpenMode openMode)
794{
795 Q_D(QBluetoothSocketBase);
796 return d->setSocketDescriptor(socketDescriptor, socketType, socketState, openMode);
797}
798
799/*!
800 Returns the platform-specific socket descriptor, if available.
801 This function returns -1 if the descriptor is not available or an error has occurred.
802*/
803
804int QBluetoothSocket::socketDescriptor() const
805{
806 Q_D(const QBluetoothSocketBase);
807 return d->socket;
808}
809
810QT_END_NAMESPACE
811
812#include "moc_qbluetoothsocket.cpp"
Combined button and popup list for selecting options.