QBluetoothLocalDevice Class

QBluetoothLocalDevice 类可访问本地蓝牙设备。更多

Header: #include <QBluetoothLocalDevice>
qmake: QT += bluetooth
Inherits: QObject

公共类型

enum Error { NoError, PairingError, MissingPermissionsError, UnknownError }
enum HostMode { HostPoweredOff, HostConnectable, HostDiscoverable, HostDiscoverableLimitedInquiry }
enum Pairing { Unpaired, Paired, AuthorizedPaired }

公共函数

QBluetoothLocalDevice(QObject *parent = nullptr)
QBluetoothLocalDevice(const QBluetoothAddress &address, QObject *parent = 0)
virtual ~QBluetoothLocalDevice()
QBluetoothAddress address() const
QList<QBluetoothAddress> connectedDevices() const
QBluetoothLocalDevice::HostMode hostMode() const
bool isValid() const
QString name() const
QBluetoothLocalDevice::Pairing pairingStatus(const QBluetoothAddress &address) const
void powerOn()
void requestPairing(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)
void setHostMode(QBluetoothLocalDevice::HostMode mode)

信号

void deviceConnected(const QBluetoothAddress &address)
void deviceDisconnected(const QBluetoothAddress &address)
(since 6.2) void errorOccurred(QBluetoothLocalDevice::Error error)
void hostModeStateChanged(QBluetoothLocalDevice::HostMode state)
void pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)

静态公共成员

QList<QBluetoothHostInfo> allDevices()

详细说明

QBluetoothLocalDevice 提供用于获取和设置本地蓝牙设备状态的函数。

在 iOS 平台上,该类不能使用,因为该平台不公开任何可提供本地蓝牙设备信息的数据或 API。

成员类型文档

enum QBluetoothLocalDevice::Error

该枚举描述了可能返回的错误

常量说明
QBluetoothLocalDevice::NoError0无已知错误
QBluetoothLocalDevice::PairingError1配对错误
QBluetoothLocalDevice::MissingPermissionsError (since Qt 6.4)2操作系统请求用户未授予的权限。
QBluetoothLocalDevice::UnknownError100未知错误

enum QBluetoothLocalDevice::HostMode

该枚举描述了本地蓝牙设备的最大值。

常数说明
QBluetoothLocalDevice::HostPoweredOff0关闭设备电源
QBluetoothLocalDevice::HostConnectable1如果远程蓝牙设备之前已与本地蓝牙设备配对或知道其地址,则可以连接到本地蓝牙设备。如果设备已关闭电源,则将其重新启动。
QBluetoothLocalDevice::HostDiscoverable2远程蓝牙设备可以发现本地蓝牙设备的存在。设备也可以连接,并开启电源。在安卓系统上,该模式最多只能激活 5 分钟。
QBluetoothLocalDevice::HostDiscoverableLimitedInquiry3在进行有限查询时,远程蓝牙设备可以发现本地蓝牙设备的存在。这应该用于定位只能在有限时间内发现的服务。这可以加快游戏设备之间的发现速度,因为不在 "有限查询 "模式下的设备可以跳过服务发现。在此模式下,如果需要,设备可以连接并打开电源。安卓系统不支持此模式。

注: 在 macOS 上,无法设置hostMode()。报告的主机模式仅限于 HostPoweredOff 和 HostConnectable。

注意: 在 Windows 上,无法将hostMode() 设置为 HostDiscoverable 或 HostDiscoverableLimitedInquiry。使用这些模式等同于 HostConnectable。

注: 从 Android 13(API 级 33)开始,HostPoweredOff 状态依赖于非公开的 Android API,因为公开 API 已被弃用,请参见(disable())。这可能会在未来的 Android 版本中发生变化。

注意: 至少在 Android 12 上,设备的蓝牙可见性设置可能会决定 HostDiscoverable 或 HostConnectable 设置的结果。例如,如果可见性设置为关闭,则可能无法进入 HostDiscoverable 模式,而会使用 HostConnectable 模式。这在未来的 Android 版本中可能会有所改变。

enum QBluetoothLocalDevice::Pairing

该枚举描述两个蓝牙设备之间的配对状态。

常数说明
QBluetoothLocalDevice::Unpaired0蓝牙设备未配对。
QBluetoothLocalDevice::Paired1蓝牙设备已配对。当远程设备启动与本地设备的连接时,系统将提示用户进行授权。
QBluetoothLocalDevice::AuthorizedPaired2蓝牙设备已配对。当远程设备启动与本地设备的连接时,系统不会提示用户进行授权。

成员功能文档

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

构建一个 QBluetoothLocalDevice(蓝牙本地设备),parent

注: 从 Android 12(API 第 31 级)开始,构建该类需要蓝牙运行时权限(BLUETOOTH_SCANBLUETOOTH_ CONNECT)。如果未授予权限,设备将无效。

另请参阅 isValid().

[explicit] QBluetoothLocalDevice::QBluetoothLocalDevice(const QBluetoothAddress &address, QObject *parent = 0)

address 构建新的 QBluetoothLocalDevice。如果address 是默认构造,生成的本地设备将选择本地默认设备。

注: 从 Android 12(API 级别 31)开始,构建该类需要蓝牙运行时权限(BLUETOOTH_SCANBLUETOOTH_ CONNECT)。如果未授予权限,设备将无效。

另请参阅 isValid().

[virtual noexcept] QBluetoothLocalDevice::~QBluetoothLocalDevice()

摧毁QBluetoothLocalDevice.

QBluetoothAddress QBluetoothLocalDevice::address() const

返回蓝牙设备的 MAC 地址。

注: 在 Android 系统中,从 Android 6.0 开始,该函数始终返回常量02:00:00:00:00:00 作为本地地址。删除了对设备本地 MAC 地址的编程访问。

[static] QList<QBluetoothHostInfo> QBluetoothLocalDevice::allDevices()

返回所有可用本地蓝牙设备的列表。在 macOS 上,只有 "默认 "本地设备。

QList<QBluetoothAddress> QBluetoothLocalDevice::connectedDevices() const

返回已连接设备的列表。该列表不同于当前已配对设备的列表。

在 Android 和 macOS 上,无法检索已连接设备的列表。只能监听(取消)连接的变化。为方便起见,该类会监控自启动以来的所有连接和断开事件,并在调用此函数时返回当前列表。因此,在创建实例后不久,该函数可能会返回一个空列表。

另请参阅 deviceConnected() 和deviceDisconnected()。

[signal] void QBluetoothLocalDevice::deviceConnected(const QBluetoothAddress &address)

当本地设备通过address 与远程设备建立连接时,就会发出该信号。

另请参阅 deviceDisconnected() 和connectedDevices()。

[signal] void QBluetoothLocalDevice::deviceDisconnected(const QBluetoothAddress &address)

当本地设备与远程蓝牙设备断开连接时会发出该信号,address

另请参阅 deviceConnected() 和connectedDevices()。

[signal, since 6.2] void QBluetoothLocalDevice::errorOccurred(QBluetoothLocalDevice::Error error)

配对过程中出现异常error 时发出的信号。

该函数在 Qt 6.2 中引入。

QBluetoothLocalDevice::HostMode QBluetoothLocalDevice::hostMode() const

返回本地蓝牙设备的当前主机模式。在 macOS 上,它是HostPoweredOffHostConnectable

另请参阅 setHostMode()。

[signal] void QBluetoothLocalDevice::hostModeStateChanged(QBluetoothLocalDevice::HostMode state)

state 的主机已过渡到不同的HostMode

bool QBluetoothLocalDevice::isValid() const

如果QBluetoothLocalDevice 代表可用的本地蓝牙设备,则返回true ;否则返回 false。

如果该类实例所代表的本地蓝牙适配器从系统中移除(如移除底层蓝牙加密狗),则该实例将变为无效。即使同一蓝牙适配器返回到系统中,已经失效的QBluetoothLocalDevice 实例仍将保持失效。

注: 从 Android 12(API 第 31 级)开始,构建该类需要蓝牙运行时权限(BLUETOOTH_SCANBLUETOOTH_ CONNECT)。如果未授予权限,设备将无效。

另请参阅 allDevices().

QString QBluetoothLocalDevice::name() const

返回用户为该蓝牙设备指定的名称。

[signal] void QBluetoothLocalDevice::pairingFinished(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)

配对或解除配对已通过address 完成。当前配对状态见pairing 。如果配对请求未成功,则不会发出该信号。如果配对请求失败,则会发出errorOccurred() 信号。只有在配对请求之前调用当前对象实例的requestPairing() 时,才会发出该信号。

QBluetoothLocalDevice::Pairing QBluetoothLocalDevice::pairingStatus(const QBluetoothAddress &address) const

返回address 的当前蓝牙配对状态,是未配对、已配对还是已配对并授权。

void QBluetoothLocalDevice::powerOn()

如果设备已关闭电源,则在返回hostMode() 状态后开启设备。

注: 由于支持平台的安全策略不同,此方法在不同平台上可能会有不同的行为。例如,在打开或关闭蓝牙之前,系统可能会要求用户确认。在 macOS 上,无法开启/关闭蓝牙。有关详情,请参阅特定平台的蓝牙文档。

void QBluetoothLocalDevice::requestPairing(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing)

使用address 设置pairing 状态。结果通过信号pairingFinished() 返回。

在 Android 和 macOS 上,AuthorizedPaired 是不可能的,其行为与 Paired 相同。在 Windows 上,具体的配对模式由操作系统决定。

在 macOS 上,无法取消设备配对。如果请求取消配对,则会立即发出pairingFinished() 信号,但设备仍保持配对状态。可以为之前未配对的设备请求配对。此外,AuthorizedPaired 的行为与Paired 相同。

注意:创建配对可能需要几分钟时间,而且可能需要用户确认。

void QBluetoothLocalDevice::setHostMode(QBluetoothLocalDevice::HostMode mode)

将此本地蓝牙设备的主机模式设置为mode

某些转换(如打开或关闭设备)可能需要一些时间。因此,只有在hostModeStateChanged() 信号结束前一个请求后,才能进行后续调用。如果忽略了这一点,一系列呼叫的结果将无法很好地界定。

注意: 由于所支持平台的安全策略不同,该方法在不同平台上可能会有不同的行为。例如,系统在打开或关闭蓝牙前可能会要求用户确认,而且并非所有的主机模式都支持蓝牙。在 macOS 上,无法通过编程更改hostMode()。用户只能在系统偏好设置中开关蓝牙。在 Windows 上,该方法必须从用户界面线程调用,因为它可能需要用户确认。详情请参考特定平台的蓝牙文档。

另请参阅 hostMode().

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