Qt蓝牙:QBluetoothDeviceInfo、QBluetoothAddress

QBluetoothDeviceInfo

一、描述

QBluetoothDeviceInfo 类存储有关蓝牙设备的信息。

二、类型成员

1、enum QBluetoothDeviceInfo::CoreConfiguration:此枚举描述了设备的配置。

  • UnknownCoreConfiguration:无法确定蓝牙设备的类型。
  • BaseRateCoreConfiguration:标准蓝牙设备。
  • BaseRateAndLowEnergyCoreConfiguration:蓝牙智能设备,支持标准和低功耗设备。
  • LowEnergyCoreConfiguration:低功耗蓝牙设备。

2、enum class QBluetoothDeviceInfo::Field:此枚举与 QBluetoothDeviceDiscoveryAgent::deviceUpdated() 信号结合使用,用于指示已更改的字段。

  • Field::None:没有任何值改变。
  • Field::RSSI:设备的 rssi() 值已更改。
  • Field::ManufacturerDatamanufacturerData() 字段已更改
  • Field::ServiceDataserviceData() 字段已更改
  • Field::All:匹配所有可能的字段。

3、enum QBluetoothDeviceInfo::MajorDeviceClass:此枚举描述了主要蓝牙设备的类型。

  • MiscellaneousDevice:杂项设备。
  • ComputerDevice:计算机或掌上电脑。
  • PhoneDevice:电话设备。
  • NetworkDevice:提供对局域网的访问的设备。
  • AudioVideoDevice:能够播放或捕获音频和/或视频的设备。
  • PeripheralDevice:键盘、鼠标等外围设备。
  • ImagingDevice:成像设备,例如显示器、打印机、扫描仪或照相机。
  • WearableDevice:可穿戴设备,例如手表或寻呼机。
  • ToyDevice:玩具设备。
  • HealthDevice:与健康相关的设备,例如心率或温度监测器。
  • UncategorizedDevice:其他分类的设备。

4、enum QBluetoothDeviceInfo::MinorAudioVideoClass:此枚举描述了音频/视频设备的次要蓝牙设备。

  • UncategorizedAudioVideoDevice:未分类的音频/视频设备。
  • WearableHeadsetDevice:可穿戴耳机设备。
  • HandsFreeDevice:免提设备。
  • Microphone:麦克风。
  • Loudspeaker:一个扬声器。
  • Headphones:耳机。
  • PortableAudioDevice:便携式音频设备。
  • CarAudio:汽车音频设备。
  • SetTopBox:机顶盒。
  • HiFiAudioDevice:HiFi 音频设备。
  • Vcr:录像机。
  • VideoCamera:摄像机。
  • Camcorder:摄像机。
  • VideoMonitor:视频监视器。
  • VideoDisplayAndLoudspeaker:带有内置扬声器的视频显示器。
  • VideoConferencing:视频会议设备。
  • GamingDevice:游戏设备。

5、enum QBluetoothDeviceInfo::MinorComputerClass:此枚举描述了计算机设备的次要蓝牙设备。

  • UncategorizedComputer:未分类的计算机设备。
  • DesktopComputer:台式计算机。
  • ServerComputer:服务器计算机。
  • LaptopComputer:笔记本电脑。
  • HandheldClamShellComputer:翻盖式手持计算机或掌上电脑。
  • HandheldComputer:手持计算机或掌上电脑。
  • WearableComputer:可穿戴计算机。

6、enum QBluetoothDeviceInfo::MinorHealthClass:此枚举描述了健康设备的次要蓝牙设备。

  • UncategorizedHealthDevice:未分类的健康设备。
  • HealthBloodPressureMonitor:血压监测器。
  • HealthThermometer:温度计。
  • HealthWeightScale:体重秤。
  • HealthGlucoseMeter:血糖仪。
  • HealthPulseOximeter:血氧饱和度计。
  • HealthDataDisplay:健康数据显示器。
  • HealthStepCounter:计步器。

7、enum QBluetoothDeviceInfo::MinorImagingClass:此枚举描述了成像设备的次要蓝牙设备。

  • UncategorizedImagingDevice:未分类的成像设备。
  • ImageDisplay:图像显示设备。
  • ImageCamera:摄像头。
  • ImageScanner:图像扫描仪。
  • ImagePrinter:图像打印机。

8、enum QBluetoothDeviceInfo::MinorMiscellaneousClass:此枚举描述了杂项设备的次要蓝牙设备。

  • UncategorizedMiscellaneous:未分类的杂项设备。

9、enum QBluetoothDeviceInfo::MinorNetworkClass:此枚举描述了局域网访问设备的当前的网络利用率。

  • NetworkFullService:100% 的总带宽可用。
  • NetworkLoadFactorOne:当前正在使用总带宽的 0% - 17%。
  • NetworkLoadFactorTwo:当前正在使用总带宽的 17% - 33%。
  • NetworkLoadFactorThree:当前正在使用总带宽的 33% - 50%。
  • NetworkLoadFactorFour:当前正在使用总带宽的 50% - 67%。
  • NetworkLoadFactorFive:当前正在使用总带宽的 67% - 83%。
  • NetworkLoadFactorSix:当前正在使用总带宽的 83% - 99%。
  • NetworkNoService:没有可用的网络服务。

10、enum QBluetoothDeviceInfo::MinorPeripheralClass:此枚举描述外围设备的次要蓝牙设备。

  • UncategorizedPeripheral:未分类的外围设备。
  • KeyboardPeripheral:键盘。
  • PointingDevicePeripheral:指点设备,例如鼠标。
  • KeyboardWithPointingDevicePeripheral:带有内置指针设备的键盘。
  • JoystickPeripheral:操纵杆。
  • GamepadPeripheral:游戏手柄。
  • RemoteControlPeripheral:遥控器。
  • SensingDevicePeripheral:传感设备。
  • DigitizerTabletPeripheral:数位板外设。
  • CardReaderPeripheral:读卡器外围设备。

11、enum QBluetoothDeviceInfo::MinorPhoneClass:此枚举描述电话设备的次要蓝牙设备。

  • UncategorizedPhone:未分类的电话设备。
  • CellularPhone:手机。
  • CordlessPhone:无绳电话。
  • SmartPhone:智能手机。
  • WiredModemOrVoiceGatewayPhone:有线调制解调器或语音网关。
  • CommonIsdnAccessPhone:提供 ISDN 访问的设备。

12、enum QBluetoothDeviceInfo::MinorToyClass:此枚举描述了玩具设备的次要蓝牙设备。

  • UncategorizedToy:未分类的玩具。
  • ToyRobot:玩具机器人。
  • ToyVehicle:玩具车。
  • ToyDoll:玩具娃娃或人偶。
  • ToyController:控制器。
  • ToyGam:游戏。

13、enum QBluetoothDeviceInfo::MinorWearableClass:此枚举描述了可穿戴设备的次要蓝牙设备。

  • UncategorizedWearableDevice:未分类的可穿戴设备。
  • WearableWristWatch:手表。
  • WearablePager:寻呼机。
  • WearableJacket:夹克。
  • WearableHelmet:头盔。
  • WearableGlasses:眼镜。

14、enum QBluetoothDeviceInfo::ServiceClass:此枚举描述了蓝牙设备的服务类型。

  • NoService:不提供任何服务。
  • PositioningService:提供定位服务。
  • NetworkingService:提供网络服务。
  • RenderingService:提供渲染服务。
  • CapturingService:提供捕获服务。
  • ObjectTransferService:提供对象传输服务。
  • AudioService:提供音频服务。
  • TelephonyService:提供电话服务。
  • InformationService:提供信息服务。
  • AllServices:提供所有类型的服务。

三、成员函数

1、QBluetoothAddress address()

返回设备的地址。

在 iOS 和 macOS 上,此地址无效,应该使用 deviceUuid()。 这两个平台不会为找到的蓝牙设备公开蓝牙地址。

2、QBluetoothUuid deviceUuid() / void setDeviceUuid(const QBluetoothUuid &uuid)

返回没有地址的蓝牙设备的唯一标识符。

一般来说,这个 uuid 在除 macOS 和 iOS 之外的所有平台上都是无效的。

3、bool isCached() / void setCached(bool cached)

当前对象是否从缓存数据创建的。缓存的信息可能不如从活动设备中读取的数据准确。

4、bool isValid()

对象是否有效。

5、QByteArray manufacturerData(quint16 manufacturerId) / 

      bool setManufacturerData(quint16 producerId, const QByteArray &data)

返回与制造商 ID 关联的数据。

制造商数据由蓝牙核心规范的补充定义,由两部分组成:

  • 制造商特定标识符代码
  • 任意数据字节的序列

远程设备可以为每个制造商 ID 提供多个数据条目。此函数仅返回第一个条目。如果需要所有条目,使用 manufacturerData()。

6、QMultiHash<quint16, QByteArray> manufacturerData()

返回所有制造商的完整数据集。

#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() << ')';
    QMultiHash<quint16, QByteArray> data = device.manufacturerData();
    for(auto it = data.cbegin();it != data.cend();++it)
    {
        qDebug() << it.key()<<QString(it.value().toHex());
    }
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "开始搜索";
    discoveryAgent->start();
}

7、QList<quint16> manufacturerIds()

返回所有制造商 ID。

8、QString name() / void setName(const QString &name)

返回分配给设备的名称。

9、qint16 rssi() / void setRssi(qint16 signal)

返回上次扫描设备时的信号强度。

10、QByteArray serviceData(const QBluetoothUuid &serviceId) 

        bool setServiceData(const QBluetoothUuid &serviceId, const QByteArray &data)

返回与 serviceId 关联的服务数据。

服务数据由蓝牙核心规范的补充定义,由两部分组成:

  • 服务 UUID
  • 任意数据字节的序列

远程设备可以为每个 serviceId 提供多个数据条目。此函数仅返回第一个条目。需要所有条使用 serviceData()。

11、QMultiHash<QBluetoothUuid, QByteArray> serviceData()

返回完整的所有服务数据集。

#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() << ')';
    QMultiHash<QBluetoothUuid, QByteArray> data = device.serviceData();
    for(auto it = data.cbegin();it != data.cend();++it)
    {
        qDebug() << it.key()<<QString(it.value().toHex());
    }
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "开始搜索";
    discoveryAgent->start();
}

12、QList<QBluetoothUuid> serviceIds()

返回所有服务数据 ID。

13、QList<QBluetoothUuid> serviceUuids()

       void setServiceUuids(const QList<QBluetoothUuid> &uuids)

返回设备支持的服务 UUID 列表。


QBluetoothAddress

一、描述

此类以独立于平台和协议的方式保存蓝牙地址。

#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() << ')';
    QBluetoothAddress addr = device.address();
    qDebug() <<addr.toString();
    qDebug() <<addr.toUInt64();
}

void Widget::on_pushButton_clicked()
{
    qDebug() << "开始搜索";
    discoveryAgent->start();
}

二、成员函数

1、void clear()

将蓝牙地址设置为 00:00:00:00:00:00。

2、bool isNull()

蓝牙地址是否有效。

3、QString toString()

以 XX:XX:XX:XX:XX:XX 形式的字符串返回蓝牙地址。

4、quint64 toUInt64()

将蓝牙地址作为 quint64 返回。

以下是一个简单的使用Qt蓝牙模块的串口调试助手的代码示例: ```cpp #include <QtBluetooth> #include <QBluetoothDeviceDiscoveryAgent> #include <QBluetoothDeviceInfo> #include <QBluetoothSocket> #include <QDataStream> #include <QIODevice> #include <QMessageBox> #include <QMainWindow> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { ui.setupUi(this); // 初始化蓝牙设备发现代理 discoveryAgent = new QBluetoothDeviceDiscoveryAgent(this); connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &MainWindow::addBluetoothDevice); // 初始化蓝牙连接socket socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol, this); connect(socket, &QBluetoothSocket::readyRead, this, &MainWindow::readData); connect(socket, &QBluetoothSocket::disconnected, this, &MainWindow::connectionClosed); // 隐藏发送按钮 ui.sendButton->hide(); } private slots: void on_searchButton_clicked() { // 开始搜索蓝牙设备 ui.deviceListWidget->clear(); discoveryAgent->start(); } void addBluetoothDevice(const QBluetoothDeviceInfo &device) { // 将搜索到的蓝牙设备添加到列表中 QListWidgetItem *item = new QListWidgetItem(device.name()); item->setData(Qt::UserRole, device.address().toString()); ui.deviceListWidget->addItem(item); } void on_deviceListWidget_itemActivated(QListWidgetItem *item) { // 连接选中的蓝牙设备 QString address = item->data(Qt::UserRole).toString(); QBluetoothAddress bluetoothAddress(address); socket->connectToService(bluetoothAddress, QBluetoothUuid(QBluetoothUuid::SerialPort)); } void readData() { // 读取接收到的数据 QDataStream stream(socket); stream.setByteOrder(QDataStream::LittleEndian); while (socket->bytesAvailable() > 0) { QByteArray data; stream >> data; ui.receivedTextEdit->appendPlainText(data); } } void on_sendButton_clicked() { // 发送数据 QString data = ui.sendTextEdit->toPlainText(); socket->write(data.toUtf8()); ui.sendTextEdit->clear(); } void connectionClosed() { // 连接断开时清理资源 socket->close(); ui.sendButton->hide(); QMessageBox::information(this, "Bluetooth", "The connection was closed."); } void on_disconnectButton_clicked() { // 断开连接 socket->disconnectFromService(); connectionClosed(); } void on_connectButton_clicked() { // 显示发送按钮 ui.sendButton->show(); } private: Ui::MainWindow ui; QBluetoothDeviceDiscoveryAgent *discoveryAgent; QBluetoothSocket *socket; }; ``` 这是一个简单的GUI应用程序,包含一个搜索蓝牙设备的按钮,一个设备列表,一个连接按钮,一个发送文本框和一个接收文本框。当用户点击搜索按钮时,程序将开始搜索可用的蓝牙设备,并将搜索结果显示在设备列表中。当用户双击设备列表中的一个项时,程序将尝试连接到该设备。如果连接成功,程序将显示发送按钮,并允许用户在发送文本框中输入数据并通过蓝牙发送。当接收到数据时,程序将在接收文本框中显示。用户可以在任何时候断开连接。 请注意,此示例仅演示了如何使用Qt蓝牙模块进行蓝牙通信。这并不是一个完整的串口调试助手,您可能需要添加更多功能来满足您的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值