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);}

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

相关推荐
寻找华年的锦瑟11 小时前
Qt-配置文件(INI/JSON/XML)
开发语言·qt
gd632137411 小时前
银河麒麟 aarch64 linux 里面的 qt 怎么安装kit
linux·服务器·qt
Jay-juice12 小时前
QT信号与槽
开发语言·qt
代码程序猿RIP12 小时前
【Qt】Qt
qt
陈增林13 小时前
基于 PyQt5 的多算法视频关键帧提取工具
开发语言·qt·算法
ajassi200015 小时前
开源 C++ QT QML 开发(二十一)多媒体--视频播放
c++·qt·开源
AI+程序员在路上16 小时前
QT6中QToolBox功能与应用
开发语言·qt
进击的大海贼1 天前
QT-C++ 自定义加工统计通用模块
开发语言·c++·qt
友友马1 天前
『 QT 』QT信号机制深度解析
开发语言·qt
nnerddboy1 天前
QT(c++)开发自学笔记:2.TCP/IP
c++·笔记·qt