QT简易蓝牙上位机(智能家居)

之前做了串口助手,能够收发信息和单片机通信,然后那是有线的,于是就考虑做个无线的,刚好手里有个BT06的蓝牙模块,于是就做了个蓝牙上位机,和串口的差别其实不大。

成果

能够成果搜索并且连接到蓝牙模块,也是实现了收发,可以控制灯光、风扇、舵机。

实现步骤

1.项目环境文件:这里需要注意的是要使用高版本的qt,我之前的qt5.9.8也因此退休了,现在换成了5.11,否则他就会在运行和配置的时候报错,没有蓝牙相关的方法

2.要想连接蓝牙,首先得打开自己设备的蓝牙

复制代码
void MainWindow::on_pushButton_openBLE_clicked()
{
    if( localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff)//开机没有打开蓝牙
    {
        localDevice->powerOn();//调用打开本地的蓝牙设备
        discoveryAgent->start();//开始扫描蓝牙设备
    }
    else
    {
         QMessageBox::information(this, tr("成功"), tr("蓝牙已打开"));
    }
}

关闭的代码也很简单

复制代码
// 关闭 断开已连接的蓝牙设备 close设备和我们的open设备的方法在形式上不一样,只能用这样的方法对蓝牙进行关闭。
void MainWindow::on_pushButton_closeBLE_clicked()
{
    socket->close();
    QMessageBox::information(this, tr("成功"), tr("已断开连接"));

}

3.蓝牙设备的查找 用到 discoveryAgent 这个类的实例化,再将查找到的设备打印出来,给一个双击就触发连接的槽函数

复制代码
//刷新 重新查找蓝牙设备
void MainWindow::on_pushButton_upDateBLE_clicked()
{
    discoveryAgent->start();
    ui->listWidget->clear();
}
//在ListWidget上显示查找到的蓝牙设备
void MainWindow::addBlueToothDevicesToList(const QBluetoothDeviceInfo &info)
{
    QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
    QList<QListWidgetItem *> items = ui->listWidget->findItems(label, Qt::MatchExactly);

    if (items.empty())
    {
        QListWidgetItem *item = new QListWidgetItem(label);
        QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(info.address());
        /* 蓝牙状态pairingStatus,Pairing枚举类型
         * 0:Unpaired没配对
         * 1:Paired配对但没授权
         * 2:AuthorizedPaired配对且授权 */
        if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired )
            item->setTextColor(QColor(Qt::red));
        else
            item->setTextColor(QColor(Qt::black));
        ui->listWidget->addItem(item);
    }
}

4.建立连接

首先的有一个Uuid(全球唯一标识符)

复制代码
static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB");

然后使用蓝牙的socket:这里选择配置的是串口模式

复制代码
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);

然后就是连接

复制代码
//蓝牙连接
void MainWindow::connectBLE(QListWidgetItem *item)
{
    QString text = item->text();
    int index = text.indexOf(' ');
    if (index == -1)
        return;
    QBluetoothAddress address(text.left(index));
    QString name(text.mid(index + 1));
    QMessageBox::information(this,tr("提示"),tr("设备正在连接中..."));
    socket->connectToService(address, QBluetoothUuid(serviceUuid) ,QIODevice::ReadWrite);
}

4.发送数据

调用socke的write方法即可,注意发送的类型是QByteArray

复制代码
void MainWindow::on_pushButton_led_on_clicked()
{
    blueStates();
    socket->write(LED_ON.toLatin1()); //转成 QByteArray 进行发送
}

5.接收数据

readyRead()信号触发,跳进readBluetoothDataEvent中,使用readAll读取.,,这里我考虑后面处理温湿度的数据,于是用来个数组。

复制代码
void MainWindow::readBluetoothDataEvent()
{    
    //这里数据需要自己做处理,不然发送接受不成功,提示:用定时器定时接受
   // QByteArray line = socket->readAll();
    QBAtemp = socket->readAll();
    Qstrtemp.clear();
    qDebug() <<"read mycom";
    for(int j = 0 ; j < QBAtemp.length();j++)
    { Qstrtemp += QBAtemp[j];
        qDebug()<<j<<":"<<QBAtemp[j];
}
    ui->textBrowser_receive->moveCursor(QTextCursor::End);
    ui->textBrowser_receive->insertPlainText(Qstrtemp);}

到这里就结束了,希望对大家有所帮助,后面考虑把界面做得炫酷一点。

相关推荐
小鱼仙官9 小时前
使用Qt封装C/C++ 动态库 用于第三方调用
c语言·c++·qt
扶尔魔ocy11 小时前
【QT OpenGL】知识点编目及传送门
qt·opengl·osg
qq_4017004112 小时前
Qt Modbus 源码剖析:从 QModbusClient 到工业级通信框架
开发语言·qt·系统架构
GreenHandBruce12 小时前
基于Qt 6.12.0 + FFmpeg 9.0.2的4k视频播放器
qt·ffmpeg·音视频
誰能久伴不乏15 小时前
桌面客户端的运行日志系统:SQLite 分块 + 独立写线程 + 文本降级
数据库·c++·qt·架构·sqlite
沫璃染墨1 天前
《Qt从零入门系列(十五):Qt线程安全与锁机制详解——从QMutex到RAII与读写锁实战》
开发语言·c++·qt·安全·交互·线程·qt6.3
小豆庄1 天前
视频脱敏引擎的追踪难题:转场误判定、ID撞号和淡入窗口
qt·算法·目标检测·音视频·视频·敏捷流程
小灰灰搞电子2 天前
Qt 中使用 QSettings 存储结构体数据详解
开发语言·qt
LaoWaiHang2 天前
关于Qt中鼠标左键事件被子控件截留的处理
qt
初阳7852 天前
【基于Qt5仿音乐播放器(一)】——项目准备
qt·1024程序员节