QBluetoothSocket 類

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

頭: #include <QBluetoothSocket>
qmake: QT += bluetooth
Since: Qt 5.2
繼承: QIODevice

該類在 Qt 5.2 引入。

公共類型

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

公共函數

QBluetoothSocket (QObject * parent = nullptr)
QBluetoothSocket (QBluetoothServiceInfo::Protocol socketType , QObject * parent = nullptr)
virtual ~QBluetoothSocket ()
void abort ()
void connectToService (const QBluetoothServiceInfo & service , QIODevice::OpenMode openMode = ReadWrite)
void connectToService (const QBluetoothAddress & address , const QBluetoothUuid & uuid , QIODevice::OpenMode openMode = ReadWrite)
void connectToService (const QBluetoothAddress & address , quint16 port , QIODevice::OpenMode openMode = ReadWrite)
void disconnectFromService ()
QBluetoothSocket::SocketError error () const
QString errorString () const
QBluetoothAddress localAddress () const
QString localName () const
quint16 localPort () const
QBluetoothAddress peerAddress () const
QString peerName () const
quint16 peerPort () const
QBluetooth::SecurityFlags preferredSecurityFlags () const
void setPreferredSecurityFlags (QBluetooth::SecurityFlags flags )
bool setSocketDescriptor (int socketDescriptor , QBluetoothServiceInfo::Protocol socketType , QBluetoothSocket::SocketState socketState = ConnectedState, QIODevice::OpenMode openMode = ReadWrite)
int socketDescriptor () const
QBluetoothServiceInfo::Protocol socketType () const
QBluetoothSocket::SocketState state () const

重實現公共函數

virtual qint64 bytesAvailable () const override
virtual qint64 bytesToWrite () const override
virtual bool canReadLine () const override
virtual void close () override
virtual bool isSequential () const override

信號

void connected ()
void disconnected ()
void error (QBluetoothSocket::SocketError error )
void stateChanged (QBluetoothSocket::SocketState state )

保護函數

void doDeviceDiscovery (const QBluetoothServiceInfo & service , QIODevice::OpenMode openMode )
void setSocketError (QBluetoothSocket::SocketError error_ )
void setSocketState (QBluetoothSocket::SocketState state )

重實現保護函數

virtual qint64 readData (char * data , qint64 maxSize ) override
virtual qint64 writeData (const char * data , qint64 maxSize ) override

詳細描述

QBluetoothSocket 支持 2 種套接字類型 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.

Protocol 在平颱中不被支持,調用 connectToService () 將發射 UnsupportedProtocolError 錯誤。

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

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

注意: 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.

成員類型文檔編製

enum QBluetoothSocket:: SocketError

此枚舉描述 Bluetooth 套接字錯誤類型。

常量 描述
QBluetoothSocket::UnknownSocketError QAbstractSocket::UnknownSocketError 齣現未知錯誤。
QBluetoothSocket::NoSocketError -2 沒有錯誤。用於測試。
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 試圖操作當套接字處於不準許狀態時。
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.

常量 描述
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.

成員函數文檔編製

QBluetoothSocket:: QBluetoothSocket ( QObject * parent = nullptr)

Constructs a Bluetooth socket with parent .

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

Constructs a Bluetooth socket of socketType type, with parent .

[signal] void QBluetoothSocket:: connected ()

This signal is emitted when a connection is established.

另請參閱 QBluetoothSocket::ConnectedState and stateChanged ().

[signal] void QBluetoothSocket:: disconnected ()

This signal is emitted when the socket is disconnected.

另請參閱 QBluetoothSocket::UnconnectedState and stateChanged ().

[signal] void QBluetoothSocket:: error ( QBluetoothSocket::SocketError error )

此信號發射,當 error 齣現。

注意: 信號 error 在此類中被重載。通過使用函數指針句法連接到此信號,Qt 提供用於獲得如此範例展示的函數指針的方便幫助程序:

connect(bluetoothSocket, QOverload<QBluetoothSocket::SocketError>::of(&QBluetoothSocket::error),
    [=](QBluetoothSocket::SocketError error){ /* ... */ });
					

另請參閱 error ().

[signal] void QBluetoothSocket:: stateChanged ( QBluetoothSocket::SocketState state )

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

另請參閱 connected (), disconnected (), state (),和 QBluetoothSocket::SocketState .

[虛擬] QBluetoothSocket:: ~QBluetoothSocket ()

Destroys the Bluetooth socket.

void QBluetoothSocket:: abort ()

中止當前連接並重置套接字。不像 disconnectFromService (),此函數立即關閉套接字,丟棄寫緩衝中任何待決數據。

注意: On Android, aborting the socket requires asynchronous interaction with Android threads. Therefore the associated disconnected () 和 stateChanged () signals are delayed until the threads have finished the closure.

另請參閱 disconnectFromService () 和 close ().

[override virtual] qint64 QBluetoothSocket:: bytesAvailable () const

重實現: QIODevice::bytesAvailable () const.

返迴等待讀取的傳入字節數。

另請參閱 bytesToWrite () 和 read ().

[override virtual] qint64 QBluetoothSocket:: bytesToWrite () const

重實現: QIODevice::bytesToWrite () const.

Returns the number of bytes that are waiting to be written. The bytes are written when control goes back to the event loop.

[override virtual] bool QBluetoothSocket:: canReadLine () const

重實現: QIODevice::canReadLine () const.

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

[override virtual] void QBluetoothSocket:: close ()

重實現: QIODevice::close ().

Disconnects the socket's connection with the device.

注意: On Android, closing the socket requires asynchronous interaction with Android threads. Therefore the associated disconnected () 和 stateChanged () signals are delayed until the threads have finished the closure.

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

Attempts to connect to the service described by service .

套接字被打開采用給定 openMode socketType () is ignored if service specifies a differing QBluetoothServiceInfo::socketProtocol ().

The socket first enters ConnectingState and attempts to connect to the device providing service 。若建立連接, QBluetoothSocket 進入 ConnectedState 並發射 connected ().

在任何時候,套接字可以發射 error () 給 (發齣錯誤的) 信號。

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.

另請參閱 state () 和 disconnectFromService ().

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

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

套接字被打開采用給定 openMode .

For BlueZ , 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 。若建立連接, QBluetoothSocket 進入 ConnectedState 並發射 connected ().

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

在任何時候,套接字可以發射 error () 給 (發齣錯誤的) 信號。

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

另請參閱 state () 和 disconnectFromService ().

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

Attempts to make a connection with address 在給定 port .

套接字被打開采用給定 openMode .

The socket first enters ConnectingState , and attempts to connect to address 。若建立連接, QBluetoothSocket 進入 ConnectedState 並發射 connected ().

在任何時候,套接字可以發射 error () 給 (發齣錯誤的) 信號。

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

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

另請參閱 state () 和 disconnectFromService ().

void QBluetoothSocket:: disconnectFromService ()

Attempts to close the socket. If there is pending data waiting to be written QBluetoothSocket 將進入 ClosingState 並等待直到所有數據被寫入。最終,它將進入 UnconnectedState 和發射 disconnected () 信號。

另請參閱 connectToService ().

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

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.

QBluetoothSocket::SocketError QBluetoothSocket:: error () const

Returns the last error.

QString QBluetoothSocket:: errorString () const

Returns a user displayable text string for the error.

[override virtual] bool QBluetoothSocket:: isSequential () const

重實現: QIODevice::isSequential () const.

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.

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.

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.

QBluetoothAddress QBluetoothSocket:: peerAddress () const

Returns the address of the peer device.

QString QBluetoothSocket:: peerName () const

Returns the name of the peer device.

quint16 QBluetoothSocket:: peerPort () const

Return the port number of the peer socket if available, otherwise returns 0. On Android, this feature is not supported.

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 QBluetooth::Secure .

該函數在 Qt 5.6 引入。

另請參閱 setPreferredSecurityFlags ().

[override virtual protected] qint64 QBluetoothSocket:: readData ( char * data , qint64 maxSize )

重實現: QIODevice::readData (char *data, qint64 maxSize).

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

Sets the preferred security parameter for the connection attempt to flags . This value is incorporated when calling connectToService (). Therefore it is required to reconnect to change this parameter for an existing connection.

On Bluez this property is set to QBluetooth::Authorization 在默認情況下。

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 QBluetooth::Secure .

Android only supports two levels of security (secure and non-secure). If this flag is set to QBluetooth::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 QBluetooth::Secure 在默認情況下。

注意: 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.

該函數在 Qt 5.6 引入。

另請參閱 preferredSecurityFlags ().

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

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

The set socket descriptor is considered owned by the QBluetoothSocket and may be e.g. closed once finished.

返迴 true 當成功時

另請參閱 socketDescriptor ().

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

將最後齣現的錯誤類型設為 error_ .

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

Sets the socket state to state .

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.

另請參閱 setSocketDescriptor ().

QBluetoothServiceInfo::Protocol QBluetoothSocket:: socketType () const

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

Android only support RFCOMM based sockets.

QBluetoothSocket::SocketState QBluetoothSocket:: state () const

返迴套接字的當前狀態。

[override virtual protected] qint64 QBluetoothSocket:: writeData (const char * data , qint64 maxSize )

重實現: QIODevice::writeData (const char *data, qint64 maxSize).