Qt編程實(shí)例:基于Android的BLE通信軟件
[導(dǎo)讀]實(shí)現(xiàn)目標(biāo)自己編寫基于Qt的Android軟件,用于實(shí)現(xiàn)手機(jī)與TB-02-kit模塊進(jìn)行數(shù)據(jù)通訊;Android軟件發(fā)送的數(shù)據(jù),經(jīng)TB-02-kit模塊轉(zhuǎn)發(fā)至串口助手中輸出;串口助手發(fā)送的數(shù)據(jù)可以在Android軟件中顯示,進(jìn)而實(shí)現(xiàn)BLE的數(shù)據(jù)雙向通信。所需工具及環(huán)境TB-02-k...

實(shí)現(xiàn)目標(biāo)
- 自己編寫基于Qt的Android軟件,用于實(shí)現(xiàn)手機(jī)與TB-02-kit模塊進(jìn)行數(shù)據(jù)通訊;
- Android軟件發(fā)送的數(shù)據(jù),經(jīng)TB-02-kit模塊轉(zhuǎn)發(fā)至串口助手中輸出;
- 串口助手發(fā)送的數(shù)據(jù)可以在Android軟件中顯示,進(jìn)而實(shí)現(xiàn)BLE的數(shù)據(jù)雙向通信。
所需工具及環(huán)境
- TB-02-kit模塊
- Qt Creator 4.10.1
- Qt 5.13.1
- XCOM V2.0 串口助手
- Android 手機(jī)
- 本人電腦 Windows 10 64bit [版本 10.0.19041.329]
前置知識(shí)
給大家介紹一款好用的藍(lán)牙BT5.0透?jìng)髂KWindows下基于Qt開發(fā)Android應(yīng)用
BLE中這些概念你都了解嗎
本文源碼

具體實(shí)現(xiàn)
1. 要使用Qt藍(lán)牙模塊, 項(xiàng)目的 .pro文件中要添加聲明才可使用


2. 掃描設(shè)備
在構(gòu)造函數(shù)中執(zhí)行藍(lán)牙設(shè)備掃描,即軟件一啟動(dòng)就執(zhí)行掃描。Widget::Widget(QWidget?*parent)
????:?QWidget(parent)
????,?ui(new?Ui::Widget)
{
????ui->setupUi(this);
????//創(chuàng)建搜索服務(wù):https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.html
????discoveryAgent?=new?QBluetoothDeviceDiscoveryAgent(this);
????//設(shè)置BLE的搜索時(shí)間
????discoveryAgent->setLowEnergyDiscoveryTimeout(20000);
????connect(discoveryAgent,SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),this,SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo)));//找到設(shè)備之后添加到列表顯示出來
????connect(discoveryAgent,?SIGNAL(finished()),?this,?SLOT(scanFinished()));
????connect(discoveryAgent,?SIGNAL(canceled()),?this,?SLOT(scanCanceled()));
????connect(this,?SIGNAL(returnAddress(QBluetoothDeviceInfo)),?this,?SLOT(createCtl(QBluetoothDeviceInfo)));
????//開始進(jìn)行設(shè)備搜索
????discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
3. 將掃描結(jié)果添加到QListWidget中
//deviceDiscovered?signals?對(duì)應(yīng)的槽函數(shù)
void?Widget::addBlueToothDevicesToList(const?QBluetoothDeviceInfo? 




