自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Qt編程實(shí)例:基于Android的BLE通信軟件

移動(dòng)開(kāi)發(fā) Android
自己編寫基于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ù)雙向通信。

[[374870]]

 實(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]

本文源碼

因?yàn)槭堑谝淮畏窒鞶t代碼,為了方便大家學(xué)習(xí),代碼中添加了大量注釋,大家對(duì)照著代碼學(xué)習(xí)效率高點(diǎn)。

后臺(tái)回復(fù)關(guān)鍵字“Android-BLE”,獲取本文涉及到的軟件及Qt工程源碼。

具體實(shí)現(xiàn)

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

2. 掃描設(shè)備

在構(gòu)造函數(shù)中執(zhí)行藍(lán)牙設(shè)備掃描,即軟件一啟動(dòng)就執(zhí)行掃描。

  1. Widget::Widget(QWidget *parent) 
  2.     : QWidget(parent) 
  3.     , ui(new Ui::Widget) 
  4.     ui->setupUi(this); 
  5.  
  6.     //創(chuàng)建搜索服務(wù):https://doc.qt.io/qt-5/qbluetoothdevicediscoveryagent.html 
  7.     discoveryAgent =new QBluetoothDeviceDiscoveryAgent(this); 
  8.     //設(shè)置BLE的搜索時(shí)間 
  9.     discoveryAgent->setLowEnergyDiscoveryTimeout(20000); 
  10.     connect(discoveryAgent,SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),this,SLOT(addBlueToothDevicesToList(QBluetoothDeviceInfo)));//找到設(shè)備之后添加到列表顯示出來(lái) 
  11.     connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished())); 
  12.     connect(discoveryAgent, SIGNAL(canceled()), this, SLOT(scanCanceled())); 
  13.     connect(this, SIGNAL(returnAddress(QBluetoothDeviceInfo)), this, SLOT(createCtl(QBluetoothDeviceInfo))); 
  14.  
  15.     //開(kāi)始進(jìn)行設(shè)備搜索 
  16.     discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); 

3. 將掃描結(jié)果添加到QListWidget中

  1. //deviceDiscovered signals 對(duì)應(yīng)的槽函數(shù) 
  2. void Widget::addBlueToothDevicesToList(const QBluetoothDeviceInfo &info) 
  3.     if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration)           //獲取設(shè)備信息,并判斷該設(shè)備是否為BLE設(shè)備 
  4.     { 
  5.         //格式化設(shè)備地址和設(shè)備名稱 
  6.         QString label = QString("%1 %2").arg(info.address().toString()).arg(info.name()); 
  7.         //檢查設(shè)備是否已存在,避免重復(fù)添加 
  8.         QList<QListWidgetItem *> items = ui->ctrBleList->findItems(label, Qt::MatchExactly); 
  9.  
  10.         //不存在則添加至設(shè)備列表 
  11.         if (items.empty()) 
  12.         { 
  13.             QListWidgetItem *item = new QListWidgetItem(label); 
  14.             ui->ctrBleList->addItem(item); 
  15.             devicesList.append(info); 
  16.         } 
  17.     } 

4. 連接藍(lán)牙,停止掃描

  1. void Widget::on_btnConnectBle_clicked() 
  2.     //確認(rèn)選取了某一個(gè)藍(lán)牙設(shè)備 
  3.     if(!ui->ctrBleList->currentItem()->text().isEmpty()) 
  4.     { 
  5.         //獲取選擇的地址 
  6.         QString bltAddress = ui->ctrBleList->currentItem()->text().left(17); 
  7.  
  8.         for (int i = 0; i<devicesList.count(); i++) 
  9.         { 
  10.             //地址對(duì)比 
  11.             if(devicesList.at(i).address().toString().left(17) == bltAddress) 
  12.             { 
  13.                 QBluetoothDeviceInfo choosenDevice = devicesList.at(i); 
  14.                 //發(fā)送自定義signals==>執(zhí)行slots:createCtl 
  15.                 emit returnAddress(choosenDevice); 
  16.                 //停止搜索服務(wù) 
  17.                 discoveryAgent->stop(); 
  18.                 break; 
  19.             } 
  20.         } 
  21.     } 

5. 獲取特征

  1. void Widget::searchCharacteristic() 
  2.     if(m_bleServer) 
  3.     { 
  4.         QList<QLowEnergyCharacteristic> list=m_bleServer->characteristics(); 
  5.         qDebug()<<"[xiaohage]list.count()="<<list.count(); 
  6.         //遍歷characteristics 
  7.         for(int i=0;i<list.count();i++) 
  8.         { 
  9.             QLowEnergyCharacteristic c=list.at(i); 
  10.             /*如果QLowEnergyCharacteristic對(duì)象有效,則返回true,否則返回false*/ 
  11.             if(c.isValid()) 
  12.             { 
  13.                 //返回特征的屬性。 
  14.                 //這些屬性定義了特征的訪問(wèn)權(quán)限。 
  15.                 if(c.properties() & QLowEnergyCharacteristic::WriteNoResponse || c.properties() & QLowEnergyCharacteristic::Write) 
  16.                 { 
  17.                     ui->ctrSystemLogInfo->insertPlainText("\n具有寫權(quán)限!"); 
  18.                     m_writeCharacteristic = c;  //保存寫權(quán)限特性 
  19.                     if(c.properties() & QLowEnergyCharacteristic::WriteNoResponse) 
  20.                     { 
  21.                         m_writeMode = QLowEnergyService::WriteWithoutResponse; 
  22.                     } 
  23.                     else 
  24.                     { 
  25.                         m_writeMode = QLowEnergyService::WriteWithResponse; 
  26.                     } 
  27.                 } 
  28.  
  29.                 if(c.properties() & QLowEnergyCharacteristic::Read
  30.                 { 
  31.                     m_readCharacteristic = c; //保存讀權(quán)限特性 
  32.                 } 
  33.  
  34.                 //描述符定義特征如何由特定客戶端配置。 
  35.                 m_notificationDesc = c.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration); 
  36.                 //值為真 
  37.                 if(m_notificationDesc.isValid()) 
  38.                 { 
  39.                     //寫描述符 
  40.                     m_bleServer->writeDescriptor(m_notificationDesc, QByteArray::fromHex("0100")); 
  41.                     ui->ctrSystemLogInfo->insertPlainText("\n寫描述符!"); 
  42.                 } 
  43.             } 
  44.         } 
  45.     } 

6. 發(fā)送數(shù)據(jù)

writeCharacteristic()方法,發(fā)送數(shù)據(jù)給ble設(shè)備。

點(diǎn)擊界面中的"發(fā)送"按鈕,發(fā)送"Hello World"字符串。

  1. void Widget::SendMsg(QString text) 
  2.     QByteArray array=text.toLocal8Bit(); 
  3.  
  4.     m_bleServer->writeCharacteristic(m_writeCharacteristic,array, m_writeMode); 
  5.  
  6. void Widget::on_btnSendData_clicked() 
  7.     SendMsg("Hello World"); 

7. 寫入數(shù)據(jù)

通過(guò)藍(lán)牙QLowEnergyService::characteristicRead的回調(diào)接口,接收藍(lán)牙收到的消息。

  1. void Widget::BleServiceCharacteristicRead(const QLowEnergyCharacteristic &c,const QByteArray &value) 
  2.     Q_UNUSED(c) 
  3.  
  4.     ui->ctrSystemLogInfo->insertPlainText("\n當(dāng)特征讀取請(qǐng)求成功返回其值時(shí):"); 
  5.     ui->ctrSystemLogInfo->insertPlainText(QString(value)); 

8. 斷開(kāi)連接

  1. Widget::~Widget() 
  2.     if(!(m_BLEController->state() == QLowEnergyController::UnconnectedState)) 
  3.             m_BLEController->disconnectFromDevice();//從設(shè)備斷開(kāi)鏈接 
  4.  
  5.     delete ui; 

界面布局

結(jié)果展示

如果出現(xiàn)" Cannot connect to remote device. " ,可以點(diǎn)擊"連接"按鈕重新連接一下。

串口助手及應(yīng)用程序輸出

To do

本實(shí)例只是演示一下Android手機(jī)與TB-02-kit模塊的通訊過(guò)程,程序里有需要完善的地方,比如,應(yīng)該增加一個(gè)"掃描"按鈕,而不是軟件啟動(dòng)過(guò)程中直接進(jìn)行藍(lán)牙掃描,這樣的話,就需要藍(lán)牙的上電要在軟件啟動(dòng)之前完成。

程序的健壯性也要完善,比如偶爾會(huì)出現(xiàn)與模塊無(wú)法正常連接的情況,需要再次點(diǎn)擊"連接"按鈕才可,這些工作你們自己可以完善一下哈。

有了本部分知識(shí),下一步我們結(jié)合Android手機(jī)和TB-02-kit模塊,實(shí)現(xiàn)STM32的設(shè)備的遠(yuǎn)程控制。

Qt小知識(shí)

1. Qt Creator程序輸出窗口過(guò)濾調(diào)試信息

2. 為Button添加事件

Button控件右鍵菜單中選中“轉(zhuǎn)到槽...”,然后在彈出列表中選中信號(hào):“clicked() ”,然后點(diǎn)擊OK按鈕,即可進(jìn)入其事件函數(shù)中。

參考資料

Qt官方文檔:https://doc.qt.io/qt-5/classes.html

本文轉(zhuǎn)載自微信公眾號(hào)「嵌入式從0到1」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系嵌入式從0到1公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: 嵌入式從0到1
相關(guān)推薦

2011-06-13 16:51:19

Qt Socket

2015-02-27 16:03:26

Android源碼Bluetooth_4BLE藍(lán)牙通信

2011-06-22 17:49:35

Linux Qt 串口

2011-06-27 11:08:37

Qt 串口 通信

2014-08-26 11:46:46

QtAndroid實(shí)例教程

2022-01-25 16:54:14

BLE操作系統(tǒng)鴻蒙

2011-06-22 16:18:23

QT 多線程 QSocket

2011-06-21 14:12:14

Qt Linux 登錄界面

2014-08-11 16:41:05

Android傳感器

2011-06-30 17:51:17

QT 元類型 線程

2011-06-08 14:24:20

JVM Qt QtJambi

2015-07-15 14:27:04

Eddystone通信蘋果

2011-07-25 13:16:23

無(wú)線局域網(wǎng)擴(kuò)頻通信

2011-07-05 14:46:34

2011-06-22 10:12:08

Qt 線程

2017-05-04 15:10:01

漏洞IoT智能燈泡BLE

2011-06-27 16:07:49

Qt Designer

2011-06-21 09:33:49

Qt 啟動(dòng) 界面

2011-06-14 16:45:57

Qt 圖標(biāo)

2011-06-24 14:34:17

Qt 小票 打印
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)