【QT上位机/嵌入式项目】基于IMX6ull--Bluez蓝牙健康助手上位机

演示链接

【QT上位机/嵌入式项目】基于IMX6ull--Bluez蓝牙健康助手上位机

代码实现

代码非完整代码,有需要源码可私信我!

#include "bluetooth_page.h"
#include "remoteselector.h"
#include "chatserver.h"
#include "chatclient.h"
#include <qbluetoothuuid.h>
#include <qbluetoothserver.h>
#include <qbluetoothservicediscoveryagent.h>
#include <qbluetoothdeviceinfo.h>
#include <qbluetoothlocaldevice.h>
#include <QGuiApplication>
#include <QScreen>
#include <QRect>
#include <QTimer>
#include <QDebug>
#include <QTabBar>
#include <QHeaderView>
#include <QTableView>ChatServer *server;static const QLatin1String
serviceUuid("e8e10f95-1a70-4b27-9ccf-02010264e9c8");Bluetooth_Page::Bluetooth_Page(QWidget *parent, Home_Page *homePageInstance, PageManage *pageManager): QWidget(parent), pageManager(pageManager), homePage(homePageInstance)
{blue_page_Widget = new QWidget(this);blue_page_Widget->setGeometry(0, 0, 800, 480);blue_page_Widget->setFixedSize(800, 480);layoutInit();localAdapterInit();
}Bluetooth_Page::~Bluetooth_Page()
{qDeleteAll(clients);delete server;
}/* 初始化本地蓝牙,作为服务端 */
void Bluetooth_Page::localAdapterInit()
{/* 查找本地蓝牙的个数 */localAdapters = QBluetoothLocalDevice::allDevices();qDebug() << "localAdapter: " << localAdapters.count();QBluetoothLocalDevice localDevice;localDevice.setHostMode(QBluetoothLocalDevice::HostDiscoverable);QBluetoothAddress adapter = QBluetoothAddress();remoteSelector = new RemoteSelector(adapter, this);connect(remoteSelector,SIGNAL(newServiceFound(QListWidgetItem*)),this, SLOT(newServiceFound(QListWidgetItem*)));/* 初始化服务端 */server = new ChatServer(homePage, this);//server = new ChatServer(this);connect(server, SIGNAL(clientConnected(QString)),this, SLOT(connected(QString)));connect(server, SIGNAL(clientDisconnected(QString)),this, SLOT(disconnected(QString)));connect(server, SIGNAL(messageReceived(QString, QString)),this, SLOT(showMessage(QString, QString)));connect(this, SIGNAL(sendMessage(QString)),server, SLOT(sendMessage(QString)));connect(this, SIGNAL(disconnect()),server, SLOT(disconnect()));server->startServer();/* 获取本地蓝牙的名称 */localName = QBluetoothLocalDevice().name();
}void Bluetooth_Page::layoutInit()
{top_frame = new QFrame(blue_page_Widget);top_frame->setGeometry(0, 0, 800, 50);top_frame->setFrameShape(QFrame::NoFrame); // QFrame::BoxtopLabel = new QLabel("蓝牙列表", top_frame);topLabel->setAlignment(Qt::AlignCenter);topFont = topLabel->font();topFont.setPointSize(24);topLabel->setFont(topFont);topLabel->setGeometry(0, -5, 800, 50);backPushButton = new QPushButton("返回", top_frame);backPushButton->setMinimumSize(120, 40);backPushButton->setGeometry(0, 0, 120, 40);connect(backPushButton, SIGNAL(clicked()),this, SLOT(backToHomePage()));//    topHLayout->addSpacerItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));//    topLabel = new QLabel("蓝牙列表", top_frame);
//    topLabel->setAlignment(Qt::AlignCenter);
//    topFont = topLabel->font();
//    topFont.setPointSize(24);
//    topLabel->setFont(topFont);
//    topLabel->setFixedSize(800, 50);
//    topHLayout->addWidget(topLabel);//    topHLayout->addSpacerItem(new QSpacerItem(80, 20, QSizePolicy::Minimum, QSizePolicy::Expanding));//top_frame->setLayout(topHLayout);list_frame = new QFrame(blue_page_Widget);list_frame->setGeometry(0, 60, 600, 400);list_frame->setFrameShape(QFrame::NoFrame); // QFrame::BoxlistWidget = new QListWidget(list_frame);listVLayout = new QVBoxLayout(list_frame);listVLayout->setContentsMargins(0, 0, 0, 0);listVLayout->addWidget(listWidget);list_frame->setLayout(listVLayout);bluetooth_btn_frame = new QFrame(blue_page_Widget);bluetooth_btn_frame->setGeometry(610, 60, 170, 400);bluetooth_btn_frame->setFrameShape(QFrame::NoFrame); // QFrame::Boxbluetooth_btn_VLayout = new QVBoxLayout(bluetooth_btn_frame);bluetooth_btn_VLayout->setContentsMargins(0, 0, 0, 0);bluetooth_btn_frame->setLayout(bluetooth_btn_VLayout);/* 0为扫描按钮,1为连接按钮 */pushButton[0] = new QPushButton();pushButton[1] = new QPushButton();pushButton[2] = new QPushButton();pushButton[3] = new QPushButton();pushButton[4] = new QPushButton();/* 页面一 */bluetooth_btn_VLayout->addWidget(pushButton[0]);bluetooth_btn_VLayout->addWidget(pushButton[1]);bluetooth_btn_VLayout->addWidget(pushButton[2]);bluetooth_btn_VLayout->addWidget(pushButton[3]);pushButton[0]->setMinimumSize(120, 40);pushButton[1]->setMinimumSize(120, 40);pushButton[2]->setMinimumSize(120, 40);pushButton[3]->setMinimumSize(120, 40);pushButton[0]->setText("开始扫描");pushButton[1]->setText("停止扫描");pushButton[2]->setText("连接");pushButton[3]->setText("断开");/* 开始搜寻蓝牙 */connect(pushButton[0], SIGNAL(clicked()),this, SLOT(searchForDevices()));/* 停止搜寻蓝牙 */connect(pushButton[1], SIGNAL(clicked()),this, SLOT(stopSearch()));/* 点击连接按钮,本地蓝牙作为客户端去连接外界的服务端 */connect(pushButton[2], SIGNAL(clicked()),this, SLOT(connectToDevice()));/* 点击断开连接按钮,断开连接 */connect(pushButton[3], SIGNAL(clicked()),this, SLOT(toDisconnected()));/* 发送消息 */connect(pushButton[4], SIGNAL(clicked()),this, SLOT(sendMessage()));
}/* 作为客户端去连接 */
void Bluetooth_Page::connectToDevice()
{if (listWidget->currentRow() == -1)return;QString name = listWidget->currentItem()->text();qDebug() << "Connecting to " << name;// Trying to get the serviceQBluetoothServiceInfo service;QMapIterator<QString,QBluetoothServiceInfo>i(remoteSelector->m_discoveredServices);bool found = false;while (i.hasNext()){i.next();QString key = i.key();/* 判断连接的蓝牙名称是否在发现的设备里 */if (key == name) {qDebug() << "The device is found";service = i.value();qDebug() << "value: " << i.value().device().address();found = true;break;}}/* 如果找到,则连接设备 */if (found) {qDebug() << "Going to create client";ChatClient *client = new ChatClient(this);qDebug() << "Connecting...";connect(client, SIGNAL(messageReceived(QString,QString)),this, SLOT(showMessage(QString,QString)));connect(client, SIGNAL(disconnected()),this, SLOT(clientDisconnected()));;connect(client, SIGNAL(connected(QString)),this, SLOT(connected(QString)));connect(this, SIGNAL(sendMessage(QString)),client, SLOT(sendMessage(QString)));connect(this, SIGNAL(disconnect()),client, SLOT(disconnect()));qDebug() << "Start client";client->startClient(service);clients.append(client);}
}/* 本地蓝牙选择,默认使用第一个蓝牙 */
int Bluetooth_Page::adapterFromUserSelection() const
{int result = 0;QBluetoothAddress newAdapter = localAdapters.at(0).address();return result;
}void Bluetooth_Page::backToHomePage()
{if (pageManager){pageManager->showHomePage();  // 切换到蓝牙页面}else{qDebug() << "Error: pageManager is null!";}
}/* 开始搜索 */
void Bluetooth_Page::searchForDevices()
{/* 先清空 */listWidget->clear();qDebug() << "search for devices!";if (remoteSelector) {delete remoteSelector;remoteSelector = NULL;}QBluetoothAddress adapter = QBluetoothAddress();remoteSelector = new RemoteSelector(adapter, this);connect(remoteSelector,SIGNAL(newServiceFound(QListWidgetItem*)),this, SLOT(newServiceFound(QListWidgetItem*)));remoteSelector->m_discoveredServices.clear();remoteSelector->startDiscovery(QBluetoothUuid(serviceUuid));connect(remoteSelector, SIGNAL(finished()),this, SIGNAL(discoveryFinished()));
}/* 停止搜索 */
void Bluetooth_Page::stopSearch()
{qDebug() << "Going to stop discovery...";if (remoteSelector) {remoteSelector->stopDiscovery();}
}/* 找到蓝牙服务 */
void Bluetooth_Page::newServiceFound(QListWidgetItem *item)
{/* 设置项的大小 */item->setSizeHint(QSize(listWidget->width(), 50));/* 添加项 */listWidget->addItem(item);/* 设置当前项 */listWidget->setCurrentRow(listWidget->count() - 1);qDebug() << "newServiceFound";// get all of the found devicesQStringList list;QMapIterator<QString, QBluetoothServiceInfo>i(remoteSelector->m_discoveredServices);while (i.hasNext()){i.next();qDebug() << "key: " << i.key();qDebug() << "value: " << i.value().device().address();list << i.key();}qDebug() << "list count: "  << list.count();emit newServicesFound(list);
}/* 已经连接 */
void Bluetooth_Page::connected(const QString &name)
{qDebug() << "已连接\n";if (pageManager){pageManager->showHomePage();  // 切换到蓝牙页面}else{qDebug() << "Error: pageManager is null!";}
}/* 接收消息 */
void Bluetooth_Page::showMessage(const QString &sender,const QString &message)
{if (pageManager){pageManager->showHomePage();  // 切换到蓝牙页面}else{qDebug() << "Error: pageManager is null!";}
}/* 发送消息 */
void Bluetooth_Page::sendMessage()
{showMessage(localName, lineEdit->text());emit sendMessage(lineEdit->text());
}/* 作为客户端断开连接 */
void Bluetooth_Page::clientDisconnected()
{ChatClient *client = qobject_cast<ChatClient *>(sender());if (client) {clients.removeOne(client);client->deleteLater();}if (pageManager){pageManager->showHomePage();  // 切换到蓝牙页面}else{qDebug() << "Error: pageManager is null!";}
}/* 主动断开连接 */
void Bluetooth_Page::toDisconnected()
{emit disconnect();
}/* 作为服务端时,客户端断开连接 */
void Bluetooth_Page::disconnected(const QString &name)
{qDebug() << "已断开\n";if (pageManager){pageManager->showHomePage();  // 切换到蓝牙页面}else{qDebug() << "Error: pageManager is null!";}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1559814.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

实验3 专题地图制图及其拓展

1.主观题 (100分) 实验3 专题地图制图及其拓展 实验目的&#xff1a;熟练掌握符号化、标注注记、格网设计和地图整饰的内容。了解空间插值的概念和意义&#xff0c;学习“地理空间统计”基础。 实验要求&#xff1a;本实验报告内容要求详细的文字过程描述以及三幅结果出图&…

杨校老师项目之基于QT的双色球彩票走势分析系统的设计与实现

获取代码&#xff1a; 知识付费时代&#xff0c;低价有偿获取代码&#xff0c;请理解&#xff01; (1) 下载链接: 后发(2) 添加博主微信获取&#xff08;有偿&#xff09;,备注来源: mryang511688项目描述 技术&#xff1a;C、QT等 摘要&#xff1a; 随着生活水平的挺高&…

【LeetCode】每日一题 2024_10_10 优质数对的总数 I(暴力/哈希)

前言 每天和你一起刷 LeetCode 每日一题~ LeetCode 启动&#xff01; 题目&#xff1a;优质数对的总数 I 代码与解题思路 简单题先暴力~ 直接对着题意模拟即可&#xff0c;力扣上只要是标着简单标签的题目&#xff0c;不用犹豫&#xff0c;直接对他使用暴力吧&#xff01; …

Unity 从零开始的框架搭建1-2 事件的发布-订阅-取消的小优化及调用对象方法总结[半干货]

该文章专栏是向QFrameWork作者凉鞋老师学习总结得来&#xff0c;吃水不忘打井人&#xff0c;不胜感激 Unity 从零开始的框架搭建1-1 unity中对象调用的三种方式的优缺点分析【干货】-CSDN博客 原来 其实就是对上一节的事件发布订阅类的小优化&#xff0c;原来是这样子的 p…

转行在大模型公司做Prompter是种怎样的体验

2014-2019&#xff1a;安徽双非-城乡规划-本科 2019-2022&#xff1a;南京985-城市规划-硕士 工作经历 2022.07-2023.07&#xff1a;设计院做城市设计&#xff1b; 2023.08-至今&#xff1a;国内大模型公司prompter&#xff08;提示词工程师&#xff09; 01 2022年刚毕业…

IDEA使用Maven创建父与子多模块项目

在 IntelliJ IDEA 中使用 Maven 创建父与子多模块项目是一个常见的开发实践&#xff0c;有助于更好地组织和管理代码。在多模块项目中&#xff0c;可以将公共的代码、资源或配置抽离到独立的模块中&#xff0c;然后在其他模块中直接引用。这样可以避免代码重复&#xff0c;提高…

第十一章:规划过程组 (11.1制定项目管理计划--11.5创建WBS)

11.1 制定项目管理计划 • 项目管理计划可以是概括或详细的&#xff0c;每个组成部分的详细程度取决于具体项目的要求 • 项目管理计划应基准化&#xff0c;即至少应规定项目的范围、时间和成本方面的基准以便据此考核项目执行情况和管理项目绩效。 • 在确定基准之前&#xf…

汽车胶黏剂市场研究:预计2030年全球市场规模将达到67.4亿美元

汽车胶黏剂是指专门用于汽车制造和维修过程中&#xff0c;用于粘接、密封和固定各种汽车部件的化学材料。它们在汽车行业中扮演着关键角色&#xff0c;广泛应用于车身、内饰、玻璃、电子元件和其他组件的粘接与密封。汽车胶黏剂旨在提高汽车的结构强度、耐用性、密封性以及舒适…

Spark第一天

MapReduce过程复习 Spark由五部分组成 RDD五大特征 1、 Spark -- 代替MapReduce <<<<< scala是单机的&#xff0c;spark是分布式的。>>>>> 开源的分布式计算引擎 可以快速做计算 -- 因为可以利用内存来做一些计算 (1) 分为5个库(模块) : 1、…

打不死的超强生命力

水熊虫是你可能听说过的小生物&#xff0c;它们能够在极端环境中生存&#xff0c;堪称地球上的“超强幸存者”。数十年来&#xff0c;科学家们试图通过各种极端实验杀死它们&#xff0c;但无论是把它们以900米/秒的速度发射&#xff0c;还是将它们暴露在宇宙辐射下&#xff0c;…

Linux系统本地搭建轻量级文件共享系统PicoShare远程连接实战

前言 本篇文章介绍&#xff0c;如何在Linux系统本地部署轻量级文件共享系统PicoShare&#xff0c;并结合Cpolar内网穿透实现公网环境远程传输文件至本地局域网内文件共享系统。 PicoShare 是一个由 Go 开发的轻量级开源共享文件系统&#xff0c;它没有文件限制&#xff0c;允…

CDGA|数据治理:企业创新发展的强劲引擎

在当今这个数据驱动的时代&#xff0c;企业之间的竞争已经悄然转变为对数据资源的争夺与利用能力的比拼。数据&#xff0c;作为新的生产要素&#xff0c;正以前所未有的速度改变着企业的运营模式、决策方式乃至整个商业生态。而数据治理&#xff0c;作为确保数据质量、安全性、…

FPGA上板调试方式总结----VIO/ILA

在Vivado中&#xff0c;VIO&#xff08;Virtual Input/Output&#xff09;是一种用于调试和测试FPGA设计的IP核&#xff0c;它允许设计者通过JTAG接口实时读取和写入FPGA内部的寄存器&#xff0c;从而检查设计的运行状态并修改其行为。VIO IP核提供了一个简单易用的接口&#x…

二叉树系列(遍历/dfs/bfs)10.10

一、二叉树的右视图(遍历) 给定一个二叉树的 根节点 root&#xff0c;想象自己站在它的右侧&#xff0c;按照从顶部到底部的顺序&#xff0c;返回从右侧所能看到的节点值。 &#xff08;如果右子树为空的话&#xff0c;那么右视图中看到的就是左子树的节点&#xff09; bfs层…

IPguard与Ping32 DLP能力对比,保护企业数据的最佳选择

在信息安全的背景下&#xff0c;数据丢失防护&#xff08;DLP&#xff09;解决方案已成为企业不可或缺的一部分。IPguard和Ping32是市场上两款功能强大的DLP产品。本文将对它们的DLP能力进行详细对比&#xff0c;帮助企业找到最适合自己的数据保护工具。 Ping32的独特之处 Pin…

java复制查询数组-cnblog

java数组 复制数组 copyOf(待复制数组,复制后新数组的长度) 如果复制后数组的长度&#xff0c;长于原来数组&#xff0c;多出来的元素会被补0&#xff0c;如果新数组元素少会从第一个元素&#xff0c;取到指定元素长度 package nb;import java.util.Arrays;public class co…

2024年,有多少程序员被迫转行?真是惨烈啊!

知乎有个很火热的帖子&#xff0c;很多人在讨论今年有多少程序员在被迫转行&#xff0c;原来今年的程序员这么难。 有个老哥说自己干了8年前端程序员&#xff0c;今年被裁之后&#xff0c;薪资从30K降到25K还是没找到工作&#xff0c;现在只能转行去卖保险。 还有一个38岁的老哥…

深度解析|生成式人工智能大模型备案全流程

一、大模型备案的含义 根据《生成式人工智能服务管理暂行办法》第十七条 提供具有舆论属性或者社会动员能力的生成式人工智能服务的&#xff0c;应当按照国家有关规定开展安全评估。这里所说的按照国家有关规定开展安全评估&#xff0c;其实就是生成式人工智能服务备案&#x…

Python_网络编程(IP 端口 协议)

网络编程&#xff1a; 互联网时代&#xff0c;现在基本上所有的程序都是网络程序&#xff0c;很少有单机版的程序了。网络编程就是如何在程序中实现两台计算机的通信。Python语言中&#xff0c;提供了大量的内置模块和第三方模块用于支持各种网络访问&#xff0c;而且Python语言…

JAVA毕业设计187—基于Java+Springboot+vue3的电动车销售管理系统(源代码+数据库)

毕设所有选题&#xff1a; https://blog.csdn.net/2303_76227485/article/details/131104075 基于JavaSpringbootvue3的电动车销售管理系统(源代码数据库)187 一、系统介绍 本项目前后端分离(可以改为ssm版本)&#xff0c;分为用户、管理员两种角色 1、用户&#xff1a; 注…