只测试过自己电脑,系统版本如下:
查看方式按win+R,然后输入winver确定即可:
查看设备管理器,确定自己电脑是否支持蓝牙模块:
需要选择MSVC版本编译,MinGW不支持Windows下的蓝牙模块,这也就限制了Qt的版本(早期有些版本没有MSVC编译器),本文使用的是Qt 5.12.6 MSVC2017 64bit。
电脑端作为主机,蓝牙模块作为从机,测试用的是在某宝买的蓝牙模块:
使用USB转串口模块和蓝牙模块按如下连接:
电脑端打开串口助手,连接USB转串口模块的端口,默认波特率是9600:
如果右下角没有,如下也可以找到:
在本文的上面一点有提到如何修改蓝牙名字,和PIN码,配对时就需要用到PIN码:
输入后点击连接即可配对成功:
头文件主要代码:
#include <QBluetoothDeviceInfo>
#include <qbluetoothdevicediscoveryagent.h>
private slots:
void discoverBlueTooth(QBluetoothDeviceInfo info);
void scanFinished();
private:
QBluetoothLocalDevice *localDevice;
cpp文件主要代码:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
discoveryAgent = new QBluetoothDeviceDiscoveryAgent;
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(discoverBlueTooth(QBluetoothDeviceInfo)));
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
discoveryAgent->start();
}
void MainWindow::discoveryFinished()
{
qDebug()<<"discoveryFinished";
}
void MainWindow::discoverBlueTooth(QBluetoothDeviceInfo info)
{
QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
qDebug()<<label;
}
头文件主要代码:
#include <QBluetoothSocket>
#include <QBluetoothUuid>
#include <QBluetoothAddress>
#include <QIODevice>
private slots:
void readBluetoothDataEvent();
void bluetoothConnectedEvent();
private:
QString BTaddress; // 记录MAC地址
QBluetoothSocket *socket;
cpp文件主要代码:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
discoveryAgent = new QBluetoothDeviceDiscoveryAgent;
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)), this, SLOT(discoverBlueTooth(QBluetoothDeviceInfo)));
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(discoveryFinished()));
discoveryAgent->start();
}
void MainWindow::discoveryFinished()
{
qDebug()<<"discoveryFinished";
static QString serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol);
socket->connectToService(QBluetoothAddress(BTaddress), QBluetoothUuid(serviceUuid),QIODevice::ReadWrite);
connect(socket,SIGNAL(readyRead()), this, SLOT(readBluetoothDataEvent()));
connect(socket,SIGNAL(connected()), this, SLOT(bluetoothConnectedEvent()));
}
void MainWindow::discoverBlueTooth(QBluetoothDeviceInfo info)
{
QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name());
if(info.name()=="HC-06")
{
BTaddress = info.address().toString();
}
qDebug()<<label;
}
void MainWindow::readBluetoothDataEvent()
{
char data[100];
qint64 len = socket->read((char *)data, 100);
QByteArray qa2((char*)data,len);
QString qstr(qa2.toHex());//
qDebug()<<"----"<<qstr.toUpper();
}
void MainWindow::bluetoothConnectedEvent()
{
qDebug()<<"bluetoothConnectedEvent";
}
MainWindow::~MainWindow()
{
delete ui;
}
代码中有一行:
static QString serviceUuid("00001101-0000-1000-8000-00805F9B34FB");
蓝牙不仅仅有串口服务,还具有比如蓝牙耳机的音频服务,文件传输服务等,就是通过这个UUID来区分,不同的UUID有不同的含义,点击这里可以看到网友列出来的相关UUID:
数据接收在上面的槽函数readBluetoothDataEvent()里面:
void MainWindow::readBluetoothDataEvent()
{
char data[100];
qint64 len = socket->read((char *)data, 100);
QByteArray qa2((char*)data,len);
QString qstr(qa2.toHex());//
qDebug()<<"----"<<qstr.toUpper();
}
数据发送可以用连接的socket来实现:
void MainWindow::bluetoothDataSend(QString str)
{
QByteArray arrayData;
arrayData = str.toUtf8();
socket->write(arrayData);
}
// 连接成功的事件中添加一行发送数据:
void MainWindow::bluetoothConnectedEvent()
{
qDebug()<<"bluetoothConnectedEvent";
bluetoothDataSend("hellow bluetooth");
}