当前位置:   article > 正文

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

qt蓝牙上位机

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

        成果

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

        

         实现步骤

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

        

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

  1. void MainWindow::on_pushButton_openBLE_clicked()
  2. {
  3. if( localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff)//开机没有打开蓝牙
  4. {
  5. localDevice->powerOn();//调用打开本地的蓝牙设备
  6. discoveryAgent->start();//开始扫描蓝牙设备
  7. }
  8. else
  9. {
  10. QMessageBox::information(this, tr("成功"), tr("蓝牙已打开"));
  11. }
  12. }

        关闭的代码也很简单

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

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

  1. //刷新 重新查找蓝牙设备
  2. void MainWindow::on_pushButton_upDateBLE_clicked()
  3. {
  4. discoveryAgent->start();
  5. ui->listWidget->clear();
  6. }
  7. //在ListWidget上显示查找到的蓝牙设备
  8. void MainWindow::addBlueToothDevicesToList(const QBluetoothDeviceInfo &info)
  9. {
  10. QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
  11. QList<QListWidgetItem *> items = ui->listWidget->findItems(label, Qt::MatchExactly);
  12. if (items.empty())
  13. {
  14. QListWidgetItem *item = new QListWidgetItem(label);
  15. QBluetoothLocalDevice::Pairing pairingStatus = localDevice->pairingStatus(info.address());
  16. /* 蓝牙状态pairingStatus,Pairing枚举类型
  17. * 0:Unpaired没配对
  18. * 1:Paired配对但没授权
  19. * 2:AuthorizedPaired配对且授权 */
  20. if (pairingStatus == QBluetoothLocalDevice::Paired || pairingStatus == QBluetoothLocalDevice::AuthorizedPaired )
  21. item->setTextColor(QColor(Qt::red));
  22. else
  23. item->setTextColor(QColor(Qt::black));
  24. ui->listWidget->addItem(item);
  25. }
  26. }

        4.建立连接

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

static const QLatin1String serviceUuid("00001101-0000-1000-8000-00805F9B34FB");

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

socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);

        然后就是连接

  1. //蓝牙连接
  2. void MainWindow::connectBLE(QListWidgetItem *item)
  3. {
  4. QString text = item->text();
  5. int index = text.indexOf(' ');
  6. if (index == -1)
  7. return;
  8. QBluetoothAddress address(text.left(index));
  9. QString name(text.mid(index + 1));
  10. QMessageBox::information(this,tr("提示"),tr("设备正在连接中..."));
  11. socket->connectToService(address, QBluetoothUuid(serviceUuid) ,QIODevice::ReadWrite);
  12. }

      4.发送数据

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

  1. void MainWindow::on_pushButton_led_on_clicked()
  2. {
  3. blueStates();
  4. socket->write(LED_ON.toLatin1()); //转成 QByteArray 进行发送
  5. }

      5.接收数据

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

  1. void MainWindow::readBluetoothDataEvent()
  2. {
  3. //这里数据需要自己做处理,不然发送接受不成功,提示:用定时器定时接受
  4. // QByteArray line = socket->readAll();
  5. QBAtemp = socket->readAll();
  6. Qstrtemp.clear();
  7. qDebug() <<"read mycom";
  8. for(int j = 0 ; j < QBAtemp.length();j++)
  9. { Qstrtemp += QBAtemp[j];
  10. qDebug()<<j<<":"<<QBAtemp[j];
  11. }
  12. ui->textBrowser_receive->moveCursor(QTextCursor::End);
  13. ui->textBrowser_receive->insertPlainText(Qstrtemp);}

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/622906
推荐阅读
相关标签
  

闽ICP备14008679号