QT藍牙開發


https://fsp1yjl.github.io/2017/07/29/2017Qt%E4%B8%AD%E4%BD%8E%E5%8A%9F%E8%80%97%E8%93%9D%E7%89%99%E6%A8%A1%E5%9D%97%E4%BD%BF%E7%94%A8/

1.要使用qt藍牙模塊, 項目的 .pro要聲明使用
2.有了lambda函數后,不用再一個個聲明和創建具名槽函數的感覺挺好,就是代碼成一坨了
3.雖然說是低功耗藍牙,但還是要記得用完后斷開和外設店連接,之前忘記斷開,一晚上一個紐扣電池的點就被耗光了
4.GATT Profile 簡介

  1. 搜尋附近全部的藍牙設備
  2. 根據搜尋出的藍牙設備信息,篩選出要連接的藍牙設備進行連接
  3. 建立連接后,去獲取該藍牙設備等services列表,根據約定好的服務uuid篩選出自己需要的服務
  4. 發現對應的服務后,根據約定好的服務下characteristic特性id,創建特征對象,並監聽特征對象內容的變化
  5. 向客戶端特征配置對象寫入特征生效消息,開始接收外圍藍牙設備。

bluedevice.h:

#ifndef BLUEDEVICE_H
#define BLUEDEVICE_H
#include <QObject>
#include<QBluetoothDeviceDiscoveryAgent>
#include<QBluetoothDeviceInfo>
#include<QBluetoothUuid>
#include<QBluetoothServiceInfo>
#include<QLowEnergyController>
#include<QLowEnergyService>
#include<QLowEnergyDescriptor>
class BlueDevice: public QObject{
    Q_OBJECT
public:

    BlueDevice();
    QString getAddress(QBluetoothDeviceInfo device) const;
private:
        QList<QBluetoothDeviceInfo> device_list;  //存放搜索到到藍牙設備列表
        QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent;  //設備搜索對象
        QLowEnergyController *m_controler;   //單個藍牙設備控制器
        QLowEnergyService *m_service; //服務對象實例
};

#endif // BLUEDEVICE_H

bluedevice.cpp:

#include "bluedevice.h"
#include <QDebug>
#include<QTimer>

BlueDevice::BlueDevice() {
    qDebug() << "enter bludedevice constructor....";

    m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
    m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000);

    //每次發現新設備時觸發
    connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this ,[this]() {
        qDebug() << "find a new bluebooth device";
    });
    
    //藍牙設備搜索完成后,篩選出目標設備進行連接,並進行相關信號與槽函數的綁定
    connect(m_deviceDiscoveryAgent,&QBluetoothDeviceDiscoveryAgent::finished, this, [this]() {
        device_list = this->m_deviceDiscoveryAgent->discoveredDevices();

        //遍歷顯示設備詳情
        QList<QBluetoothDeviceInfo>::iterator it;
        for(it=device_list.begin(); it != device_list.end(); it++) {
            
            // 外圍藍牙設備對象
            QBluetoothDeviceInfo tmp_device = *it;  
            QString device_name = tmp_device.name();
            //qDebug() <<"device name:::" << device_name;
            //qDebug() << "device address:::" << this->getAddress(tmp_device);
            
           //打印搜索出來的全部低功耗藍牙
           if (tmp_device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
                qDebug() << " low Energy device ....";
                qDebug() <<"22222device name:::" << device_name;
              }
            
            //正則匹配目標設備,
            QString pattern_str = "^Eric.*";  //qt中正則匹配任意個字符,需要使用.*而不是*
            QRegExp rx(pattern_str);
            if(!rx.exactMatch(device_name)) {
                continue;
            }
            qDebug() <<"device name:::" << device_name;
            qDebug() << "device address:::" << this->getAddress(tmp_device);

            // 創建藍牙設備控制器對象 
            m_controler = new QLowEnergyController(tmp_device, this);
            
            // 監聽目標設備連接成功消息,連接成功后,搜索目標設備等服務列表
            connect(m_controler, &QLowEnergyController::connected, this, [this](){
                qDebug() << "m_controler connected ......";
                //必須要在連接建立后 執行開始尋找service的函數 
                //之前調試,就是因為沒有在設備連接后主動請求獲取服務列表信息,后續監聽便沒有觸發
                m_controler->discoverServices();
            });
            
            // 監聽發現服務消息,如果服務的uuid 為約定好的要使用服務類型,則進行后續處理
            connect(m_controler,&QLowEnergyController::serviceDiscovered, this, [this](QBluetoothUuid serviceUuid) {
                if(serviceUuid == QBluetoothUuid( quint16(0xffd0))) {  //我們用的服務類型是0xffd0對應的uuid
                    
                    //發現匹配的服務后,使用控制器對象創建服務對象
                    m_service = m_controler->createServiceObject(serviceUuid,this);
                    if(m_service) {

                        // 服務對象創建成功后,堅挺服務狀態變化,如果狀態變成已發現,則進行后續服務下特征對象獲取
                        connect(m_service,&QLowEnergyService::stateChanged, this, [this]() {
                            qDebug() << "service state change" << m_service->state() << ",||||||";

                            //發現服務, 建立characteristic對象實例
                            if(m_service->state() == QLowEnergyService::ServiceDiscovered) {
                                QLowEnergyCharacteristic hrChar = m_service->characteristic(QBluetoothUuid(quint16(0xfff6)));
                                if(!hrChar.isValid()) {
                                    qDebug() << "characteristic fff6 error:::";
                                }
                                // 設置特征對象可用
                                //enable the chracteristic notification by write 0x01 to client characteristic configuration
                                QLowEnergyDescriptor m_notificationDesc = hrChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
                                if (m_notificationDesc.isValid()) {
                                    if(hrChar.properties() & QLowEnergyCharacteristic::Notify) {
                                        qDebug() << "";
                                    }
                                    m_service->writeDescriptor(m_notificationDesc, QByteArray::fromHex("0100"));
                                }
                            }
                        });

                        // 通過監聽特征對象的變化,不斷獲得鞋墊壓力數據。 
                        connect(m_service,&QLowEnergyService::characteristicChanged, this,
                                [this](QLowEnergyCharacteristic c,QByteArray value) {
                                    qDebug() << "characteristicChanged state change::" <<c.uuid()<< ",||||||";
                                    qDebug() << "value length::" << value.length();
                                    qDebug() << "value length::" << value;

                                    QByteArray sub_1 = value.mid(0,2);
                                    QByteArray sub_2 = value.mid(2,2);
                                    QByteArray sub_3 = value.mid(4,2);

                                    bool ok;
                                    // num 1,2,3  分別對應三個壓力感應點的壓力值
                                    int num_1 =  QString(sub_1.toHex()).toInt(&ok,16);
                                    qDebug() << "num_1:::" << num_1;
                                    int num_2 =  QString(sub_2.toHex()).toInt(&ok,16);
                                    qDebug() << "num_1:::" << num_2;
                                    int num_3 =  QString(sub_3.toHex()).toInt(&ok,16);
                                    qDebug() << "num_1:::" << num_3;

                                }
                        );
                        
                        // 觸發服務詳情發現函數 ,不要忘記調用
                        m_service->discoverDetails();
                    }
                }
            });

            connect(m_controler,&QLowEnergyController::discoveryFinished, this, [this]() {
                qDebug() << "finish service discovery";
            });

            //連接外圍設備
            m_controler->connectToDevice();

            //QTimer *timer = new QTimer(this);
            //connect(timer, &QTimer::timeout, this, [this](){ qDebug() <<"state:::" <<  this->m_controler->state();});
            //timer->start(1000);
        }
    });
   
    // 開始外圍設備搜索
    m_deviceDiscoveryAgent->start();

}

// mac和其他系統上address獲取有少許差異,參見官方文檔
QString BlueDevice::getAddress(QBluetoothDeviceInfo device) const
{
#ifdef Q_OS_MAC
    // On OS X and iOS we do not have addresses,
    // only unique UUIDs generated by Core Bluetooth.
    return device.deviceUuid().toString();
#else
    return device.address().toString();
#endif
}

//void BlueDevice::getNewService(QBluetoothServiceInfo info) {
//    qDebug() << "ppppp uuuuuuuuuu";
//}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM