Qt windows端的蓝牙串口服务

  • 环境
    • 系统
    • Qt
    • 蓝牙模块
  • 使用步骤
    • 蓝牙模块参数获取
    • 配对
    • 扫描、连接、数据收发
      • 扫描
      • 连接
      • 数据收发

环境

系统

只测试过自己电脑,系统版本如下:

查看方式按win+R,然后输入winver确定即可:

查看设备管理器,确定自己电脑是否支持蓝牙模块:

Qt

需要选择MSVC版本编译,MinGW不支持Windows下的蓝牙模块,这也就限制了Qt的版本(早期有些版本没有MSVC编译器),本文使用的是Qt 5.12.6 MSVC2017 64bit。

蓝牙模块

电脑端作为主机,蓝牙模块作为从机,测试用的是在某宝买的蓝牙模块:

使用步骤

蓝牙模块参数获取

使用USB转串口模块和蓝牙模块按如下连接:

电脑端打开串口助手,连接USB转串口模块的端口,默认波特率是9600:


(网络截图)

配对

双击电脑的右下角的蓝牙图标:

如果右下角没有,如下也可以找到:




在本文的上面一点有提到如何修改蓝牙名字,和PIN码,配对时就需要用到PIN码:

输入后点击连接即可配对成功:

扫描、连接、数据收发

新建工程,在.pro加入bluetooth:

扫描

头文件主要代码:

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

即可实现蓝牙的串口交互(这里的波特率是根据上面的修改方式改过,如果没改默认是9600):

本文的demo工程。

Qt windows端的蓝牙串口服务相关推荐

  1. qt windows ble低功耗蓝牙

    注意事项: 1.qt版本必须在5.14.2及以上 2.开发环境必须使用msvc 如果使用MingGW会导致无法发现任何设备 3. 必须添加qt中 bluetooth 库,qt中开发在.pro中添加 Q ...

  2. 基于Qt的Windows平台桌面蓝牙调试器软件

    基于Qt的Windows平台桌面蓝牙调试器软件 在进行嵌入式的调试时,我们经常需要下位机给主机打印数据来观察数据变化.绘制波形等进行调试.一般的做法可以是直接通过串口与PC进行数据通信(USB to ...

  3. android 蓝牙串口数据格式,Android 蓝牙串口调试程序开发

    前言:本次项目需要为智能设备开发一个 App 于是就开始学习 Android 的蓝牙串口通信方面的知识,现在 App 已经写完了,当初学的时候走了不少弯路和尝试,现作为一名初学者和大家分享一下经验,以 ...

  4. BlackBerry 10 BlackBerry OS 7 1 手机通过蓝牙串口读取Arduino 蓝牙传过来的温度

    目标:BlackBerry手机通过蓝牙串口读取Arduino 蓝牙传过来的温度 湿度信息 硬件:Arduino主板 + DHT11温湿度传感器 + 蓝牙模块,开发工具C语言 手机:BlackBerry ...

  5. Qt工作笔记-Qt生成dll或so,并且调用(含Liunx端与Windows端)

    目录 共享库生成 Linux端编译及调用 Windows端编译及调用 共享库生成 注意:编译的时候要用lib,相当于列表,关联dll中的函数! 这里首先用Qt Creator 新建一个共享库 如下图: ...

  6. Qt:Qt实现Winsock网络编程—TCP服务端和客户端通信(多线程)

    Qt实现Winsock网络编程-TCP服务端和客户端通信(多线程) 前言 感觉Winsock网络编程的api其实和Linux下网络编程的api非常像,其实和其他编程语言的网络编程都差不太多.博主用Qt ...

  7. 在 Windows 上安装 SVN Subversion 服务端

    现在的Subversion已经足够稳定,而且已经进入了它的黄金时段.我们看到大量的项目都在使用它完成版本控件的工作.本文将主要针对Windows平台讲述安装Subversion服务器的步骤.本文所使用 ...

  8. Windows端安装Oracle客户端并配置服务、Oracle数据库备份还原以及修改数据库编码

    配置客户端服务 1.安装Oracle客户端步骤省略... 2.点击[开始菜单]-> [Oracle] -> [Net Configuration Assistant] 3.[本地 Net ...

  9. 基于QT的安卓手机蓝牙APP开发

    摘要:前段时间用QT写了一个串口调试助手,感觉还可以.因为QT是跨平台的,同样一套代码可以在windows上面跑,也可以在linux上面跑,也可以在安卓手机上面跑.而且不需要修改任何东西,编译器会自动 ...

最新文章

  1. 使用RSA算法解析令牌
  2. 盲僧一键r闪用什么设置_美加狮R.A.T. PRO X3至尊版带你畅玩模拟飞行
  3. java实用类_Java—实用类
  4. django的get和filter方法的区别
  5. CentOS系统时间与网络同步
  6. Word Frequency(Leetcode192)
  7. 阿里云服务器搭建过程(小白起步)
  8. 2021-07-13网络术语解释
  9. 数据库范式(1 2 3 BCNF范式)详解
  10. python蜂鸣器_Python与硬件学习笔记:蜂鸣器(转)
  11. [转载]乔布斯十大经典语录
  12. LTE/LTE-Advanced 第2章 网络架构
  13. mysql 实例结构体_C语言结构体实例-创建兔子
  14. 在阿里云ECS服务器上使用NextCloud+Aria2+AriaNg部署下载站
  15. 编写1个JSP页面,在JSP页面中使用Java程序片输出26个小写的英文字母表。
  16. Python 之 Anaconda
  17. 【Python】 调用百度地图API抓取西安市小区信息
  18. 2019-9-17【Javase】object、final、实现关系、抽象类、接口、多态、UML类图
  19. 任正非一语中的,未来科技的发展核心靠他们!
  20. 能够破解网站禁止复制的插件:Enable Copy

热门文章

  1. arcgis 同名图层合并_arcgis合并多个图层
  2. CSS设置 background-image透明度小技巧
  3. 以太坊geth基本命令
  4. IO流的详解,彻底了解IO流
  5. 什么是JPA?SpringBoot 中使用JPA
  6. linux系统——管道
  7. MySQL索引原理理解
  8. 这位.NET开发者曾说:“GitHub 存在的意义,是帮助开发人员”,但还是要离职了...
  9. shiro的基本认识
  10. 一些需要用到的latex语句