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.
| Constant | Value | Description |
|---|---|---|
QBluetoothSocket::UnknownSocketError | QAbstractSocket::UnknownSocketError | An unknown error has occurred. |
QBluetoothSocket::NoSocketError | -2 | No error. Used for testing. |
QBluetoothSocket::HostNotFoundError | QAbstractSocket::HostNotFoundError | Could not find the remote host. |
QBluetoothSocket::ServiceNotFoundError | QAbstractSocket::SocketAddressNotAvailableError | Could not find the service UUID on remote host. |
QBluetoothSocket::NetworkError | QAbstractSocket::NetworkError | Attempt to read or write from socket returned an error |
QBluetoothSocket::UnsupportedProtocolError | 8 | The Protocol is not supported on this platform. |
QBluetoothSocket::OperationError | QAbstractSocket::OperationError | An operation was attempted while the socket was in a state that did not permit it. |
QBluetoothSocket::RemoteHostClosedError | QAbstractSocket::RemoteHostClosedError | The 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.
| Constant | Value | Description |
|---|---|---|
QBluetoothSocket::UnconnectedState | QAbstractSocket::UnconnectedState | Socket is not connected. |
QBluetoothSocket::ServiceLookupState | QAbstractSocket::HostLookupState | Socket is querying connection parameters. |
QBluetoothSocket::ConnectingState | QAbstractSocket::ConnectingState | Socket is attempting to connect to a device. |
QBluetoothSocket::ConnectedState | QAbstractSocket::ConnectedState | Socket is connected to a device. |
QBluetoothSocket::BoundState | QAbstractSocket::BoundState | Socket is bound to a local address and port. |
QBluetoothSocket::ClosingState | QAbstractSocket::ClosingState | Socket is connected and will be closed once all pending data is written to the socket. |
QBluetoothSocket::ListeningState | QAbstractSocket::ListeningState | Socket is listening for incoming connections. |