世界快看:Qt 蓝牙开发
2023-05-25 11:19:49
来源:QT教程
1.要使用qt蓝牙模块, 项目的 .pro要声明使用
2.有了lambda函数后,不用再一个个声明和创建具名槽函数的感觉挺好,就是代码成一坨了
3.虽然说是低功耗蓝牙,但还是要记得用完后断开和外设店连接,之前忘记断开,一晚上一个纽扣电池的点就被耗光了
【资料图】
4.GATT Profile 简介
搜寻附近全部的蓝牙设备
根据搜寻出的蓝牙设备信息,筛选出要连接的蓝牙设备进行连接
建立连接后,去获取该蓝牙设备等services列表,根据约定好的服务uuid筛选出自己需要的服务
发现对应的服务后,根据约定好的服务下characteristic特性id,创建特征对象,并监听特征对象内容的变化
向客户端特征配置对象写入特征生效消息,开始接收外围蓝牙设备。
bluedevice.h:
#ifndef BLUEDEVICE_H#define BLUEDEVICE_H#include #include#include#include#include#include#include#includeclass BlueDevice: public QObject{Q_OBJECTpublic:BlueDevice();QString getAddress(QBluetoothDeviceInfo device) const;private:QListdevice_list; //存放搜索到到蓝牙设备列表QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent; //设备搜索对象QLowEnergyController *m_controler; //单个蓝牙设备控制器QLowEnergyService *m_service; //服务对象实例};#endif // BLUEDEVICE_H
bluedevice.cpp:
#include \"bluedevice.h\"#include #includeBlueDevice::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::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 configurationQLowEnergyDescriptor 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::\" <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();#elsereturn device.address().toString();#endif}//void BlueDevice::getNewService(QBluetoothServiceInfo info) {// qDebug() << \"ppppp uuuuuuuuuu\";//}
【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】
点击这里: