#include "service.h"
Service::Service(QObject *parent) : QObject(parent)
{
m_service = NULL;
}
void Service::ConnectService(QLowEnergyService * service)
{
m_service = service;
if(m_service)
{
if(m_service->state() == QLowEnergyService::ServiceDiscovered)
{
onStateChanged(QLowEnergyService::ServiceDiscovered);
}
else
{
connect(m_service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)), this, SLOT(onStateChanged(QLowEnergyService::ServiceState)));
connect(m_service, SIGNAL(characteristicChanged(QLowEnergyCharacteristic, QByteArray)), this, SLOT(onCharacteristicChanged(QLowEnergyCharacteristic, QByteArray)));
connect(m_service, SIGNAL(characteristicRead(QLowEnergyCharacteristic, QByteArray)), this, SLOT(onCharacteristicRead(QLowEnergyCharacteristic, QByteArray)));
connect(m_service, SIGNAL(characteristicWritten(QLowEnergyCharacteristic, QByteArray)), this, SLOT(onCharacteristicWritten(QLowEnergyCharacteristic, QByteArray)));
connect(m_service, SIGNAL(descriptorRead(QLowEnergyCharacteristic, QByteArray)), this, SLOT(onDescriptorRead(QLowEnergyCharacteristic, QByteArray)));
connect(m_service, SIGNAL(descriptorWritten(QLowEnergyCharacteristic, QByteArray)), this, SLOT(onDescriptorWritten(QLowEnergyCharacteristic, QByteArray)));
connect(m_service, SIGNAL(error(QLowEnergyService::ServiceError)), this, SLOT(onError(QLowEnergyService::ServiceError)));
m_service->discoverDetails();
}
}
}
void Service::SendMessage(QString msg)
{
emit message(msg);
}
void Service::OpenNotify(QLowEnergyCharacteristic ch, bool flag)
{
if(m_service)
{
if(ch.isValid())
{
if(ch.properties() & QLowEnergyCharacteristic::Notify)
{
QLowEnergyDescriptor d = ch.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
if(d.isValid())
{
if(true == flag)
{
m_service->writeDescriptor(d, QByteArray::fromHex("0100"));
}
else
{
m_service->writeDescriptor(d, QByteArray::fromHex("0000"));
}
}
}
}
}
}
void Service::ReadCharacteristic(QLowEnergyCharacteristic ch)
{
if(m_service)
{
if(ch.isValid())
{
if(ch.properties() & QLowEnergyCharacteristic::Read)
{
m_service->readCharacteristic(ch);
}
}
}
}
void Service::WriteCharacteristic(QLowEnergyCharacteristic ch, QByteArray arr)
{
if(m_service)
{
if(ch.isValid())
{
if(ch.properties() & QLowEnergyCharacteristic::Write)
{
m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteWithResponse);
}
else if(ch.properties() & QLowEnergyCharacteristic::WriteNoResponse)
{
m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteWithoutResponse);
}
else if(ch.properties() & QLowEnergyCharacteristic::WriteSigned)
{
m_service->writeCharacteristic(ch, arr, QLowEnergyService::WriteSigned);
}
}
}
}
void Service::onStateChanged(QLowEnergyService::ServiceState newState)
{
if(m_service)
{
switch(newState)
{
case QLowEnergyService::DiscoveringServices:
{
SendMessage("Discovering services...");
}
break;
case QLowEnergyService::DiscoveryRequired:
{
SendMessage("require discover services...");
}
break;
case QLowEnergyService::ServiceDiscovered:
{
SendMessage("Discovered services");
QList<QLowEnergyCharacteristic> characteristics = m_service->characteristics();
foreach(QLowEnergyCharacteristic ch, characteristics)
{
emit discoveryCharacteristic(ch);
}
}
break;
default:
break;
}
}
}
void Service::onCharacteristicChanged(const QLowEnergyCharacteristic &info, const QByteArray &value)
{
QString ch = info.uuid().toString() + " - Characteristic Changed:" + QString(value);
SendMessage(ch);
}
void Service::onCharacteristicRead(const QLowEnergyCharacteristic &info, const QByteArray &value)
{
QString ch = info.uuid().toString() + " - Characteristic read:" + QString(value);
SendMessage(ch);
}
void Service::onCharacteristicWritten(const QLowEnergyCharacteristic &info, const QByteArray &value)
{
QString ch = info.uuid().toString() + " - Characteristic written:" + QString(value);
SendMessage(ch);
}
void Service::onDescriptorRead(const QLowEnergyDescriptor &info, const QByteArray &value)
{
QString ch = info.uuid().toString() + " - descriptor read:" + QString(value);
SendMessage(ch);
}
void Service::onDescriptorWritten(const QLowEnergyDescriptor &info, const QByteArray &value)
{
QString ch = info.uuid().toString() + " - descriptor written:" + QString(value);
SendMessage(ch);
}
void Service::onError(QLowEnergyService::ServiceError error)
{
const QString ServiceError[] {
"NoError",
"OperationError",
"CharacteristicWriteError",
"DescriptorWriteError",
"UnknownError",
"CharacteristicReadError",
"DescriptorReadError"
};
if(error < ServiceError->size())
{
QString str;
str = QString("service Error(%1):").arg(error);
str += ServiceError[error];
SendMessage(str);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
QtBLE.rar (12个子文件)
QtBLE
QtBLE.pro.user 22KB
QtBLE.pro 835B
QtBluetooth
controller.cpp 3KB
service.h 1KB
controller.h 991B
agent.h 683B
service.cpp 6KB
agent.cpp 1KB
main.cpp 293B
mainwindow.ui 8KB
mainwindow.cpp 4KB
mainwindow.h 1KB
共 12 条
- 1
makin.liu
- 粉丝: 2174
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论4