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

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


2. 掃描設(shè)備
在構(gòu)造函數(shù)中執(zhí)行藍牙設(shè)備掃描,即軟件一啟動就執(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的搜索時間
????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)));
????//開始進行設(shè)備搜索
????discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
3. 將掃描結(jié)果添加到QListWidget中
//deviceDiscovered?signals?對應(yīng)的槽函數(shù)
void?Widget::addBlueToothDevicesToList(const?QBluetoothDeviceInfo?