Qt中低功耗蓝牙模块使用

最近在Qt下使用opencv库,进行一些图像识别和处理相关的工作,主要是通过摄像头采集运动采样视频,然后进行一些跑姿分析和稳定性测试。但仅仅从视频帧来分析,还是有很多缺陷,后来就想着,如果结合公司的智能鞋垫产品,获取跑步实时的脚上各个部位的压力数据可以对视频分析提供很大的数据补充。跟硬件组的小伙伴聊了聊,得知可以通过蓝牙通讯,配对后实时获取鞋垫压力数据。从小伙伴那里搞来一套鞋垫样品,并拿了一份通讯协议列表,就抓紧搞起了。
得益于Qt详尽的文档说明,对照Qt官方Bluetooth Low Enegy文档,再结合一下官网的例子,断断续续搞了一天,算是在我的客户端程序里看到鞋垫传过来的压力数据了。官网的文档流程讲得很清楚,这里自己还是给整个通讯的流程梳理一下。

  1. 搜寻附近全部的蓝牙设备
  2. 根据搜寻出的蓝牙设备信息,筛选出要连接的蓝牙设备进行连接
  3. 建立连接后,去获取该蓝牙设备等services列表,根据约定好的服务uuid筛选出自己需要的服务
  4. 发现对应的服务后,根据约定好的服务下characteristic特性id,创建特征对象,并监听特征对象内容的变化
  5. 向客户端特征配置对象写入特征生效消息,开始接收外围蓝牙设备。

蓝牙模块代码如下:

bluedevice.h

#ifndef BLUEDEVICE_H
#define BLUEDEVICE_H
#include <QObject>
#include<QBluetoothDeviceDiscoveryAgent>
#include<QBluetoothDeviceInfo>
#include<QBluetoothUuid>
#include<QBluetoothServiceInfo>
#include<QLowEnergyController>
#include<QLowEnergyService>
#include<QLowEnergyDescriptor>
class BlueDevice: public QObject{
    Q_OBJECT
public:
    BlueDevice();
    QString getAddress(QBluetoothDeviceInfo device) const;
private:
        QList<QBluetoothDeviceInfo> device_list;  //存放搜索到到蓝牙设备列表
        QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent;  //设备搜索对象
        QLowEnergyController *m_controler;   //单个蓝牙设备控制器
        QLowEnergyService *m_service; //服务对象实例
};
#endif // BLUEDEVICE_H

bluedevice.cpp

#include "bluedevice.h"
#include <QDebug>
#include<QTimer>
BlueDevice::BlueDevice() {
    qDebug() << "enter bludedevice constructor....";
    m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000);
    //每次发现新设备时触发
    connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this ,[this]() {
        qDebug() << "find a new bluebooth device";
    });
    
    //蓝牙设备搜索完成后,筛选出目标设备进行连接,并进行相关信号与槽函数的绑定
    connect(m_deviceDiscoveryAgent,&QBluetoothDeviceDiscoveryAgent::finished, this, [this]() {
        device_list = this->m_deviceDiscoveryAgent->discoveredDevices();
        //遍历显示设备详情
        QList<QBluetoothDeviceInfo>::iterator it;
        for(it=device_list.begin(); it != device_list.end(); it++) {
            
            // 外围蓝牙设备对象
            QBluetoothDeviceInfo tmp_device = *it;  
            QString device_name = tmp_device.name();
            //qDebug() <<"device name:::" << device_name;
            //qDebug() << "device address:::" << this->getAddress(tmp_device);
            
           //打印搜索出来的全部低功耗蓝牙
           if (tmp_device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
                qDebug() << " low Energy device ....";
                qDebug() <<"22222device name:::" << device_name;
              }
            
            //正则匹配目标设备,
            QString pattern_str = "^Eric.*";  //qt中正则匹配任意个字符,需要使用.*而不是*
            QRegExp rx(pattern_str);
            if(!rx.exactMatch(device_name)) {
                continue;
            }
            qDebug() <<"device name:::" << device_name;
            qDebug() << "device address:::" << this->getAddress(tmp_device);
            // 创建蓝牙设备控制器对象 
            m_controler = new QLowEnergyController(tmp_device, this);
            
            // 监听目标设备连接成功消息,连接成功后,搜索目标设备等服务列表
            connect(m_controler, &QLowEnergyController::connected, this, [this](){
                qDebug() << "m_controler connected ......";
                //必须要在连接建立后 执行开始寻找service的函数 
                //之前调试,就是因为没有在设备连接后主动请求获取服务列表信息,后续监听便没有触发
                m_controler->discoverServices();
            });
            
            // 监听发现服务消息,如果服务的uuid 为约定好的要使用服务类型,则进行后续处理
            connect(m_controler,&QLowEnergyController::serviceDiscovered, this, [this](QBluetoothUuid serviceUuid) {
                if(serviceUuid == QBluetoothUuid( quint16(0xffd0))) {  //我们用的服务类型是0xffd0对应的uuid
                    
                    //发现匹配的服务后,使用控制器对象创建服务对象
                    m_service = m_controler->createServiceObject(serviceUuid,this);
                    if(m_service) {
                        // 服务对象创建成功后,坚挺服务状态变化,如果状态变成已发现,则进行后续服务下特征对象获取
                        connect(m_service,&QLowEnergyService::stateChanged, this, [this]() {
                            qDebug() << "service state change" << m_service->state() << ",||||||";
                            //发现服务, 建立characteristic对象实例
                            if(m_service->state() == QLowEnergyService::ServiceDiscovered) {
                                QLowEnergyCharacteristic hrChar = m_service->characteristic(QBluetoothUuid(quint16(0xfff6)));
                                if(!hrChar.isValid()) {
                                    qDebug() << "characteristic fff6 error:::";
                                }
                                // 设置特征对象可用
                                //enable the chracteristic notification by write 0x01 to client characteristic configuration
                                QLowEnergyDescriptor m_notificationDesc = hrChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
                                if (m_notificationDesc.isValid()) {
                                    if(hrChar.properties() & QLowEnergyCharacteristic::Notify) {
                                        qDebug() << "";
                                    }
                                    m_service->writeDescriptor(m_notificationDesc, QByteArray::fromHex("0100"));
                                }
                            }
                        });
                        // 通过监听特征对象的变化,不断获得鞋垫压力数据。 
                        connect(m_service,&QLowEnergyService::characteristicChanged, this,
                                [this](QLowEnergyCharacteristic c,QByteArray value) {
                                    qDebug() << "characteristicChanged state change::" <<c.uuid()<< ",||||||";
                                    qDebug() << "value length::" << value.length();
                                    qDebug() << "value length::" << value;
                                    QByteArray sub_1 = value.mid(0,2);
                                    QByteArray sub_2 = value.mid(2,2);
                                    QByteArray sub_3 = value.mid(4,2);
                                    bool ok;
                                    // num 1,2,3  分别对应三个压力感应点的压力值
                                    int num_1 =  QString(sub_1.toHex()).toInt(&ok,16);
                                    qDebug() << "num_1:::" << num_1;
                                    int num_2 =  QString(sub_2.toHex()).toInt(&ok,16);
                                    qDebug() << "num_1:::" << num_2;
                                    int num_3 =  QString(sub_3.toHex()).toInt(&ok,16);
                                    qDebug() << "num_1:::" << num_3;
                                }
                        );
                        
                        // 触发服务详情发现函数 ,不要忘记调用
                        m_service->discoverDetails();
                    }
                }
            });
            connect(m_controler,&QLowEnergyController::discoveryFinished, this, [this]() {
                qDebug() << "finish service discovery";
            });
            //连接外围设备
            m_controler->connectToDevice();
            //QTimer *timer = new QTimer(this);
            //connect(timer, &QTimer::timeout, this, [this](){ qDebug() <<"state:::" <<  this->m_controler->state();});
            //timer->start(1000);
        }
    });
   
    // 开始外围设备搜索
    m_deviceDiscoveryAgent->start();
}
// mac和其他系统上address获取有少许差异,参见官方文档
QString BlueDevice::getAddress(QBluetoothDeviceInfo device) const
{
#ifdef Q_OS_MAC
    // On OS X and iOS we do not have addresses,
    // only unique UUIDs generated by Core Bluetooth.
    return device.deviceUuid().toString();
#else
    return device.address().toString();
#endif
}
//void BlueDevice::getNewService(QBluetoothServiceInfo info) {
//    qDebug() << "ppppp uuuuuuuuuu";
//}

 

1.要使用qt蓝牙模块, 项目的 .pro要声明使用
2.有了lambda函数后,不用再一个个声明和创建具名槽函数的感觉挺好,就是代码成一坨了
3.虽然说是低功耗蓝牙,但还是要记得用完后断开和外设店连接,之前忘记断开,一晚上一个纽扣电池的点就被耗光了

  • 6
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
### 回答1: Qt低功耗蓝牙HID是指在Qt开发框架上实现的低功耗蓝牙Human Interface Device(HID)技术。 低功耗蓝牙是一种在无线通信使用低功耗蓝牙技术,它专注于提高设备的功耗效率,延长电池寿命,并支持在低功耗情况下进行可靠的通信。 HID是一种标准的通信协议,用于连接和通信人机接口设备,例如键盘、鼠标、游戏手柄等。它允许设备通过无线连接与计算机或其他设备进行通信。 在Qt框架,可以使用Qt Bluetooth模块来实现低功耗蓝牙HID功能。该模块提供了一组API,用于实现与低功耗蓝牙设备的通信。开发人员可以利用这些API创建和管理低功耗蓝牙连接,发送和接收数据,以及处理与HID设备的交互。 使用Qt低功耗蓝牙HID可以实现许多应用,例如通过蓝牙连接智能手机和电脑,使手机可以充当电脑的键盘或鼠标;或者设计专门的自定义HID设备与计算机进行通信等。 总之,Qt低功耗蓝牙HID提供了一种方便的方式来实现与低功耗蓝牙HID设备的通信,扩展了Qt开发框架在无线通信领域的应用范围。开发人员可以使用这些功能来创建各种具有低功耗需求的无线设备应用程序。 ### 回答2: Qt低功耗蓝牙HID是指使用Qt开发平台创建的支持低功耗蓝牙HID(人机接口设备)的应用程序。低功耗蓝牙HID是一种无线通信技术,用于连接各种外部设备,例如键盘、鼠标和游戏控制器等,与计算机或其他设备进行交互。 Qt作为一个跨平台的开发框架,提供了一系列的API和工具,以便开发人员可以方便地使用低功耗蓝牙HID功能。通过Qt,开发人员可以轻松地创建适用于不同操作系统和设备的应用程序,无需重复编写代码。 使用Qt开发低功耗蓝牙HID应用程序,可以实现以下功能: 1. 设备搜索与连接:通过Qt提供的API,应用程序能够扫描附近的低功耗蓝牙设备,并与目标设备建立连接。 2. 数据传输:应用程序可以通过低功耗蓝牙HID传输数据,例如从键盘或鼠标接收输入数据,并将其发送到计算机或其他设备。 3. 设备管理:使用Qt,应用程序可以管理已连接的低功耗蓝牙设备,例如连接、断开连接和配置设备参数等。 Qt低功耗蓝牙HID的开发流程相对简单,开发人员只需通过Qt提供的API进行设备搜索、连接和数据传输等操作即可。同时,Qt提供了丰富的示例代码和文档,以帮助开发人员更快地上手。 总之,Qt低功耗蓝牙HID为开发人员提供了一个方便、快捷的工具,用于创建支持低功耗蓝牙HID的应用程序,使用户可以更好地与外部设备进行交互。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

往事只能回味味道

如果对你有帮助,一块钱鼓励一下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值