#include "mainwindow.h"
#include "ui_mainwindow.h"
/*
* 设置QT界面的样式
*/
void MainWindow::SetStyle(const QString &qssFile) {
QFile file(qssFile);
if (file.open(QFile::ReadOnly)) {
QString qss = QLatin1String(file.readAll());
qApp->setStyleSheet(qss);
QString PaletteColor = qss.mid(20,7);
qApp->setPalette(QPalette(QColor(PaletteColor)));
file.close();
}
else
{
qApp->setStyleSheet("");
}
}
//#蓝牙串口服务
//SerialPortServiceClass_UUID = '{00001101-0000-1000-8000-00805F9B34FB}'
//Service UUID 0xFEE0 主服务
//static const QLatin1String serviceUuid("0000FEE0-0000-1000-8000-00805F9B34FB");
//这个字符串里面的内容就是串口模式的Uuid
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->SetStyle(":/qss/blue.css"); //设置样式表
this->setWindowTitle("BLE蓝牙调试助手"); //设置标题
this->setWindowIcon(QIcon(":/wbyq.ico")); //设置图标
/*1. 实例化蓝牙相关的对象*/
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
localDevice = new QBluetoothLocalDevice();
/*2. 关联蓝牙设备相关的信号*/
/*2.1 关联发现设备的槽函数,当扫描发现周围的蓝牙设备时,会发出deviceDiscovered信号*/
connect(discoveryAgent,
SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this,
SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo))
);
/*3. 检查蓝牙的状态,用于设置按钮的初始状态*/
/*3.1 检查蓝牙是否开启*/
if(localDevice->hostMode() == QBluetoothLocalDevice::HostPoweredOff)
{
//如果蓝牙处于关闭状态
ui->pushButton_OpenBluetooth->setEnabled(true); //打开按钮
ui->pushButton_CloseBluetooth->setEnabled(false); //关闭按钮
}
else //如果蓝牙处于开启状态
{
ui->pushButton_OpenBluetooth->setEnabled(false);//打开按钮
ui->pushButton_CloseBluetooth->setEnabled(true);//关闭按钮
ui->pushButton_BluetoothScan->setEnabled(true); //设置扫描按钮可用
}
/*3.2 设置标签显示本地蓝牙的名称*/
QString name_info("本机蓝牙:");
name_info+=localDevice->name();
ui->label_BluetoothName->setText(name_info);
ui->pushButton_StopScan->setEnabled(false); //设置停止扫描蓝牙的按钮不可用
ui->plainTextEdit_BluetoothInfiShow->setEnabled(false); //设置不可编辑
m_control=NULL; //初始值
m_service=NULL; //初始值
SendModeSelect=0;
SendMaxMode=0;
}
MainWindow::~MainWindow()
{
delete ui;
delete discoveryAgent;
delete localDevice;
}
void MainWindow::on_pushButton_OpenBluetooth_clicked()
{
/*请求打开蓝牙设备*/
localDevice->powerOn();
ui->pushButton_OpenBluetooth->setEnabled(false);//打开按钮
ui->pushButton_CloseBluetooth->setEnabled(true);//关闭按钮
ui->pushButton_BluetoothScan->setEnabled(true); //设置扫描按钮可用
}
void MainWindow::on_pushButton_CloseBluetooth_clicked()
{
/*关闭蓝牙设备*/
localDevice->setHostMode(QBluetoothLocalDevice::HostPoweredOff);
ui->pushButton_OpenBluetooth->setEnabled(true);//打开按钮
ui->pushButton_CloseBluetooth->setEnabled(false);//关闭按钮
ui->pushButton_BluetoothScan->setEnabled(false); //设置扫描按钮不可用
}
void MainWindow::on_pushButton_BluetoothScan_clicked()
{
/*开始扫描周围的蓝牙设备*/
discoveryAgent->start();
ui->comboBox_BluetoothDevice->clear(); //清除条目
ui->pushButton_BluetoothScan->setEnabled(false); //设置扫描按钮不可用
ui->pushButton_StopScan->setEnabled(true); //设置停止扫描按钮可用
}
void MainWindow::on_pushButton_StopScan_clicked()
{
/*停止扫描周围的蓝牙设备*/
discoveryAgent->stop();
ui->pushButton_StopScan->setEnabled(false); //设置停止扫描按钮不可用
ui->pushButton_BluetoothScan->setEnabled(true); //设置扫描按钮可用
}
/*当扫描到周围的设备时会调用当前的槽函数*/
void MainWindow::addBlueToothDevicesToList(const QBluetoothDeviceInfo &info)
{
// QString label = QString("%1 %2").arg(info.name()).arg(info.address().toString());
QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
ui->comboBox_BluetoothDevice->addItem(label); //添加字符串到comboBox上
}
/*
在说蓝牙设备连接之前,不得不提一个非常重要的概念,就是蓝牙的Uuid,引用一下百度的:
在蓝牙中,每个服务和服务属性都唯一地由"全球唯一标识符" (UUID)来校验。
正如它的名字所暗示的,每一个这样的标识符都要在时空上保证唯一。
UUID类可表现为短整形(16或32位)和长整形(128位)UUID。
他提供了分别利用String和16位或32位数值来创建类的构造函数,提供了一个可以比较两个UUID(如果两个都是128位)的方法,还有一个可以转换一个UUID为一个字符串的方法。
UUID实例是不可改变的(immutable),只有被UUID标示的服务可以被发现。
在Linux下你用一个命令uuidgen -t可以生成一个UUID值;
在Windows下则执行命令uuidgen 。UUID看起来就像如下的这个形式:2d266186-01fb-47c2-8d9f-10b8ec891363。当使用生成的UUID去创建一个UUID对象,你可以去掉连字符。
*/
//发送数据
void MainWindow::on_pushButton_SendData_clicked()
{
QString text=ui->lineEdit_SendData->text();
QByteArray array=text.toLocal8Bit();
/*写入newValue作为特性的值。
如果操作成功,将发射characteristicWritten()信号;
低功耗设备: 每次最多写20个字节
*/
m_service->writeCharacteristic(m_writeCharacteristic[SendModeSelect],array, m_writeMode);
}
//清空收到的数据
void MainWindow::on_pushButton_Clear_clicked()
{
ui->plainTextEdit_BluetoothInfiShow->setPlainText("");
}
//连接蓝牙
void MainWindow::on_pushButton_ConnectDev_clicked()
{
QString text = ui->comboBox_BluetoothDevice->currentText();
int index = text.indexOf(' ');
if(index == -1) return;
QBluetoothAddress address(text.left(index));
QString connect_device="开始连接蓝牙设备:\n";
connect_device+=ui->comboBox_BluetoothDevice->currentText();
QMessageBox::information(this,tr("连接提示"),connect_device);
/*低功耗蓝牙设备*/
if(m_control!=NULL)
{
m_control->disconnectFromDevice(); //断开远程设备
delete m_control;
m_control = NULL;
}
ui->comboBox_UUID->clear(); //清除显示UUID服务的列表框
QList<QBluetoothDeviceInfo> info_list=discoveryAgent->discoveredDevices(); //得到扫描的所有设备信息
for(int i=0;i<info_list.count();i++)
{
if(info_list.at(i).address().toString()==text.left(index))
{
remoteDevice=info_list.at(i);
ui->plainTextEdit_BluetoothInfiShow->insertPlainText("连接设备:");
ui->plainTextEdit_BluetoothInfiShow->insertPlainText(remoteDevice.name());
ui->plainTextEdit_BluetoothInfiShow->insertPlainText("\n");
ui->plainTextEdit_BluetoothInfiShow->insertPlainText(remoteDevice.address().toString());
ui->plainTextEdit_BluetoothInfiShow->insertPlainText("\n");
break;
}
}
//创建中央角色设备
m_control = new QLowEnergyController(remoteDevice, this);
//m_control=QLowEnergyController::createCentral(remoteDevice,this);
if(m_control==0)
{
ui->plainTextEdit_BluetoothIn
- 1
- 2
- 3
- 4
- 5
- 6
前往页