summaryrefslogtreecommitdiffstats
path: root/examples/bluetooth/lowenergyscanner/device.cpp
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@digia.com>2014-07-03 16:18:03 +0200
committerAlex Blasche <alexander.blasche@digia.com>2014-07-15 20:39:40 +0200
commitc39189a922ce7057b410eba12e80abac589712cf (patch)
tree338815352496d1b1e2d0234f8fd81488e6dc84c5 /examples/bluetooth/lowenergyscanner/device.cpp
parent4a52ec355e2b2541d1867f785ef58c8c483b7316 (diff)
Port lowenergyscanner example to new API
Some minor improvements have been incorporated too but the focus is on the port. More code and UI cleanups are required. Change-Id: I56f226b8418b78190d498df04d67686220a3644e Reviewed-by: Fabian Bumberger <fbumberger@rim.com>
Diffstat (limited to 'examples/bluetooth/lowenergyscanner/device.cpp')
-rw-r--r--examples/bluetooth/lowenergyscanner/device.cpp172
1 files changed, 106 insertions, 66 deletions
diff --git a/examples/bluetooth/lowenergyscanner/device.cpp b/examples/bluetooth/lowenergyscanner/device.cpp
index 8f899354..58454185 100644
--- a/examples/bluetooth/lowenergyscanner/device.cpp
+++ b/examples/bluetooth/lowenergyscanner/device.cpp
@@ -1,6 +1,7 @@
/***************************************************************************
**
** Copyright (C) 2013 BlackBerry Limited. All rights reserved.
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtBluetooth module of the Qt Toolkit.
@@ -44,29 +45,18 @@
#include <qbluetoothlocaldevice.h>
#include <qbluetoothdeviceinfo.h>
#include <qbluetoothservicediscoveryagent.h>
-#include <qbluetoothserviceinfo.h>
-#include <qlowenergycharacteristicinfo.h>
-#include <qlowenergycontroller.h>
#include <QDebug>
#include <QList>
Device::Device():
- connected(false), info(0), m_deviceScanState(false)
+ connected(false), controller(0), m_deviceScanState(false)
{
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
connect(discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
- connect(discoveryAgent, SIGNAL(finished()), this, SLOT(scanFinished()));
- serviceDiscoveryAgent = new QBluetoothServiceDiscoveryAgent(QBluetoothAddress());
-
- //Connecting signals and slots for service discovery
- connect(serviceDiscoveryAgent, SIGNAL(serviceDiscovered(const QLowEnergyServiceInfo&)),
- this, SLOT(addLowEnergyService(const QLowEnergyServiceInfo&)));
- connect(serviceDiscoveryAgent, SIGNAL(finished()), this, SLOT(serviceScanDone()));
- connect(serviceDiscoveryAgent, SIGNAL(error(QBluetoothServiceDiscoveryAgent::Error)),
- this, SLOT(serviceScanError(QBluetoothServiceDiscoveryAgent::Error)));
+ connect(discoveryAgent, SIGNAL(finished()), this, SLOT(deviceScanFinished()));
setUpdate("Search");
}
@@ -74,8 +64,7 @@ Device::Device():
Device::~Device()
{
delete discoveryAgent;
- delete serviceDiscoveryAgent;
- delete info;
+ delete controller;
qDeleteAll(devices);
qDeleteAll(m_services);
qDeleteAll(m_characteristics);
@@ -86,6 +75,7 @@ Device::~Device()
void Device::startDeviceDiscovery()
{
+ qDeleteAll(devices);
devices.clear();
setUpdate("Scanning for devices ...");
discoveryAgent->start();
@@ -102,11 +92,11 @@ void Device::addDevice(const QBluetoothDeviceInfo &info)
}
}
-void Device::scanFinished()
+void Device::deviceScanFinished()
{
- Q_EMIT devicesDone();
+ emit devicesUpdated();
m_deviceScanState = false;
- Q_EMIT stateChanged();
+ emit stateChanged();
if (devices.isEmpty())
setUpdate("No Low Energy devices found...");
else
@@ -133,73 +123,115 @@ QString Device::getUpdate()
return m_message;
}
-void Device::scanServices(QString address)
+void Device::scanServices(const QString &address)
{
// We need the current device for service discovery.
for (int i = 0; i < devices.size(); i++) {
- if (((DeviceInfo*)devices.at(i))->getAddress() == address ) {
+ if (((DeviceInfo*)devices.at(i))->getAddress() == address )
currentDevice.setDevice(((DeviceInfo*)devices.at(i))->getDevice());
- }
}
+ if (!currentDevice.getDevice().isValid()) {
+ qWarning() << "Not a valid device";
+ return;
+ }
+
+ qDeleteAll(m_characteristics);
+ m_characteristics.clear();
+ qDeleteAll(m_services);
m_services.clear();
- QBluetoothDeviceInfo dev = currentDevice.getDevice();
- serviceDiscoveryAgent->setRemoteAddress(dev.address());
- serviceDiscoveryAgent->start();
- setUpdate("Scanning for services...");
- if (!info) {
+
+ setUpdate("Connecting to device...");
+
+ if (controller && controller->remoteAddress() != currentDevice.getDevice().address()) {
+ controller->disconnectFromDevice();
+ delete controller;
+ controller = 0;
+ }
+
+ if (!controller) {
// Connecting signals and slots for connecting to LE services.
- info = new QLowEnergyController();
- connect(info, SIGNAL(connected(QLowEnergyServiceInfo)), this, SLOT(serviceConnected(QLowEnergyServiceInfo)));
- connect(info, SIGNAL(error(QLowEnergyServiceInfo, QLowEnergyCOntroller::Error)),
- this, SLOT(errorReceived(QLowEnergyServiceInfo,QLowEnergyCOntroller::Error)));
- connect(info, SIGNAL(disconnected(QLowEnergyServiceInfo)), this, SLOT(serviceDisconnected(QLowEnergyServiceInfo)));
+ controller = new QLowEnergyControllerNew(currentDevice.getDevice().address());
+ connect(controller, SIGNAL(connected()),
+ this, SLOT(deviceConnected()));
+ connect(controller, SIGNAL(error(QLowEnergyControllerNew::Error)),
+ this, SLOT(errorReceived(QLowEnergyControllerNew::Error)));
+ connect(controller, SIGNAL(disconnected()),
+ this, SLOT(deviceDisconnected()));
+ connect(controller, SIGNAL(serviceDiscovered(QBluetoothUuid)),
+ this, SLOT(addLowEnergyService(QBluetoothUuid)));
+ connect(controller, SIGNAL(discoveryFinished()),
+ this, SLOT(serviceScanDone()));
}
+
+ controller->connectToDevice();
}
-void Device::addLowEnergyService(const QLowEnergyServiceInfo &service)
+void Device::addLowEnergyService(const QBluetoothUuid &serviceUuid)
{
+ QLowEnergyService *service = controller->createServiceObject(serviceUuid);
+ if (!service) {
+ qWarning() << "Cannot create service for uuid";
+ return;
+ }
ServiceInfo *serv = new ServiceInfo(service);
m_services.append(serv);
+
+ emit servicesUpdated();
}
void Device::serviceScanDone()
{
- Q_EMIT servicesDone();
setUpdate("Service scan done!");
}
void Device::connectToService(const QString &uuid)
{
- QString serviceUuid = uuid;
- serviceUuid = serviceUuid.remove(QLatin1Char('{')).remove(QLatin1Char('}'));
- QBluetoothUuid u(serviceUuid);
- QLowEnergyServiceInfo a;
+ QLowEnergyService *service = 0;
for (int i = 0; i < m_services.size(); i++) {
- ServiceInfo *service = (ServiceInfo*)m_services.at(i);
- a = QLowEnergyServiceInfo(service->getLeService());
- if (a.serviceUuid() == u)
- info->connectToService(a);
+ ServiceInfo *serviceInfo = (ServiceInfo*)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) {
+ connect(service, SIGNAL(stateChanged(QLowEnergyService::ServiceState)),
+ this, SLOT(serviceDetailsDiscovered(QLowEnergyService::ServiceState)));
+ service->discoverDetails();
+ return;
+ }
+
+ //discovery already done
+ const QList<QLowEnergyCharacteristic> chars = service->characteristics();
+ foreach (const QLowEnergyCharacteristic &ch, chars) {
+ CharacteristicInfo *cInfo = new CharacteristicInfo(ch);
+ m_characteristics.append(cInfo);
+ }
+
+ emit characteristicsUpdated();
}
-void Device::serviceConnected(const QLowEnergyServiceInfo &service)
+void Device::deviceConnected()
{
- m_characteristics.clear();
- setUpdate("Service connected!");
+
+ setUpdate("Discovering services!");
connected = true;
- for (int i = 0; i < service.characteristics().size(); i++) {
- CharacteristicInfo *chars = new CharacteristicInfo((QLowEnergyCharacteristicInfo)service.characteristics().at(i));
- m_characteristics.append(chars);
- }
- emit characteristicsDone();
+ controller->discoverServices();
}
-void Device::errorReceived(const QLowEnergyServiceInfo &service, QLowEnergyController::Error error)
+void Device::errorReceived(QLowEnergyControllerNew::Error /*error*/)
{
- qWarning() << "Error: " << info->errorString() << service.serviceUuid() << error;
- setUpdate(info->errorString());
+ qWarning() << "Error: " << controller->errorString();
+ setUpdate(controller->errorString());
}
void Device::setUpdate(QString message)
@@ -208,32 +240,40 @@ void Device::setUpdate(QString message)
emit updateChanged();
}
-void Device::disconnectFromService()
+void Device::disconnectFromDevice()
{
if (connected)
- info->disconnectFromService();
+ controller->disconnectFromDevice();
}
-void Device::serviceDisconnected(const QLowEnergyServiceInfo &service)
+void Device::deviceDisconnected()
{
- setUpdate("Service Disconnected " + service.serviceName());
+ //setUpdate("Device disconnected " + currentDevice.getName());
}
-void Device::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
+void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState)
{
- if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
- setUpdate("The Bluetooth adaptor is powered off, power it on before doing discovery.");
- else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
- setUpdate("Writing or reading from the device resulted in an error.");
- else
- setUpdate("An unknown error has occurred.");
+ if (newState != QLowEnergyService::ServiceDiscovered)
+ return;
+
+ QLowEnergyService *service = qobject_cast<QLowEnergyService *>(sender());
+ if (!service)
+ return;
+
+ const QList<QLowEnergyCharacteristic> chars = service->characteristics();
+ foreach (const QLowEnergyCharacteristic &ch, chars) {
+ CharacteristicInfo *cInfo = new CharacteristicInfo(ch);
+ m_characteristics.append(cInfo);
+ }
+
+ emit characteristicsUpdated();
}
-void Device::serviceScanError(QBluetoothServiceDiscoveryAgent::Error error)
+void Device::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
{
- if (error == QBluetoothServiceDiscoveryAgent::PoweredOffError)
+ if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
setUpdate("The Bluetooth adaptor is powered off, power it on before doing discovery.");
- else if (error == QBluetoothServiceDiscoveryAgent::InputOutputError)
+ else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
setUpdate("Writing or reading from the device resulted in an error.");
else
setUpdate("An unknown error has occurred.");