代码拉取完成,页面将自动刷新
同步操作将从 nabian/TelinkOTA 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#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();
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。