QBluetoothSocket Class

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

Header: #include <QBluetoothSocket>
qmake: QT += bluetooth
Since: Qt 5.2
Inherits: QIODevice

This class was introduced in Qt 5.2.

Public Types

enum SocketError { UnknownSocketError, NoSocketError, HostNotFoundError, ServiceNotFoundError, NetworkError, …, RemoteHostClosedError }
enum SocketState { UnconnectedState, ServiceLookupState, ConnectingState, ConnectedState, BoundState, …, ListeningState }

Detailed Description

QBluetoothSocket supports two socket types, L2CAP and RFCOMM.

L2CAP is a low level datagram-oriented Bluetooth socket. Android does not support L2CAP for socket connections.

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 Protocol is not supported on a platform, calling connectToService() will emit a UnsupportedProtocolError error.

Note: QBluetoothSocket does not support synchronous read and write operations. Functions such as waitForReadyRead() and waitForBytesWritten() are not implemented. I/O operations should be performed using readyRead(), read() and 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.

Member Type Documentation

enum QBluetoothSocket::SocketError

This enum describes Bluetooth socket error types.

ConstantValueDescription
QBluetoothSocket::UnknownSocketErrorQAbstractSocket::UnknownSocketErrorAn unknown error has occurred.
QBluetoothSocket::NoSocketError-2No error. Used for testing.
QBluetoothSocket::HostNotFoundErrorQAbstractSocket::HostNotFoundErrorCould not find the remote host.
QBluetoothSocket::ServiceNotFoundErrorQAbstractSocket::SocketAddressNotAvailableErrorCould not find the service UUID on remote host.
QBluetoothSocket::NetworkErrorQAbstractSocket::NetworkErrorAttempt to read or write from socket returned an error
QBluetoothSocket::UnsupportedProtocolError8The Protocol is not supported on this platform.
QBluetoothSocket::OperationErrorQAbstractSocket::OperationErrorAn operation was attempted while the socket was in a state that did not permit it.
QBluetoothSocket::RemoteHostClosedErrorQAbstractSocket::RemoteHostClosedErrorThe remote host closed the connection. This value was introduced by Qt 5.10.

enum QBluetoothSocket::SocketState

This enum describes the state of the Bluetooth socket.

ConstantValueDescription
QBluetoothSocket::UnconnectedStateQAbstractSocket::UnconnectedStateSocket is not connected.
QBluetoothSocket::ServiceLookupStateQAbstractSocket::HostLookupStateSocket is querying connection parameters.
QBluetoothSocket::ConnectingStateQAbstractSocket::ConnectingStateSocket is attempting to connect to a device.
QBluetoothSocket::ConnectedStateQAbstractSocket::ConnectedStateSocket is connected to a device.
QBluetoothSocket::BoundStateQAbstractSocket::BoundStateSocket is bound to a local address and port.
QBluetoothSocket::ClosingStateQAbstractSocket::ClosingStateSocket is connected and will be closed once all pending data is written to the socket.
QBluetoothSocket::ListeningStateQAbstractSocket::ListeningStateSocket is listening for incoming connections.