当前位置:网站首页>Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
Qt蓝牙:搜索蓝牙设备的类——QBluetoothDeviceDiscoveryAgent
2022-07-05 04:26:00 【友善啊,朋友】
一、描述
此类用于查找附近的蓝牙设备。
查找附近的蓝牙设备过程:
- 创建 QBluetoothDeviceDiscoveryAgent 的实例,
- 连接到 deviceDiscovered() 或 finished() 信号,
- 并调用 start()。
#include "widget.h"
#include "ui_widget.h"
#include <QBluetoothDeviceDiscoveryAgent>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,this, &Widget::deviceDiscovered);
}
Widget::~Widget()
{
delete ui;
}
void Widget::deviceDiscovered(const QBluetoothDeviceInfo &device)
{
qDebug() << "发现新设备:" << device.name() << '(' << device.address().toString() << ')';
}
void Widget::on_pushButton_clicked()
{
qDebug() << "开始搜索";
discoveryAgent->start();
}
二、类型成员
1、enum QBluetoothDeviceDiscoveryAgent::DiscoveryMethod:此枚举描述了此类使用的搜索蓝牙设备的方法。不一定有用,具体看各操作系统。
- NoMethod:无法搜索。不支持任何可用的方法。
- ClassicMethod:搜索蓝牙经典(BaseRate)设备。
- LowEnergyMethod:搜索低功耗蓝牙设备。
2、enum QBluetoothDeviceDiscoveryAgent::Error:在搜索蓝牙设备期间可能出现的错误情况。
- NoError:没有发生错误。
- PoweredOffError:蓝牙适配器已关闭,在进行搜索之前应打开它。
- InputOutputError:从设备写入或读取导致错误。
- InvalidBluetoothAdapterError:传递的本地适配器地址与任何本地蓝牙设备的物理适配器地址不匹配。
- UnsupportedPlatformError:设备搜索在当前平台上是不支持的。该错误是响应对 start() 的调用而设置的。
- UnsupportedDiscoveryMethod:当前平台不支持设置的搜索方法 DiscoveryMethod。
- LocationServiceTurnedOffError:定位服务已关闭。关闭位置服务时,无法使用蓝牙 API。
- UnknownError:发生未知错误。
三、成员函数
1、QBluetoothDeviceDiscoveryAgent(const QBluetoothAddress &deviceAdapter, QObject *parent = nullptr)
构造一个具有父级的新蓝牙设备搜索代理。
它使用 deviceAdapter 进行设备搜索。如果 deviceAdapter 是默认构造的,则生成的 QBluetoothDeviceDiscoveryAgent 对象将使用本地默认蓝牙适配器。
如果指定的 deviceAdapter 不是本地适配器,则 error() 将设置为 InvalidBluetoothAdapterError。 因此建议在使用此构造函数后立即测试错误标志。
2、【信号】void canceled()
当通过调用 stop() 中止设备搜索时会发出此信号。
3、【信号】void deviceDiscovered(const QBluetoothDeviceInfo &info)
当发现 info 描述的蓝牙设备时发出此信号。
一旦收集到的设备信息,就会发出信号。但是,只要还没有发出 finished() 信号,即使对于已经发现的设备,信息收集也会继续。
4、【信号】void deviceUpdated(const QBluetoothDeviceInfo &info, QBluetoothDeviceInfo::Fields updatedFields)
当收到有关由 info 描述的蓝牙设备的附加信息时,会发出此信号。updatedFields 标志告诉哪些信息已被更新。
在搜索过程中,一些信息可以动态变化,例如信号强度和制造商数据。此信号通知用户,如果应用程序正在显示此数据,则可以对其进行更新,而不是等到发现完成。
5、【信号】void errorOccurred(QBluetoothDeviceDiscoveryAgent::Error error)
在蓝牙设备搜索过程中发生错误时发出此信号。error 参数描述了发生的错误。
6、【信号】void finished()
当蓝牙设备搜索完成时发出此信号。如果设备搜索以错误结束,则不会发出信号。
7、void start(QBluetoothDeviceDiscoveryAgent::DiscoveryMethods methods)
启动蓝牙设备搜索(如果尚未启动)。搜索方法限制了设备搜索的范围。
注意:methods 仅确定发现的类型,并不意味着过滤结果。例如,尽管方法仅设置为 LowEnergyMethod,但搜索可能仍包含经典蓝牙设备。这可能是由于先前缓存的搜索结果可能被合并到搜索结果中而发生的。
void start()
启动蓝牙设备发现(如果尚未启动)。
8、void stop()
停止蓝牙设备搜索。一旦设备搜索被取消,就会发出 cancel() 信号。
9、QList<QBluetoothDeviceInfo> discoveredDevices()
返回所有发现的蓝牙设备的列表。
10、QBluetoothDeviceDiscoveryAgent::Error error()
返回最后一个错误。
11、QString errorString()
返回上一个错误的可读描述。
12、bool isActive()
是否正在搜索蓝牙设备。
13、int lowEnergyDiscoveryTimeout()
返回应用于低功耗蓝牙设备搜索的超时(毫秒)。
值为 -1 表示平台不支持此属性,并且无法调整设备搜索的超时时间。
值为 0 表示必须通过 stop() 手动停止的永无止境的搜索。
14、void setLowEnergyDiscoveryTimeout(int timeout)
设置搜索低功耗蓝牙设备的最大搜索时间(以毫秒为单位)。如果超时为 0,则搜索将一直运行,直到调用 stop()。
重新启动设备搜索后,新的超时值才会生效。此外超时不影响经典蓝牙设备搜索。
对于可靠的低功耗蓝牙搜索,至少使用 40000 毫秒。
15、【static】QBluetoothDeviceDiscoveryAgent::DiscoveryMethods supportedDiscoveryMethods()
返回当前平台支持的搜索方法。
边栏推荐
- 【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
- Realize the attention function of the article in the applet
- level17
- web资源部署后navigator获取不到mediaDevices实例的解决方案(navigator.mediaDevices为undefined)
- Common features of ES6
- [untitled]
- Serpentine matrix
- Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
- OWASP top 10 vulnerability Guide (2021)
- Basic analysis of IIC SPI protocol
猜你喜欢
【thingsboard】替换首页logo的方法
WeNet:面向工业落地的E2E语音识别工具
Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
函數(易錯)
Ctfshow web entry code audit
Threejs factory model 3DMAX model obj+mtl format, source file download
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
Common features of ES6
level17
Rome chain analysis
随机推荐
[Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
函数(基本:参数,返回值)
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
[thingsboard] how to replace the homepage logo
指针函数(基础)
Hypothesis testing -- learning notes of Chapter 8 of probability theory and mathematical statistics
PHP reads the INI file and writes the modified content
Scheduling system of kubernetes cluster
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
Power management bus (pmbus)
What are the building energy-saving software
首席信息官如何利用业务分析构建业务价值?
Study notes 7
Learning notes 8
【虚幻引擎UE】运行和启动的区别,常见问题分析
【UNIAPP】系统热更新实现思路
Threejs realizes rain, snow, overcast, sunny, flame
[phantom engine UE] package error appears! Solutions to findpin errors
蛇形矩阵
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates