Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QBluetoothSocket Class Reference

\inmodule QtBluetooth More...

#include <qbluetoothsocket.h>

+ Inheritance diagram for QBluetoothSocket:
+ Collaboration diagram for QBluetoothSocket:

Public Types

enum class  SocketState {
  UnconnectedState , ServiceLookupState , ConnectingState , ConnectedState ,
  BoundState , ClosingState , ListeningState
}
 This enum describes the state of the Bluetooth socket. More...
 
enum class  SocketError {
  NoSocketError , UnknownSocketError , RemoteHostClosedError , HostNotFoundError ,
  ServiceNotFoundError , NetworkError , UnsupportedProtocolError , OperationError ,
  MissingPermissionsError
}
 This enum describes Bluetooth socket error types. More...
 
- Public Types inherited from QIODeviceBase
enum  OpenModeFlag {
  NotOpen = 0x0000 , ReadOnly = 0x0001 , WriteOnly = 0x0002 , ReadWrite = ReadOnly | WriteOnly ,
  Append = 0x0004 , Truncate = 0x0008 , Text = 0x0010 , Unbuffered = 0x0020 ,
  NewOnly = 0x0040 , ExistingOnly = 0x0080
}
 This enum is used with QIODevice::open() to describe the mode in which a device is opened. More...
 

Signals

void connected ()
 This signal is emitted when a connection is established.
 
void disconnected ()
 This signal is emitted when the socket is disconnected.
 
void errorOccurred (QBluetoothSocket::SocketError error)
 This signal is emitted when an error occurs.
 
void stateChanged (QBluetoothSocket::SocketState state)
 This signal is emitted when the socket state changes to state.
 
- Signals inherited from QIODevice
void readyRead ()
 This signal is emitted once every time new data is available for reading from the device's current read channel.
 
void channelReadyRead (int channel)
 
void bytesWritten (qint64 bytes)
 This signal is emitted every time a payload of data has been written to the device's current write channel.
 
void channelBytesWritten (int channel, qint64 bytes)
 
void aboutToClose ()
 This signal is emitted when the device is about to close.
 
void readChannelFinished ()
 
- Signals inherited from QObject
void destroyed (QObject *=nullptr)
 This signal is emitted immediately before the object obj is destroyed, after any instances of QPointer have been notified, and cannot be blocked.
 
void objectNameChanged (const QString &objectName, QPrivateSignal)
 This signal is emitted after the object's name has been changed.
 

Public Member Functions

 QBluetoothSocket (QBluetoothServiceInfo::Protocol socketType, QObject *parent=nullptr)
 Constructs a Bluetooth socket of socketType type, with parent.
 
 QBluetoothSocket (QObject *parent=nullptr)
 Constructs a Bluetooth socket with parent.
 
virtual ~QBluetoothSocket ()
 Destroys the Bluetooth socket.
 
void abort ()
 Aborts the current connection and resets the socket.
 
void close () override
 Disconnects the socket's connection with the device.
 
bool isSequential () const override
 \reimp
 
qint64 bytesAvailable () const override
 Returns the number of incoming bytes that are waiting to be read.
 
qint64 bytesToWrite () const override
 Returns the number of bytes that are waiting to be written.
 
bool canReadLine () const override
 Returns true if you can read at least one line from the device.
 
void connectToService (const QBluetoothServiceInfo &service, OpenMode openMode=ReadWrite)
 Attempts to connect to the service described by service.
 
void connectToService (const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode=ReadWrite)
 Attempts to make a connection to the service identified by uuid on the device with address address.
 
void connectToService (const QBluetoothAddress &address, quint16 port, OpenMode openMode=ReadWrite)
 Attempts to make a connection with address on the given port.
 
void connectToService (const QBluetoothAddress &address, QBluetoothUuid::ServiceClassUuid uuid, OpenMode mode=ReadWrite)
 
void disconnectFromService ()
 Attempts to close the socket.
 
QString localName () const
 Returns the name of the local device.
 
QBluetoothAddress localAddress () const
 Returns the address of the local device.
 
quint16 localPort () const
 Returns the port number of the local socket if available, otherwise returns 0.
 
QString peerName () const
 Returns the name of the peer device.
 
QBluetoothAddress peerAddress () const
 Returns the address of the peer device.
 
quint16 peerPort () const
 Return the port number of the peer socket if available, otherwise returns 0.
 
bool setSocketDescriptor (int socketDescriptor, QBluetoothServiceInfo::Protocol socketType, SocketState socketState=SocketState::ConnectedState, OpenMode openMode=ReadWrite)
 Sets the socket to use socketDescriptor with a type of socketType, which is in state socketState, and mode openMode.
 
int socketDescriptor () const
 Returns the platform-specific socket descriptor, if available.
 
QBluetoothServiceInfo::Protocol socketType () const
 Returns the socket type.
 
SocketState state () const
 Returns the current state of the socket.
 
SocketError error () const
 Returns the last error.
 
QString errorString () const
 Returns a user displayable text string for the error.
 
void setPreferredSecurityFlags (QBluetooth::SecurityFlags flags)
 Sets the preferred security parameter for the connection attempt to flags.
 
QBluetooth::SecurityFlags preferredSecurityFlags () const
 Returns the security parameters used for the initial connection attempt.
 
- Public Member Functions inherited from QIODevice
 QIODevice ()
 Constructs a QIODevice object.
 
 QIODevice (QObject *parent)
 Constructs a QIODevice object with the given parent.
 
virtual ~QIODevice ()
 The destructor is virtual, and QIODevice is an abstract base class.
 
QIODeviceBase::OpenMode openMode () const
 Returns the mode in which the device has been opened; i.e.
 
void setTextModeEnabled (bool enabled)
 If enabled is true, this function sets the \l Text flag on the device; otherwise the \l Text flag is removed.
 
bool isTextModeEnabled () const
 Returns true if the \l Text flag is enabled; otherwise returns false.
 
bool isOpen () const
 Returns true if the device is open; otherwise returns false.
 
bool isReadable () const
 Returns true if data can be read from the device; otherwise returns false.
 
bool isWritable () const
 Returns true if data can be written to the device; otherwise returns false.
 
int readChannelCount () const
 
int writeChannelCount () const
 
int currentReadChannel () const
 
void setCurrentReadChannel (int channel)
 
int currentWriteChannel () const
 
void setCurrentWriteChannel (int channel)
 
virtual bool open (QIODeviceBase::OpenMode mode)
 Opens the device and sets its OpenMode to mode.
 
virtual qint64 pos () const
 For random-access devices, this function returns the position that data is written to or read from.
 
virtual qint64 size () const
 For open random-access devices, this function returns the size of the device.
 
virtual bool seek (qint64 pos)
 For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred.
 
virtual bool atEnd () const
 Returns true if the current read and write position is at the end of the device (i.e.
 
virtual bool reset ()
 Seeks to the start of input for random-access devices.
 
qint64 read (char *data, qint64 maxlen)
 Reads at most maxSize bytes from the device into data, and returns the number of bytes read.
 
QByteArray read (qint64 maxlen)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reads at most maxSize bytes from the device, and returns the data read as a QByteArray.
 
QByteArray readAll ()
 Reads all remaining data from the device, and returns it as a byte array.
 
qint64 readLine (char *data, qint64 maxlen)
 This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read.
 
QByteArray readLine (qint64 maxlen=0)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Reads a line from the device, but no more than maxSize characters, and returns the result as a byte array.
 
void startTransaction ()
 
void commitTransaction ()
 
void rollbackTransaction ()
 
bool isTransactionStarted () const
 
qint64 write (const char *data, qint64 len)
 Writes at most maxSize bytes of data from data to the device.
 
qint64 write (const char *data)
 
qint64 write (const QByteArray &data)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Writes the content of data to the device.
 
qint64 peek (char *data, qint64 maxlen)
 
QByteArray peek (qint64 maxlen)
 
qint64 skip (qint64 maxSize)
 
virtual bool waitForReadyRead (int msecs)
 Blocks until new data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed.
 
virtual bool waitForBytesWritten (int msecs)
 For buffered devices, this function waits until a payload of buffered written data has been written to the device and the bytesWritten() signal has been emitted, or until msecs milliseconds have passed.
 
void ungetChar (char c)
 Puts the character c back into the device, and decrements the current position unless the position is 0.
 
bool putChar (char c)
 Writes the character c to the device.
 
bool getChar (char *c)
 Reads one character from the device and stores it in c.
 
QString errorString () const
 Returns a human-readable description of the last device error that occurred.
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent=nullptr)
 Constructs an object with parent object parent.
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects.
 
virtual bool event (QEvent *event)
 This virtual function receives events to an object and should return true if the event e was recognized and processed.
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 Filters events if this object has been installed as an event filter for the watched object.
 
QString objectName () const
 
Q_WEAK_OVERLOAD void setObjectName (const QString &name)
 Sets the object's name to name.
 
void setObjectName (QAnyStringView name)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
QBindable< QStringbindableObjectName ()
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false.
 
bool isWindowType () const
 Returns true if the object is a window; otherwise returns false.
 
bool isQuickItemType () const
 Returns true if the object is a QQuickItem; otherwise returns false.
 
bool signalsBlocked () const noexcept
 Returns true if signals are blocked; otherwise returns false.
 
bool blockSignals (bool b) noexcept
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it).
 
QThreadthread () const
 Returns the thread in which the object lives.
 
bool moveToThread (QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
 Changes the thread affinity for this object and its children and returns true on success.
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 This is an overloaded function that will start a timer of type timerType and a timeout of interval milliseconds.
 
int startTimer (std::chrono::nanoseconds time, Qt::TimerType timerType=Qt::CoarseTimer)
 
void killTimer (int id)
 Kills the timer with timer identifier, id.
 
void killTimer (Qt::TimerId id)
 
template<typename T >
findChild (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns the child of this object that can be cast into type T and that is called name, or \nullptr if there is no such object.
 
template<typename T >
QList< T > findChildren (QAnyStringView aName, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects.
 
template<typename T >
findChild (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
template<typename T >
QList< T > findChildren (Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
const QObjectListchildren () const
 Returns a list of child objects.
 
void setParent (QObject *parent)
 Makes the object a child of parent.
 
void installEventFilter (QObject *filterObj)
 Installs an event filter filterObj on this object.
 
void removeEventFilter (QObject *obj)
 Removes an event filter object obj from this object.
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=nullptr, const QObject *receiver=nullptr, const char *member=nullptr) const
 
bool disconnect (const QObject *receiver, const char *member=nullptr) const
 
void dumpObjectTree () const
 Dumps a tree of children to the debug output.
 
void dumpObjectInfo () const
 Dumps information about signal connections, etc.
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value.
 
bool setProperty (const char *name, QVariant &&value)
 
QVariant property (const char *name) const
 Returns the value of the object's name property.
 
QList< QByteArraydynamicPropertyNames () const
 
QBindingStoragebindingStorage ()
 
const QBindingStoragebindingStorage () const
 
QObjectparent () const
 Returns a pointer to the parent object.
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false.
 

Protected Member Functions

qint64 readData (char *data, qint64 maxSize) override
 \reimp
 
qint64 writeData (const char *data, qint64 maxSize) override
 \reimp
 
void setSocketState (SocketState state)
 Sets the socket state to state.
 
void setSocketError (SocketError error)
 Sets the type of error that last occurred to error_.
 
void doDeviceDiscovery (const QBluetoothServiceInfo &service, OpenMode openMode)
 Start device discovery for service and open the socket with openMode.
 
- Protected Member Functions inherited from QIODevice
 QIODevice (QIODevicePrivate &dd, QObject *parent=nullptr)
 
virtual qint64 readLineData (char *data, qint64 maxlen)
 Reads up to maxSize characters into data and returns the number of characters read.
 
virtual qint64 skipData (qint64 maxSize)
 
void setOpenMode (QIODeviceBase::OpenMode openMode)
 Sets the OpenMode of the device to openMode.
 
void setErrorString (const QString &errorString)
 Sets the human readable description of the last device error that occurred to str.
 
- Protected Member Functions inherited from QObject
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns \nullptr.
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal.
 
bool isSignalConnected (const QMetaMethod &signal) const
 
virtual void timerEvent (QTimerEvent *event)
 This event handler can be reimplemented in a subclass to receive timer events for the object.
 
virtual void childEvent (QChildEvent *event)
 This event handler can be reimplemented in a subclass to receive child events.
 
virtual void customEvent (QEvent *event)
 This event handler can be reimplemented in a subclass to receive custom events.
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
 QObject (QObjectPrivate &dd, QObject *parent=nullptr)
 
- Protected Member Functions inherited from QIODeviceBase
 ~QIODeviceBase ()=default
 

Protected Attributes

QBluetoothSocketBasePrivated_ptr
 
- Protected Attributes inherited from QObject
QScopedPointer< QObjectDatad_ptr
 

Friends

class QBluetoothServer
 
class QBluetoothServerPrivate
 
class QBluetoothSocketPrivateDarwin
 
class QBluetoothSocketPrivateAndroid
 
class QBluetoothSocketPrivateBluez
 
class QBluetoothSocketPrivateBluezDBus
 
class QBluetoothSocketPrivateDummy
 
class QBluetoothSocketPrivateWin
 
class QBluetoothSocketPrivateWinRT
 
class QLowEnergyControllerPrivateBluez
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 \threadsafe
 
- Static Public Member Functions inherited from QObject
static QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 \threadsafe
 
static QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::ContextTypeForFunctor< Func2 >::ContextType *context, Func2 &&slot, Qt::ConnectionType type=Qt::AutoConnection)
 
template<typename Func1 , typename Func2 >
static QMetaObject::Connection connect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, Func2 &&slot)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 \threadsafe
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static bool disconnect (const QMetaObject::Connection &)
 Disconnect a connection.
 
template<typename Func1 , typename Func2 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiver, Func2 slot)
 
template<typename Func1 >
static bool disconnect (const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const QObject *receiver, void **zero)
 
- Properties inherited from QObject
QString objectName
 the name of this object
 

Detailed Description

\inmodule QtBluetooth

The QBluetoothSocket class enables connection to a Bluetooth device running a bluetooth server.

Since
5.2

QBluetoothSocket supports two socket types, \l {QBluetoothServiceInfo::L2capProtocol}{L2CAP} and \l {QBluetoothServiceInfo::RfcommProtocol}{RFCOMM}.

\l {QBluetoothServiceInfo::L2capProtocol}{L2CAP} is a low level datagram-oriented Bluetooth socket. Android does not support \l {QBluetoothServiceInfo::L2capProtocol}{L2CAP} for socket connections.

\l {QBluetoothServiceInfo::RfcommProtocol}{RFCOMM} is a reliable, stream-oriented socket. RFCOMM sockets emulate an RS-232 serial port.

To create a connection to a Bluetooth service, create a socket of the appropriate type and call connectToService() passing the Bluetooth address and port number. QBluetoothSocket will emit the connected() signal when the connection is established.

If the \l {QBluetoothServiceInfo::Protocol}{Protocol} is not supported on a platform, calling \l connectToService() will emit a \l {QBluetoothSocket::SocketError::UnsupportedProtocolError}{UnsupportedProtocolError} error.

Note
QBluetoothSocket does not support synchronous read and write operations. Functions such as \l waitForReadyRead() and \l waitForBytesWritten() are not implemented. I/O operations should be performed using \l readyRead(), \l read() and \l write().

On iOS, this class cannot be used because the platform does not expose an API which may permit access to QBluetoothSocket related features.

Note
On macOS Monterey (12) the socket data flow is paused when a modal dialogue is executing, or an event tracking mode is entered (for example by long-pressing a Window close button). This may change in the future releases of macOS.

Definition at line 21 of file qbluetoothsocket.h.

Member Enumeration Documentation

◆ SocketError

enum class QBluetoothSocket::SocketError
strong

This enum describes Bluetooth socket error types.

\value UnknownSocketError An unknown error has occurred. \value NoSocketError No error. Used for testing. \value HostNotFoundError Could not find the remote host. \value ServiceNotFoundError Could not find the service UUID on remote host. \value NetworkError Attempt to read or write from socket returned an error \value UnsupportedProtocolError The \l {QBluetoothServiceInfo::Protocol}{Protocol} is not supported on this platform. \value OperationError An operation was attempted while the socket was in a state that did not permit it. \value [since 5.10] RemoteHostClosedError The remote host closed the connection. \value [since 6.4] MissingPermissionsError The operating system requests permissions which were not granted by the user.

Enumerator
NoSocketError 
UnknownSocketError 
RemoteHostClosedError 
HostNotFoundError 
ServiceNotFoundError 
NetworkError 
UnsupportedProtocolError 
OperationError 
MissingPermissionsError 

Definition at line 50 of file qbluetoothsocket.h.

◆ SocketState

enum class QBluetoothSocket::SocketState
strong

This enum describes the state of the Bluetooth socket.

\value UnconnectedState Socket is not connected. \value ServiceLookupState Socket is querying connection parameters. \value ConnectingState Socket is attempting to connect to a device. \value ConnectedState Socket is connected to a device. \value BoundState Socket is bound to a local address and port. \value ClosingState Socket is connected and will be closed once all pending data is written to the socket. \value ListeningState Socket is listening for incoming connections.

Enumerator
UnconnectedState 
ServiceLookupState 
ConnectingState 
ConnectedState 
BoundState 
ClosingState 
ListeningState 

Definition at line 39 of file qbluetoothsocket.h.

Constructor & Destructor Documentation

◆ QBluetoothSocket() [1/2]

QBluetoothSocket::QBluetoothSocket ( QBluetoothServiceInfo::Protocol socketType,
QObject * parent = nullptr )
explicit

Constructs a Bluetooth socket of socketType type, with parent.

Definition at line 254 of file qbluetoothsocket.cpp.

References createSocketPrivate(), d, d_ptr, QIODeviceBase::NotOpen, QBluetoothSocketBasePrivate::q_ptr, QIODevice::setOpenMode(), and socketType().

+ Here is the call graph for this function:

◆ QBluetoothSocket() [2/2]

QBluetoothSocket::QBluetoothSocket ( QObject * parent = nullptr)
explicit

Constructs a Bluetooth socket with parent.

Definition at line 269 of file qbluetoothsocket.cpp.

References createSocketPrivate(), d_ptr, QIODeviceBase::NotOpen, QBluetoothSocketBasePrivate::q_ptr, and QIODevice::setOpenMode().

+ Here is the call graph for this function:

◆ ~QBluetoothSocket()

QBluetoothSocket::~QBluetoothSocket ( )
virtual

Destroys the Bluetooth socket.

Definition at line 301 of file qbluetoothsocket.cpp.

References d_ptr.

Member Function Documentation

◆ abort()

void QBluetoothSocket::abort ( )

Aborts the current connection and resets the socket.

Unlike disconnectFromService(), this function immediately closes the socket, discarding any pending data in the write buffer.

Note
On Android, aborting the socket requires asynchronous interaction with Android threads. Therefore the associated \l disconnected() and \l stateChanged() signals are delayed until the threads have finished the closure.
See also
disconnectFromService(), close()

Definition at line 657 of file qbluetoothsocket.cpp.

References ClosingState, d, QIODeviceBase::NotOpen, ServiceLookupState, QIODevice::setOpenMode(), setSocketState(), state(), and UnconnectedState.

+ Here is the call graph for this function:

◆ bytesAvailable()

qint64 QBluetoothSocket::bytesAvailable ( ) const
overridevirtual

Returns the number of incoming bytes that are waiting to be read.

See also
bytesToWrite(), read()

Reimplemented from QIODevice.

Definition at line 320 of file qbluetoothsocket.cpp.

References QIODevice::bytesAvailable(), and d.

+ Here is the call graph for this function:

◆ bytesToWrite()

qint64 QBluetoothSocket::bytesToWrite ( ) const
overridevirtual

Returns the number of bytes that are waiting to be written.

The bytes are written when control goes back to the event loop.

Reimplemented from QIODevice.

Definition at line 330 of file qbluetoothsocket.cpp.

References d.

◆ canReadLine()

bool QBluetoothSocket::canReadLine ( ) const
overridevirtual

Returns true if you can read at least one line from the device.

Reimplemented from QIODevice.

Definition at line 562 of file qbluetoothsocket.cpp.

References QIODevice::canReadLine(), and d.

+ Here is the call graph for this function:

◆ close()

void QBluetoothSocket::close ( )
overridevirtual

Disconnects the socket's connection with the device.

Note
On Android, closing the socket requires asynchronous interaction with Android threads. Therefore the associated \l disconnected() and \l stateChanged() signals are delayed until the threads have finished the closure.

Reimplemented from QIODevice.

Definition at line 735 of file qbluetoothsocket.cpp.

References ClosingState, d, QIODeviceBase::NotOpen, ServiceLookupState, QIODevice::setOpenMode(), setSocketState(), state(), and UnconnectedState.

Referenced by QLowEnergyControllerPrivateBluez::disconnectFromDevice(), and disconnectFromService().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ connected

void QBluetoothSocket::connected ( )
signal

This signal is emitted when a connection is established.

See also
QBluetoothSocket::SocketState::ConnectedState, stateChanged()

Referenced by setSocketState().

+ Here is the caller graph for this function:

◆ connectToService() [1/4]

void QBluetoothSocket::connectToService ( const QBluetoothAddress & address,
const QBluetoothUuid & uuid,
OpenMode openMode = ReadWrite )

Attempts to make a connection to the service identified by uuid on the device with address address.

The socket is opened in the given openMode.

For BlueZ, the socket first enters the \l ServiceLookupState and queries the connection parameters for uuid. If the service parameters are successfully retrieved the socket enters ConnectingState, and attempts to connect to address. If a connection is established, QBluetoothSocket enters \l ConnectedState and emits connected().

On Android, the service connection can directly be established using the UUID of the remote service. Therefore the platform does not require the \l ServiceLookupState and \l socketType() is always set to \l QBluetoothServiceInfo::RfcommProtocol.

At any point, the socket can emit errorOccurred() to signal that an error occurred.

Note that most platforms require a pairing prior to connecting to the remote device. Otherwise the connection process may fail.

See also
state(), disconnectFromService()

Definition at line 393 of file qbluetoothsocket.cpp.

References d, and QIODevice::openMode().

+ Here is the call graph for this function:

◆ connectToService() [2/4]

void QBluetoothSocket::connectToService ( const QBluetoothAddress & address,
QBluetoothUuid::ServiceClassUuid uuid,
OpenMode mode = ReadWrite )
inline

Exists to avoid QTBUG-65831.

Definition at line 81 of file qbluetoothsocket.h.

◆ connectToService() [3/4]

void QBluetoothSocket::connectToService ( const QBluetoothAddress & address,
quint16 port,
OpenMode openMode = ReadWrite )

Attempts to make a connection with address on the given port.

The socket is opened in the given openMode.

The socket first enters ConnectingState, and attempts to connect to address. If a connection is established, QBluetoothSocket enters ConnectedState and emits connected().

At any point, the socket can emit errorOccurred() to signal that an error occurred.

On Android and BlueZ (version 5.46 or above), a connection to a service can not be established using a port. Calling this function will emit a \l {QBluetoothSocket::SocketError::ServiceNotFoundError}{ServiceNotFoundError}.

Note that most platforms require a pairing prior to connecting to the remote device. Otherwise the connection process may fail.

See also
state(), disconnectFromService()

Definition at line 417 of file qbluetoothsocket.cpp.

References d, and QIODevice::openMode().

+ Here is the call graph for this function:

◆ connectToService() [4/4]

void QBluetoothSocket::connectToService ( const QBluetoothServiceInfo & service,
OpenMode openMode = ReadWrite )

Attempts to connect to the service described by service.

The socket is opened in the given openMode. The \l socketType() is ignored if service specifies a differing \l QBluetoothServiceInfo::socketProtocol().

The socket first enters ConnectingState and attempts to connect to the device providing service. If a connection is established, QBluetoothSocket enters ConnectedState and emits connected().

At any point, the socket can emit errorOccurred() to signal that an error occurred.

Note that most platforms require a pairing prior to connecting to the remote device. Otherwise the connection process may fail.

On Android, only RFCOMM connections are possible. This function ignores any socket protocol indicator and assumes RFCOMM.

See also
state(), disconnectFromService()

Definition at line 356 of file qbluetoothsocket.cpp.

References d, and QIODevice::openMode().

+ Here is the call graph for this function:

◆ disconnected

void QBluetoothSocket::disconnected ( )
signal

This signal is emitted when the socket is disconnected.

See also
QBluetoothSocket::SocketState::UnconnectedState, stateChanged()

Referenced by setSocketState().

+ Here is the caller graph for this function:

◆ disconnectFromService()

void QBluetoothSocket::disconnectFromService ( )

Attempts to close the socket.

If there is pending data waiting to be written QBluetoothSocket will enter ClosingState and wait until all data has been written. Eventually, it will enter UnconnectedState and emit the disconnected() signal.

See also
connectToService()

Definition at line 675 of file qbluetoothsocket.cpp.

References close().

+ Here is the call graph for this function:

◆ doDeviceDiscovery()

void QBluetoothSocket::doDeviceDiscovery ( const QBluetoothServiceInfo & service,
OpenMode openMode )
protected

Start device discovery for service and open the socket with openMode.

If the socket is created with a service uuid device address, use service discovery to find the port number to connect to.

Definition at line 584 of file qbluetoothsocket.cpp.

References QObject::connect(), d, QBluetoothServiceDiscoveryAgent::finished(), QBluetoothServiceDiscoveryAgent::FullDiscovery, QIODevice::openMode(), Q_ASSERT, qCDebug, QBluetoothServiceDiscoveryAgent::serviceDiscovered(), ServiceLookupState, and setSocketState().

+ Here is the call graph for this function:

◆ error()

QBluetoothSocket::SocketError QBluetoothSocket::error ( ) const

Returns the last error.

Definition at line 448 of file qbluetoothsocket.cpp.

References d.

◆ errorOccurred

void QBluetoothSocket::errorOccurred ( QBluetoothSocket::SocketError error)
signal

This signal is emitted when an error occurs.

See also
error()
Since
6.2

Referenced by setSocketError().

+ Here is the caller graph for this function:

◆ errorString()

QString QBluetoothSocket::errorString ( ) const

Returns a user displayable text string for the error.

Definition at line 457 of file qbluetoothsocket.cpp.

References d.

◆ isSequential()

bool QBluetoothSocket::isSequential ( ) const
overridevirtual

\reimp

Reimplemented from QIODevice.

Definition at line 310 of file qbluetoothsocket.cpp.

◆ localAddress()

QBluetoothAddress QBluetoothSocket::localAddress ( ) const

Returns the address of the local device.

Although some platforms may differ the socket must generally be connected to guarantee the return of a valid address. In particular, this is true when dealing with platforms that support multiple local Bluetooth adapters.

Definition at line 686 of file qbluetoothsocket.cpp.

References d.

◆ localName()

QString QBluetoothSocket::localName ( ) const

Returns the name of the local device.

Although some platforms may differ the socket must generally be connected to guarantee the return of a valid name. In particular, this is true when dealing with platforms that support multiple local Bluetooth adapters.

Definition at line 680 of file qbluetoothsocket.cpp.

References d.

◆ localPort()

quint16 QBluetoothSocket::localPort ( ) const

Returns the port number of the local socket if available, otherwise returns 0.

Although some platforms may differ the socket must generally be connected to guarantee the return of a valid port number.

On Android and \macos, this feature is not supported and returns 0.

Definition at line 692 of file qbluetoothsocket.cpp.

References d.

◆ peerAddress()

QBluetoothAddress QBluetoothSocket::peerAddress ( ) const

Returns the address of the peer device.

Definition at line 704 of file qbluetoothsocket.cpp.

References d.

◆ peerName()

QString QBluetoothSocket::peerName ( ) const

Returns the name of the peer device.

Definition at line 698 of file qbluetoothsocket.cpp.

References d.

◆ peerPort()

quint16 QBluetoothSocket::peerPort ( ) const

Return the port number of the peer socket if available, otherwise returns 0.

On Android, this feature is not supported.

Definition at line 710 of file qbluetoothsocket.cpp.

References d.

◆ preferredSecurityFlags()

QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags ( ) const

Returns the security parameters used for the initial connection attempt.

The security parameters may be renegotiated between the two parties during or after the connection has been established. If such a change happens it is not reflected in the value of this flag.

On \macos, this flag is always set to \l QBluetooth::Security::Secure.

See also
setPreferredSecurityFlags()
Since
5.6

Definition at line 515 of file qbluetoothsocket.cpp.

References d, and QBluetooth::Secure.

◆ readData()

qint64 QBluetoothSocket::readData ( char * data,
qint64 maxSize )
overrideprotectedvirtual

\reimp

Implements QIODevice.

Definition at line 729 of file qbluetoothsocket.cpp.

References d.

◆ setPreferredSecurityFlags()

void QBluetoothSocket::setPreferredSecurityFlags ( QBluetooth::SecurityFlags flags)

Sets the preferred security parameter for the connection attempt to flags.

This value is incorporated when calling \l connectToService(). Therefore it is required to reconnect to change this parameter for an existing connection.

On Bluez this property is set to QBluetooth::Security::Authorization by default.

On \macos, this value is ignored as the platform does not permit access to the security parameter of the socket. By default the platform prefers secure/encrypted connections though and therefore this function always returns \l QBluetooth::Security::Secure.

Android only supports two levels of security (secure and non-secure). If this flag is set to \l QBluetooth::Security::NoSecurity the socket object will not employ any authentication or encryption. Any other security flag combination will trigger a secure Bluetooth connection. This flag is set to \l QBluetooth::Security::Secure by default.

Note
A secure connection requires a pairing between the two devices. On some platforms, the pairing is automatically initiated during the establishment of the connection. Other platforms require the application to manually trigger the pairing before attempting to connect.
See also
preferredSecurityFlags()
Since
5.6

Definition at line 491 of file qbluetoothsocket.cpp.

References d.

◆ setSocketDescriptor()

bool QBluetoothSocket::setSocketDescriptor ( int socketDescriptor,
QBluetoothServiceInfo::Protocol socketType,
SocketState socketState = SocketState::ConnectedState,
OpenMode openMode = ReadWrite )

Sets the socket to use socketDescriptor with a type of socketType, which is in state socketState, and mode openMode.

The socket descriptor is owned by the QBluetoothSocket instance and may be closed once finished.

Returns true on success.

Definition at line 769 of file qbluetoothsocket.cpp.

References d, QIODevice::openMode(), socketDescriptor(), and socketType().

+ Here is the call graph for this function:

◆ setSocketError()

void QBluetoothSocket::setSocketError ( QBluetoothSocket::SocketError error_)
protected

Sets the type of error that last occurred to error_.

Definition at line 571 of file qbluetoothsocket.cpp.

References d, emit, and errorOccurred().

Referenced by writeData().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setSocketState()

void QBluetoothSocket::setSocketState ( QBluetoothSocket::SocketState state)
protected

Sets the socket state to state.

Definition at line 529 of file qbluetoothsocket.cpp.

References ClosingState, connected(), ConnectedState, d, disconnected(), emit, ListeningState, qCWarning, state(), stateChanged(), and UnconnectedState.

Referenced by abort(), close(), and doDeviceDiscovery().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ socketDescriptor()

int QBluetoothSocket::socketDescriptor ( ) const

Returns the platform-specific socket descriptor, if available.

This function returns -1 if the descriptor is not available or an error has occurred.

Definition at line 781 of file qbluetoothsocket.cpp.

References d.

Referenced by setSocketDescriptor().

+ Here is the caller graph for this function:

◆ socketType()

QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType ( ) const

Returns the socket type.

The socket automatically adjusts to the protocol offered by the remote service.

Android only support \l{QBluetoothServiceInfo::RfcommProtocol}{RFCOMM} based sockets.

Definition at line 430 of file qbluetoothsocket.cpp.

References d.

Referenced by QBluetoothSocket(), and setSocketDescriptor().

+ Here is the caller graph for this function:

◆ state()

QBluetoothSocket::SocketState QBluetoothSocket::state ( ) const

Returns the current state of the socket.

Definition at line 439 of file qbluetoothsocket.cpp.

References d.

Referenced by abort(), close(), and setSocketState().

+ Here is the caller graph for this function:

◆ stateChanged

QBluetoothSocket::stateChanged ( QBluetoothSocket::SocketState state)
signal

This signal is emitted when the socket state changes to state.

See also
connected(), disconnected(), state(), QBluetoothSocket::SocketState

Referenced by setSocketState().

+ Here is the caller graph for this function:

◆ writeData()

qint64 QBluetoothSocket::writeData ( const char * data,
qint64 maxSize )
overrideprotectedvirtual

\reimp

Implements QIODevice.

Definition at line 716 of file qbluetoothsocket.cpp.

References d, d_ptr, QBluetoothSocketBasePrivate::errorString, OperationError, setSocketError(), and tr.

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QBluetoothServer

friend class QBluetoothServer
friend

Definition at line 27 of file qbluetoothsocket.h.

◆ QBluetoothServerPrivate

friend class QBluetoothServerPrivate
friend

Definition at line 28 of file qbluetoothsocket.h.

◆ QBluetoothSocketPrivateAndroid

friend class QBluetoothSocketPrivateAndroid
friend

Definition at line 30 of file qbluetoothsocket.h.

◆ QBluetoothSocketPrivateBluez

friend class QBluetoothSocketPrivateBluez
friend

Definition at line 31 of file qbluetoothsocket.h.

◆ QBluetoothSocketPrivateBluezDBus

friend class QBluetoothSocketPrivateBluezDBus
friend

Definition at line 32 of file qbluetoothsocket.h.

◆ QBluetoothSocketPrivateDarwin

friend class QBluetoothSocketPrivateDarwin
friend

Definition at line 29 of file qbluetoothsocket.h.

◆ QBluetoothSocketPrivateDummy

friend class QBluetoothSocketPrivateDummy
friend

Definition at line 33 of file qbluetoothsocket.h.

◆ QBluetoothSocketPrivateWin

friend class QBluetoothSocketPrivateWin
friend

Definition at line 34 of file qbluetoothsocket.h.

◆ QBluetoothSocketPrivateWinRT

friend class QBluetoothSocketPrivateWinRT
friend

Definition at line 35 of file qbluetoothsocket.h.

◆ QLowEnergyControllerPrivateBluez

friend class QLowEnergyControllerPrivateBluez
friend

Definition at line 151 of file qbluetoothsocket.h.

Member Data Documentation

◆ d_ptr


The documentation for this class was generated from the following files: