1 Star 0 Fork 2

Leonard/TelinkOTA

forked from nabian/TelinkOTA 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
qgpioble.cpp 11.85 KB
一键复制 编辑 原始数据 按行查看 历史
nabian 提交于 2021-07-11 09:38 . 修改UI界面
#include <QtCore>
#include <qbluetoothaddress.h>
#include <qbluetoothdevicediscoveryagent.h>
#include <qbluetoothlocaldevice.h>
#include <QDebug>
#include <QSettings>
#include <QMutex>
#include "qgpioble.h"
QGPIOBle::QGPIOBle(QObject *parent) : QObject(parent)
{
localDevice = new QBluetoothLocalDevice;
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
discoveryAgent->setInquiryType(QBluetoothDeviceDiscoveryAgent::GeneralUnlimitedInquiry);
qDebug() << discoveryAgent->LimitedInquiry;
qDebug() << discoveryAgent->supportedDiscoveryMethods();
connect(discoveryAgent, SIGNAL(deviceDiscovered(QBluetoothDeviceInfo)),
this, SLOT(FindDevice_cb(QBluetoothDeviceInfo)));
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished_cb()));
connect(discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)), this, SLOT(deviceScanError_cb(QBluetoothDeviceDiscoveryAgent::Error)));
}
QGPIOBle::~QGPIOBle()
{
// qDeleteAll(Devicelist);
//Devicelist.clear();
//delete localDevice;
//delete discoveryAgent;
if (controller)
{
controller->disconnect();
controller->disconnectFromDevice();
}
// delete m_service;
//m_service = nullptr;
}
/************************************************************************************
* 函数名称: startScan()
* 输入参数:
* 返回值 : 无
* 功 能: 开始扫描,只扫描低功耗节点
**************************************************************************************/
void QGPIOBle::startScan()
{
qDeleteAll(Devicelist);
Devicelist.clear();
qDebug() << "scan start";
discoveryAgent->setLowEnergyDiscoveryTimeout(3000);
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod); // //QBluetoothDeviceDiscoveryAgent::LowEnergyMethod
}
/************************************************************************************
* 函数名称: startScan()
* 输入参数:
* 返回值 : 无
* 功 能: 开始扫描
**************************************************************************************/
void QGPIOBle::deviceScanError_cb(QBluetoothDeviceDiscoveryAgent::Error error)
{
qDebug() << error;
}
/************************************************************************************
* 函数名称: scanFinished_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 扫描结束回调函数
**************************************************************************************/
void QGPIOBle::scanFinished_cb()
{
qDebug() << "scan finish";
const QList<QBluetoothDeviceInfo> foundDevices = discoveryAgent->discoveredDevices();
for (auto nextDevice : foundDevices)
{
QBluetoothDeviceInfo *dev = new QBluetoothDeviceInfo();
*dev = nextDevice;
Devicelist.append(dev);
}
emit emit_scanfinish();
//ui->textEdit->append("scan finish");
}
/************************************************************************************
* 函数名称: FindDevice_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 扫描到设备回调
**************************************************************************************/
void QGPIOBle::FindDevice_cb(const QBluetoothDeviceInfo &info)
{
QString label = QString("finded %1 %2").arg(info.address().toString()).arg(info.name()) + QString::number(info.manufacturerData().count());
qDebug() << label << info.rssi()<<info.manufacturerIds().length();
}
void QGPIOBle::connectDevice()
{
qDeleteAll(m_characteristics);
m_characteristics.clear();
qDeleteAll(m_services);
m_services.clear();
if (!controller)
{
// Connecting signals and slots for connecting to LE services.
controller = QLowEnergyController::createCentral(currentDevice);
connect(controller, &QLowEnergyController::connected,
this, &QGPIOBle::deviceConnected_cb);
connect(controller, QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error),
this, &QGPIOBle::ControllerError_cb);
connect(controller, &QLowEnergyController::disconnected,
this, &QGPIOBle::deviceDisconnected_cb);
connect(controller, &QLowEnergyController::serviceDiscovered,
this, &QGPIOBle::addLowEnergyService);
connect(controller, &QLowEnergyController::discoveryFinished,
this, &QGPIOBle::serviceScanDone_cb);
}
controller->connectToDevice();
}
/************************************************************************************
* 函数名称: ControllerError_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 设备错误回调
*************************************************************************************/
void QGPIOBle::ControllerError_cb(QLowEnergyController::Error)
{
qWarning() << "ControllerError_cb: " << controller->errorString();
}
/************************************************************************************
* 函数名称: deviceDisconnected_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 设备错误回调
*************************************************************************************/
void QGPIOBle::deviceDisconnected_cb()
{
connected = false;
qWarning() << "Disconnect from device";
delete controller;
controller = nullptr;
disconnect_service(m_service);
delete m_service;
m_service = nullptr;
}
/************************************************************************************
* 函数名称: deviceConnected_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 设备连接回调
**************************************************************************************/
void QGPIOBle::deviceConnected_cb()
{
qWarning() << "deviceConnected_cb";
connected = true;
controller->discoverServices();
}
/************************************************************************************
* 函数名称: addLowEnergyService()
* 输入参数:
* 返回值 : 无
* 功 能: 发现服务回调
**************************************************************************************/
void QGPIOBle::addLowEnergyService(const QBluetoothUuid &serviceUuid)
{
QLowEnergyService *service = controller->createServiceObject(serviceUuid);
if (!service)
{
qWarning() << "Cannot create service for uuid";
return;
}
qDebug() << "QGPIOBle" << serviceUuid;
}
/************************************************************************************
* 函数名称: getservice()
* 输入参数:
* 返回值 : 无
* 功 能: 连接指定uuid服务
**************************************************************************************/
void QGPIOBle::connect_service(const QString Uuid)
{
qWarning() << "QGPIOBle"
<< "serviceScanDone_cb";
m_service = controller->createServiceObject(QBluetoothUuid(Uuid), this);
if (m_service)
{
connect(m_service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)), this, SLOT(serviceStateChanged_cb(QLowEnergyService::ServiceState)));
connect(m_service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)),
this, SLOT(CharaterNotify_cb(QLowEnergyCharacteristic, QByteArray)));
connect(m_service, SIGNAL(error(QLowEnergyService::ServiceError)),
this, SLOT(serviceStateErr_cb(QLowEnergyService::ServiceError)));
connect(m_service, SIGNAL(characteristicWritten(const QLowEnergyCharacteristic, const QByteArray)), this, SLOT(confirmedcharacteristicWrite(const QLowEnergyCharacteristic, const QByteArray)));
m_service->discoverDetails();
}
else
{
qWarning() << "Heart Rate Service not found.";
}
}
void QGPIOBle::disconnect_service(QLowEnergyService *service)
{
if (service)
{
disconnect(m_service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)), this, SLOT(serviceStateChanged_cb(QLowEnergyService::ServiceState)));
disconnect(m_service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)),
this, SLOT(CharaterNotify_cb(QLowEnergyCharacteristic, QByteArray)));
disconnect(m_service, SIGNAL(error(QLowEnergyService::ServiceError)),
this, SLOT(serviceStateErr_cb(QLowEnergyService::ServiceError)));
disconnect(m_service, SIGNAL(characteristicWritten(const QLowEnergyCharacteristic, const QByteArray)), this, SLOT(confirmedcharacteristicWrite(const QLowEnergyCharacteristic, const QByteArray)));
m_service->destroyed();
}
}
/************************************************************************************
* 函数名称: serviceScanDone_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 服务状态改变回调
**************************************************************************************/
void QGPIOBle::serviceScanDone_cb()
{
connect_service(QString("{00010203-0405-0607-0809-0a0b0c0d1912}"));
}
/************************************************************************************
* 函数名称: serviceStateChanged_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 服务状态改变回调
**************************************************************************************/
void QGPIOBle::serviceStateChanged_cb(QLowEnergyService::ServiceState s)
{
qWarning() << "QGPIOBle"
<< "serviceStateChanged_cb";
switch (s)
{
case QLowEnergyService::DiscoveringServices:
qWarning() << "QGPIOBle" << (("Discovering services..."));
break;
case QLowEnergyService::ServiceDiscovered:
{
qWarning() << "QGPIOBle" << (("Service discovered."));
const QList<QLowEnergyCharacteristic> chars = m_service->characteristics();
for (const QLowEnergyCharacteristic &ch : chars)
{
qDebug() << "QGPIOBle" << ch.uuid();
}
OtaChar = m_service->characteristic(QBluetoothUuid(QBluetoothUuid(QString("{00010203-0405-0607-0809-0a0b0c0d2b12}"))));
qDebug() << "QGPIOBle" << OtaChar.properties();
break;
}
default:
//nothing for now
break;
}
}
/************************************************************************************
* 函数名称: CharaterNotify_cb()
* 输入参数:
* 返回值 : 无
* 功 能: Notify回调函数
*************************************************************************************/
void QGPIOBle::CharaterNotify_cb(const QLowEnergyCharacteristic &c, const QByteArray &value)
{
qDebug() << c.uuid();
qDebug() << value.toHex();
emit emit_confirmedcharacteristicWrite(c.uuid().toString(), value);
}
/************************************************************************************
* 函数名称: confirmedcharacteristicWrite()
* 输入参数:
* 返回值 : 无
* 功 能: 连接设备
**************************************************************************************/
void QGPIOBle::confirmedcharacteristicWrite(const QLowEnergyCharacteristic &d, const QByteArray &value)
{
qWarning() << "QGPIOBle"
<< d.uuid().toString();
emit emit_confirmedcharacteristicWrite(d.uuid().toString(), value);
}
bool QGPIOBle::writeCharacteristics(QLowEnergyService *ser,QLowEnergyCharacteristic &c, QByteArray &sendbuffer, bool rsp)
{
ser->writeCharacteristic(c, sendbuffer, (QLowEnergyService::WriteWithoutResponse));
}
/************************************************************************************
* 函数名称: serviceStateErr_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 服务异常回调
**************************************************************************************/
void QGPIOBle::serviceStateErr_cb(QLowEnergyService::ServiceError newError)
{
qWarning() << "QGPIOBle"
<< "serviceStateErr_cb";
qWarning() << newError;
if (newError == QLowEnergyService::DescriptorWriteError)
{
connected = false;
qDebug() << "serviceStateErr_cb" << OtaChar.properties();
delete m_service;
m_service = nullptr;
if (controller)
controller->disconnectFromDevice();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/leonard33/TelinkOTA.git
git@gitee.com:leonard33/TelinkOTA.git
leonard33
TelinkOTA
TelinkOTA
master

搜索帮助