柚子快报邀请码778899分享:QT简易蓝牙上位机(智能家居)

http://yzkb.51969.com/

        之前做了串口助手,能够收发信息和单片机通信,然后那是有线的,于是就考虑做个无线的,刚好手里有个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 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()<

}

ui->textBrowser_receive->moveCursor(QTextCursor::End);

ui->textBrowser_receive->insertPlainText(Qstrtemp);}

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

柚子快报邀请码778899分享:QT简易蓝牙上位机(智能家居)

http://yzkb.51969.com/

好文链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: