Since: 1.2

#include <QtConnectivity/QBluetoothSocket>

The QBluetoothSocket class provides a Bluetooth socket.

QBluetoothSocket supports two socket types, {QBluetoothSocket::L2capSocket}{L2CAP} and {QBluetoothSocket::RfcommSocket}{RFCOMM}.

{QBluetoothSocket::L2capSocket}{L2CAP} is a low level datagram-oriented Bluetooth socket.

{QBluetoothSocket::RfcommSocket}{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.




Public Types Index

enum SocketError

NoSocketError -2, UnknownSocketError QAbstractSocket::UnknownSocketError, ConnectionRefusedError QAbstractSocket::ConnectionRefusedError, RemoteHostClosedError QAbstractSocket::RemoteHostClosedError, HostNotFoundError QAbstractSocket::HostNotFoundError, ServiceNotFoundError QAbstractSocket::SocketAddressNotAvailableError, NetworkError QAbstractSocket::NetworkError

enum SocketState

UnconnectedState QAbstractSocket::UnconnectedState, ServiceLookupState QAbstractSocket::HostLookupState, ConnectingState QAbstractSocket::ConnectingState, ConnectedState QAbstractSocket::ConnectedState, BoundState QAbstractSocket::BoundState, ClosingState QAbstractSocket::ClosingState, ListeningState QAbstractSocket::ListeningState

enum SocketType

UnknownSocketType -1, L2capSocket, RfcommSocket

enum OpenModeFlag

NotOpen 0x0000, ReadOnly 0x0001, WriteOnly 0x0002, ReadWrite ReadOnly | WriteOnly, Append 0x0004, Truncate 0x0008, Text 0x0010, Unbuffered 0x0020

Protected Attributes Index

QBluetoothSocketPrivate *d_ptr

Public Functions Index

QBluetoothSocket (SocketType socketType, QObject *parent=0)
QBluetoothSocket (QObject *parent=0)
virtual ~QBluetoothSocket ()
voidabort ()
virtual qint64bytesAvailable () const
virtual qint64bytesToWrite () const
virtual boolcanReadLine () const
virtual voidclose ()
voidconnectToService (const QBluetoothServiceInfo &service, OpenMode openMode=ReadWrite)
voidconnectToService (const QBluetoothAddress &address, const QBluetoothUuid &uuid, OpenMode openMode=ReadWrite)
voidconnectToService (const QBluetoothAddress &address, quint16 port, OpenMode openMode=ReadWrite)
voiddisconnectFromService ()
SocketErrorerror () const
QStringerrorString () const
virtual boolisSequential () const
QBluetoothAddresslocalAddress () const
QStringlocalName () const
quint16localPort () const
QBluetoothAddresspeerAddress () const
QStringpeerName () const
quint16peerPort () const
boolsetSocketDescriptor (int socketDescriptor, SocketType socketType, SocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
intsocketDescriptor () const
SocketTypesocketType () const
SocketStatestate () const
virtual boolatEnd () const Inherited
boolgetChar (char *c)Inherited
boolisOpen () const Inherited
boolisReadable () const Inherited
boolisTextModeEnabled () const Inherited
boolisWritable () const Inherited
virtual boolopen (OpenMode mode)Inherited
OpenModeopenMode () const Inherited
qint64peek (char *data, qint64 maxlen)Inherited
QByteArraypeek (qint64 maxlen)Inherited
virtual qint64pos () const Inherited
boolputChar (char c)Inherited
QIODevice ()Inherited
QIODevice (QObject *parent)Inherited
qint64read (char *data, qint64 maxlen)Inherited
QByteArrayread (qint64 maxlen)Inherited
QByteArrayreadAll ()Inherited
qint64readLine (char *data, qint64 maxlen)Inherited
QByteArrayreadLine (qint64 maxlen=0)Inherited
virtual boolreset ()Inherited
virtual boolseek (qint64 pos)Inherited
voidsetTextModeEnabled (bool enabled)Inherited
virtual qint64size () const Inherited
voidungetChar (char c)Inherited
virtual boolwaitForBytesWritten (int msecs)Inherited
virtual boolwaitForReadyRead (int msecs)Inherited
qint64write (const char *data, qint64 len)Inherited
qint64write (const char *data)Inherited
qint64write (const QByteArray &data)Inherited

Protected Functions Index

voiddoDeviceDiscovery (const QBluetoothServiceInfo &service, OpenMode openMode)
virtual qint64readData (char *data, qint64 maxSize)
voidsetSocketError (SocketError error)
voidsetSocketState (SocketState state)
virtual qint64writeData (const char *data, qint64 maxSize)
QIODevice (QIODevicePrivate &dd, QObject *parent=0)Inherited
virtual qint64readLineData (char *data, qint64 maxlen)Inherited
voidsetErrorString (const QString &errorString)Inherited
voidsetOpenMode (OpenMode openMode)Inherited

Signals Index

voidconnected ()
voiddisconnected ()
voiderror (QBluetoothSocket::SocketError error)
voidstateChanged (QBluetoothSocket::SocketState state)
voidaboutToClose ()Inherited
voidbytesWritten (qint64 bytes)Inherited
voidreadChannelFinished ()Inherited
voidreadyRead ()Inherited

Public Types


This enum describes Bluetooth socket error types.

UnknownSocketError An unknown error has occurred. NoSocketError No error. Used for testing. ConnectionRefusedError Connection refused or device not available. RemoteHostClosedError The remote host closed the socket HostNotFoundError Could not find the remote host ServiceNotFoundError Could not find the service UUID on remote host NetworkError Attempt to read or write from socket returned an error

NoSocketError -2
UnknownSocketError QAbstractSocket::UnknownSocketError
ConnectionRefusedError QAbstractSocket::ConnectionRefusedError
RemoteHostClosedError QAbstractSocket::RemoteHostClosedError
HostNotFoundError QAbstractSocket::HostNotFoundError
ServiceNotFoundError QAbstractSocket::SocketAddressNotAvailableError
NetworkError QAbstractSocket::NetworkError


This enum describes the state of the Bluetooth socket.

UnconnectedState Socket is not connected. ServiceLookupState Socket is querying connection parameters. ConnectingState Socket is attempting to connect to a device. ConnectedState Socket is connected to a device. BoundState 242.nmp.nokia.com _IceTransSocketUNIXConnect: Cannot connect to non-local host saisd Socket is bound to a local address and port. ClosingState Socket is connected and will be closed once all pending data is written to the socket. ListeningState Socket is listening for incoming connections.

UnconnectedState QAbstractSocket::UnconnectedState
ServiceLookupState QAbstractSocket::HostLookupState
ConnectingState QAbstractSocket::ConnectingState
ConnectedState QAbstractSocket::ConnectedState
BoundState QAbstractSocket::BoundState
ClosingState QAbstractSocket::ClosingState
ListeningState QAbstractSocket::ListeningState


This enum describes the Bluetooth socket type.

UnknownSocketType Unknown socket type. L2capSocket L2CAP socket. RfcommSocket RFCOMM socket.

UnknownSocketType -1

OpenModeFlag Inherited

NotOpen 0x0000
ReadOnly 0x0001
WriteOnly 0x0002
ReadWrite ReadOnly | WriteOnly
Append 0x0004
Truncate 0x0008
Text 0x0010
Unbuffered 0x0020

Protected Attributes

QBluetoothSocketPrivate * d_ptr ()

Public Functions

QBluetoothSocket (

Constructs a Bluetooth socket of socketType type, with parent.

QBluetoothSocket (

Constructs a Bluetooth socket with parent.

virtual~QBluetoothSocket ()


void abort ()

Aborts the current connection and resets the socket.

virtual qint64 bytesAvailable ()

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

See also:

bytesToWrite(), read()

virtual qint64 bytesToWrite ()

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

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

virtual bool canReadLine ()

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

virtual void close ()

Disconnects the socket's connection with the device.

void connectToService (

Attempts to connect to the service described by service.

The socket is opened in the given openMode.

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 error() to siganl that an error occurred.

void connectToService (

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.

The socket first enters the 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 Connected State and emits connected().

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

void connectToService (

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 error() to signal that an error occurred.

void 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.

SocketError error ()

Returns the last error.

QString errorString ()

Returns a user displayable text string for the error.

virtual bool isSequential ()

QBluetoothAddress localAddress ()

Returns the address of the local device.

QString localName ()

Returns the name of the local device.

quint16 localPort ()

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

QBluetoothAddress peerAddress ()

Returns the address of the peer device.

QString peerName ()

Returns the name of the peer device.

quint16 peerPort ()

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

bool setSocketDescriptor (

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

Returns true on success

int socketDescriptor ()

Returns the platform specific socket descriptor, if available.

SocketType socketType ()

Returns the socket type.

SocketState state ()

Returns the current state of the socket.

virtual bool atEnd ()Inherited

bool getChar (
  • char *c)

bool isOpen ()Inherited

bool isReadable ()Inherited

bool isTextModeEnabled ()Inherited

bool isWritable ()Inherited

virtual bool open (
  • OpenModemode)

OpenMode openMode ()Inherited

qint64 peek (
  • char *data,
  • qint64maxlen )

QByteArray peek (
  • qint64maxlen)

virtual qint64 pos ()Inherited

bool putChar (
  • charc)

QIODevice ()Inherited

QIODevice (Inherited

qint64 read (
  • char *data,
  • qint64maxlen )

QByteArray read (
  • qint64maxlen)

QByteArray readAll ()Inherited

qint64 readLine (
  • char *data,
  • qint64maxlen )

QByteArray readLine (
  • qint64maxlen)

virtual bool reset ()Inherited

virtual bool seek (
  • qint64pos)

void setTextModeEnabled (
  • boolenabled)

virtual qint64 size ()Inherited

void ungetChar (
  • charc)

virtual bool waitForBytesWritten (
  • intmsecs)

virtual bool waitForReadyRead (
  • intmsecs)

qint64 write (
  • const char *data,
  • qint64len )

qint64 write (
  • const char *data)

qint64 write (Inherited

Protected Functions

void doDeviceDiscovery (

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

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

virtual qint64 readData (
  • char *data,
  • qint64maxSize )

void setSocketError (

Sets the type of error that last occurred to error_.

void setSocketState (

Sets the socket state to state.

virtual qint64 writeData (
  • const char *data,
  • qint64maxSize )

QIODevice (
  • QIODevicePrivate &dd,
  • QObject *parent )

virtual qint64 readLineData (
  • char *data,
  • qint64maxlen )

void setErrorString (Inherited

void setOpenMode (
  • OpenModeopenMode)


void connected ()

This signal is emitted when a connection is established.

See also:

QBluetoothSocket::ConnectedState, stateChanged()

void disconnected ()

This signal is emitted when the socket is disconnected.

See also:

QBluetoothSocket::UnconnectedState, stateChanged()

void error (

This signal is emitted when an error occurs.

See also:


void stateChanged (

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

void aboutToClose ()Inherited

void bytesWritten (
  • qint64bytes)

void readChannelFinished ()Inherited

void readyRead ()Inherited

Last modified: 2014-09-30

Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus