#include "device.h"
#include <qbluetoothaddress.h>
#include <qbluetoothdevicediscoveryagent.h>
#include <qbluetoothlocaldevice.h>
#include <qbluetoothdeviceinfo.h>
#include <qbluetoothservicediscoveryagent.h>
#include <QDebug>
#include <QList>
#include <QString>
#include "service.h"
#include <QLowEnergyService>
{
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)), this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
}
Device::~Device() {
delete discoveryAgent;
delete m_control;
qDeleteAll(devices);
qDeleteAll(m_services);
qDeleteAll(m_characteristics);
devices.clear();
m_services.clear();
m_characteristics.clear();
}
void Device::addDevice(const QBluetoothDeviceInfo &info) {
if(info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
DeviceInfo *d = new DeviceInfo(info);
devices.append(d);
qWarning() << "Discovered BLE Device " + d->getName() << "Address: " + d->getAddress();
if(d->getName() == "EX BLE") {
}
}
}
void Device::startDeviceDiscovery() {
qDeleteAll(devices);
devices.clear();
discoveryAgent->start();
qWarning() << "Starting to Scan.";
//address(QString("Starting to Scan."));
}
void Device
::connectDevice(const QString &address
) { for(int i = 0; i < devices.size(); i++) {
if(((DeviceInfo*)devices.at(i)) -> getAddress() == address) {
m_currentDevice.setDevice(((DeviceInfo*)devices.at(i))->getDevice());
qWarning() << "Connecting to Device.";
break;
}
}
qDeleteAll(m_characteristics);
m_characteristics.clear();
qDeleteAll(m_services);
m_services.clear();
m_control = new QLowEnergyController(m_currentDevice.getDevice(), this);
connect(m_control, SIGNAL(serviceDiscovered(QBluetoothUuid)), this, SLOT(addLowEnergyService(QBluetoothUuid)));
connect(m_control, SIGNAL(discoveryFinished()), this, SLOT(serviceScanDone()));
connect(m_control, SIGNAL(error(QLowEnergyController::Error)), this, SLOT(controllerError(QLowEnergyController::Error)));
connect(m_control, SIGNAL(connected()), this, SLOT(deviceConnected()));
connect(m_control, SIGNAL(disconnected()), this, SLOT(deviceDisconnected()));
m_control->connectToDevice();
}
void Device::deviceConnected() {
m_control->discoverServices();
}
void Device::deviceDisconnected() {
qWarning() << "Remote Device Disconnected.";
}
void Device::controllerError(QLowEnergyController::Error error) {
qWarning() << "Controller Error: " << error << "Cannot connect to remote device.";
serviceUuid
(QString("Controller Error."));
}
}
void Device::serviceScanDone() {
qWarning() << "Service Scan Done.";
}
void Device::addLowEnergyService(const QBluetoothUuid &gatt) {
QLowEnergyService *service = m_control->createServiceObject(gatt);
if(!service) {
qWarning() << "Cannot Create Service for UUID." ;
serviceUuid
(QString("Cannot Create Service For UUID."));
return;
}
Service *serv = new Service(service);
m_services.append(serv);
qWarning() << "Service Found: " + serv->getName() << "UUID: " << serv->getUuid();
if(serv->getUuid() == "0xb73a") {
serviceUuid
(QString(serv
->getUuid
()));
qWarning() << "Service Found";
}
}
void Device::disconFromDevice() {
if(m_control->state() != QLowEnergyController::UnconnectedState)
m_control->disconnectFromDevice();
else
deviceDisconnected();
}
void Device
::connectToService(const QString &uuid
) {
QLowEnergyService *service = 0;
for(int i = 0; i<m_services.size(); i++) {
Service *serviceInfo = (Service*)m_services.at(i);
if(serviceInfo->getUuid() == uuid) {
service = serviceInfo->service();
break;
}
}
if(!service)
return;
qDeleteAll(m_characteristics);
m_characteristics.clear();
if(service->state() == QLowEnergyService::DiscoveryRequired) {
service->discoverDetails();
qWarning() << "Discovering Service Details";
serviceUuid
(QString("Service Details Discovered"));
return;
}
const QList<QLowEnergyCharacteristic> chars = service->characteristics();
foreach(const QLowEnergyCharacteristic &ch, chars) {
Characteristics *cInfo = new Characteristics(ch);
m_characteristics.append(cInfo);
//readData(QString(cInfo->getValue()));
}
const QLowEnergyCharacteristic link = service->characteristic(QBluetoothUuid::mine);
if(service->state() == QLowEnergyService::ServiceDiscovered) {
service
->QLowEnergyService
::writeCharacteristic(link,
QByteArray::fromHex("03"),QLowEnergyService
::WriteWithResponse);
}
}
Bookmarks