QBluetoothSocket Class

QBluetoothSocket 类可连接到运行蓝牙服务器的蓝牙设备。更多

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

公共类型

enum class SocketError { UnknownSocketError, NoSocketError, HostNotFoundError, ServiceNotFoundError, NetworkError, …, MissingPermissionsError }
enum class 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, QIODeviceBase::OpenMode openMode = ReadWrite)
void connectToService(const QBluetoothAddress &address, const QBluetoothUuid &uuid, QIODeviceBase::OpenMode openMode = ReadWrite)
void connectToService(const QBluetoothAddress &address, quint16 port, QIODeviceBase::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 = SocketState::ConnectedState, QIODeviceBase::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()
(since 6.2) void errorOccurred(QBluetoothSocket::SocketError error)
void stateChanged(QBluetoothSocket::SocketState state)

受保护函数

void doDeviceDiscovery(const QBluetoothServiceInfo &service, QIODeviceBase::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 支持两种套接字类型:L2CAPRFCOMM

L2CAP 是一种面向数据报的低级蓝牙套接字。Android 不支持 的套接字连接。L2CAP

RFCOMM 是一种可靠的、面向流的套接字。RFCOMM 套接字模拟 RS-232 串行端口。

要创建与蓝牙服务的连接,请创建相应类型的套接字,并调用connectToService() 传递蓝牙地址和端口号。连接建立后,QBluetoothSocket 将发出connected() 信号。

如果某个平台不支持Protocol ,则调用connectToService() 会引发UnsupportedProtocolError 错误。

注意: QBluetoothSocket 不支持同步读写操作。waitForReadyRead() 和waitForBytesWritten() 等函数没有实现。应使用readyRead(),read() 和write() 执行 I/O 操作。

在 iOS 上,不能使用该类,因为该平台没有公开允许访问 QBluetoothSocket 相关功能的 API。

注: 在 macOS Monterey (12) 上,当执行模式对话或进入事件跟踪模式(例如长按窗口关闭按钮)时,套接字数据流会暂停。这可能会在未来的 macOS 版本中有所改变。

成员类型文档

enum class QBluetoothSocket::SocketError

该枚举描述了蓝牙套接字错误类型。

常量描述
QBluetoothSocket::SocketError::UnknownSocketError1发生未知错误。
QBluetoothSocket::SocketError::NoSocketError0无错误。用于测试。
QBluetoothSocket::SocketError::HostNotFoundError3无法找到远程主机。
QBluetoothSocket::SocketError::ServiceNotFoundError4无法在远程主机上找到服务 UUID。
QBluetoothSocket::SocketError::NetworkError5从套接字读取或写入的尝试返回错误。
QBluetoothSocket::SocketError::UnsupportedProtocolError6该平台不支持Protocol
QBluetoothSocket::SocketError::OperationError7当套接字处于不允许操作的状态时尝试了操作。
QBluetoothSocket::SocketError::RemoteHostClosedError (since Qt 5.10)2远程主机关闭了连接。
QBluetoothSocket::SocketError::MissingPermissionsError (since Qt 6.4)8操作系统请求用户未授予的权限。

enum class QBluetoothSocket::SocketState

该枚举描述蓝牙套接字的状态。

常数描述
QBluetoothSocket::SocketState::UnconnectedState0套接字未连接。
QBluetoothSocket::SocketState::ServiceLookupState1套接字正在查询连接参数。
QBluetoothSocket::SocketState::ConnectingState2Socket 正在尝试连接设备。
QBluetoothSocket::SocketState::ConnectedState3套接字已连接到设备。
QBluetoothSocket::SocketState::BoundState4套接字已绑定到本地地址和端口。
QBluetoothSocket::SocketState::ClosingState5套接字已连接,并将在所有待处理数据写入套接字后关闭。
QBluetoothSocket::SocketState::ListeningState6Socket 正在监听传入连接。

成员函数文档

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

通过parent 构建蓝牙插座。

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

通过parent 构建socketType 类型的蓝牙插座。

[virtual noexcept] QBluetoothSocket::~QBluetoothSocket()

销毁蓝牙插座。

void QBluetoothSocket::abort()

终止当前连接并重置套接字。与disconnectFromService() 不同,该函数会立即关闭套接字,并丢弃写缓冲区中的任何待处理数据。

注: 在 Android 系统中,中止套接字需要与 Android 线程进行异步交互。因此,相关的disconnected() 和stateChanged() 信号会延迟,直到线程完成关闭。

另请参阅 disconnectFromService() 和close()。

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

重实现:QIODevice::bytesAvailable() 常量。

返回等待读取的传入字节数。

另请参阅 bytesToWrite() 和read()。

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

重实现:QIODevice::bytesToWrite() 常量。

返回等待写入的字节数。当控制返回到事件循环时,字节将被写入。

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

重实现:QIODevice::canReadLine() 常量。

如果能从设备读取至少一行数据,则返回 true

[override virtual] void QBluetoothSocket::close()

重新实现:QIODevice::close().

断开套接字与设备的连接。

注: 在 Android 系统中,关闭套接字需要与 Android 线程进行异步交互。因此,相关的disconnected() 和stateChanged() 信号会延迟,直到线程完成关闭。

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

尝试连接service 所描述的服务。

套接字在给定的openMode 中打开。如果service 指定了不同的QBluetoothServiceInfo::socketProtocol() ,则socketType() 将被忽略。

套接字首先进入ConnectingState ,并尝试连接到提供service 的设备。如果建立了连接,QBluetoothSocket 进入ConnectedState 并发出connected() 。

在任何时候,套接字都可以发出errorOccurred() 信号,表示发生了错误。

请注意,大多数平台都要求在连接远程设备前进行配对。否则连接过程可能会失败。

在 Android 平台上,只能进行 RFCOMM 连接。此函数忽略任何套接字协议指示符,并假定为 RFCOMM。

另请参阅 state() 和disconnectFromService()。

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

尝试在地址为address 的设备上与uuid 所标识的服务建立连接。

套接字在给定的openMode 中打开。

对于 BlueZ,套接字首先进入ServiceLookupState ,然后查询uuid 的连接参数。如果成功检索到服务参数,套接字就会进入ConnectingState ,并尝试连接到address 。如果建立了连接,QBluetoothSocket 就会进入ConnectedState 并发送connected ()。

在安卓平台上,服务连接可以直接使用远程服务的 UUID 建立。因此,该平台不需要ServiceLookupStatesocketType() 始终设置为QBluetoothServiceInfo::RfcommProtocol

在任何时候,套接字都可以发出errorOccurred() 信号,表明发生了错误。

请注意,大多数平台都要求在连接远程设备前进行配对。否则,连接过程可能会失败。

另请参阅 state() 和disconnectFromService()。

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

尝试在给定的port 上与address 建立连接。

套接字在给定的openMode 中打开。

套接字首先进入ConnectingState ,并尝试连接address 。如果建立了连接,QBluetoothSocket 进入ConnectedState 并发出connected() 信号。

在任何时候,套接字都会发出errorOccurred() 信号,表明发生了错误。

在 Android 和 BlueZ(5.46 或以上版本)上,无法使用端口建立与服务的连接。调用此函数将发出ServiceNotFoundError

请注意,大多数平台都要求在连接远程设备前进行配对。否则,连接过程可能会失败。

另请参阅 state() 和disconnectFromService()。

[signal] void QBluetoothSocket::connected()

建立连接时发出该信号。

另请参阅 QBluetoothSocket::SocketState::ConnectedStatestateChanged()。

void QBluetoothSocket::disconnectFromService()

尝试关闭套接字。如果有等待写入的数据,QBluetoothSocket 将进入ClosingState 并等待所有数据写入。最后,它将进入UnconnectedState 并发出disconnected() 信号。

另请参阅 connectToService()。

[signal] void QBluetoothSocket::disconnected()

插座断开连接时会发出该信号。

另请参阅 QBluetoothSocket::SocketState::UnconnectedStatestateChanged()。

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

启动service 的设备发现功能,用openMode 打开套接字。如果套接字是用服务 uuid 设备地址创建的,则使用服务发现功能查找要连接的端口号。

QBluetoothSocket::SocketError QBluetoothSocket::error() const

返回最后一个错误。

[signal, since 6.2] void QBluetoothSocket::errorOccurred(QBluetoothSocket::SocketError error)

error 时发出该信号。

该函数在 Qt 6.2 中引入。

另请参阅 error()。

QString QBluetoothSocket::errorString() const

返回用户可显示的错误文本字符串。

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

重实现:QIODevice::isSequential() const.

QBluetoothAddress QBluetoothSocket::localAddress() const

返回本地设备的地址。

虽然某些平台可能有所不同,但通常必须连接套接字才能保证返回有效地址。特别是在处理支持多个本地蓝牙适配器的平台时更是如此。

QString QBluetoothSocket::localName() const

返回本地设备的名称。

虽然某些平台可能有所不同,但通常必须连接套接字才能保证返回有效的名称。特别是在处理支持多个本地蓝牙适配器的平台时更是如此。

quint16 QBluetoothSocket::localPort() const

如果本地套接字可用,则返回端口号,否则返回 0。虽然某些平台可能有所不同,但通常必须连接套接字才能保证返回有效的端口号。

在 Android 和 macOS 上不支持此功能,因此返回 0。

QBluetoothAddress QBluetoothSocket::peerAddress() const

返回对等设备的地址。

QString QBluetoothSocket::peerName() const

返回对等设备的名称。

quint16 QBluetoothSocket::peerPort() const

如果对等套接字可用,则返回其端口号,否则返回 0。安卓系统不支持此功能。

QBluetooth::SecurityFlags QBluetoothSocket::preferredSecurityFlags() const

返回用于初始连接尝试的安全参数。

在建立连接期间或之后,双方可能会重新协商安全参数。如果发生这种变化,该标志的值不会反映出来。

在 macOS 上,该标志始终设置为QBluetooth::Security::Secure

另请参见 setPreferredSecurityFlags()。

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

重实现:QIODevice::readData(char *data, qint64 maxSize)。

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

将连接尝试的首选安全参数设置为flags 。该值将在调用connectToService() 时加入。因此,需要重新连接才能更改现有连接的这一参数。

在 Bluez 上,该属性默认设置为QBluetooth::Security::Authorization

在 macOS 上,该值将被忽略,因为该平台不允许访问套接字的安全参数。默认情况下,平台更喜欢安全/加密连接,因此该函数总是返回QBluetooth::Security::Secure

Android 只支持两种安全级别(安全和非安全)。如果该标志设置为QBluetooth::Security::NoSecurity ,则套接字对象将不采用任何身份验证或加密。任何其他安全标记组合都将触发安全蓝牙连接。该标志默认设置为QBluetooth::Security::Secure

注意: 安全连接需要两个设备配对。在某些平台上,配对会在建立连接时自动启动。其他平台则要求应用程序在尝试连接前手动触发配对。

另请参阅 preferredSecurityFlags().

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

设置套接字使用socketDescriptor ,类型为socketType ,状态为socketState ,模式为openMode

套接字描述符归QBluetoothSocket 实例所有,完成后可以关闭。

成功时返回true

另请参阅 socketDescriptor() 。

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

将上次发生的错误类型设置为error_

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

将套接字状态设置为state

int QBluetoothSocket::socketDescriptor() const

返回特定平台的套接字描述符(如果可用)。如果描述符不可用或发生错误,该函数将返回-1。

另请参阅 setSocketDescriptor()。

QBluetoothServiceInfo::Protocol QBluetoothSocket::socketType() const

返回套接字类型。套接字会自动调整为远程服务提供的协议。

安卓仅支持基于RFCOMM 的套接字。

QBluetoothSocket::SocketState QBluetoothSocket::state() const

返回套接字的当前状态。

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

当套接字状态变为state 时会发出该信号。

另请参阅 connected(),disconnected(),state() 和QBluetoothSocket::SocketState

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

重实现:QIODevice::writeData(const char *data, qint64 maxSize)。

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.