2013-12-16 01:12:22 +00:00
|
|
|
/***************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2013 BlackBerry Limited. All rights reserved.
|
2014-07-03 14:18:03 +00:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-12-16 01:12:22 +00:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** This file is part of the QtBluetooth module of the Qt Toolkit.
|
|
|
|
|
**
|
|
|
|
|
** $QT_BEGIN_LICENSE:BSD$
|
|
|
|
|
** You may use this file under the terms of the BSD license as follows:
|
|
|
|
|
**
|
|
|
|
|
** "Redistribution and use in source and binary forms, with or without
|
|
|
|
|
** modification, are permitted provided that the following conditions are
|
|
|
|
|
** met:
|
|
|
|
|
** * Redistributions of source code must retain the above copyright
|
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
|
** * Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
** notice, this list of conditions and the following disclaimer in
|
|
|
|
|
** the documentation and/or other materials provided with the
|
|
|
|
|
** distribution.
|
|
|
|
|
** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
|
|
|
|
|
** of its contributors may be used to endorse or promote products derived
|
|
|
|
|
** from this software without specific prior written permission.
|
|
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
|
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
|
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
|
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|
|
|
|
**
|
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "device.h"
|
|
|
|
|
#include <qbluetoothaddress.h>
|
|
|
|
|
#include <qbluetoothdevicediscoveryagent.h>
|
|
|
|
|
#include <qbluetoothlocaldevice.h>
|
|
|
|
|
#include <qbluetoothdeviceinfo.h>
|
|
|
|
|
#include <qbluetoothservicediscoveryagent.h>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QList>
|
2014-07-14 11:01:47 +00:00
|
|
|
#include <QTimer>
|
2013-12-16 01:12:22 +00:00
|
|
|
|
|
|
|
|
Device::Device():
|
2014-07-03 14:18:03 +00:00
|
|
|
connected(false), controller(0), m_deviceScanState(false)
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
2014-02-26 11:09:23 +00:00
|
|
|
discoveryAgent = new QBluetoothDeviceDiscoveryAgent();
|
2013-12-16 01:12:22 +00:00
|
|
|
connect(discoveryAgent, SIGNAL(deviceDiscovered(const QBluetoothDeviceInfo&)),
|
|
|
|
|
this, SLOT(addDevice(const QBluetoothDeviceInfo&)));
|
2014-02-26 11:09:23 +00:00
|
|
|
connect(discoveryAgent, SIGNAL(error(QBluetoothDeviceDiscoveryAgent::Error)),
|
|
|
|
|
this, SLOT(deviceScanError(QBluetoothDeviceDiscoveryAgent::Error)));
|
2014-07-03 14:18:03 +00:00
|
|
|
connect(discoveryAgent, SIGNAL(finished()), this, SLOT(deviceScanFinished()));
|
2013-12-16 01:12:22 +00:00
|
|
|
|
|
|
|
|
setUpdate("Search");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Device::~Device()
|
|
|
|
|
{
|
|
|
|
|
delete discoveryAgent;
|
2014-07-03 14:18:03 +00:00
|
|
|
delete controller;
|
2013-12-16 01:12:22 +00:00
|
|
|
qDeleteAll(devices);
|
|
|
|
|
qDeleteAll(m_services);
|
|
|
|
|
qDeleteAll(m_characteristics);
|
|
|
|
|
devices.clear();
|
|
|
|
|
m_services.clear();
|
|
|
|
|
m_characteristics.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Device::startDeviceDiscovery()
|
|
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
qDeleteAll(devices);
|
2014-02-26 11:09:23 +00:00
|
|
|
devices.clear();
|
2014-07-14 11:01:47 +00:00
|
|
|
emit devicesUpdated();
|
|
|
|
|
|
2014-02-26 11:09:23 +00:00
|
|
|
setUpdate("Scanning for devices ...");
|
|
|
|
|
discoveryAgent->start();
|
2014-03-08 13:19:55 +00:00
|
|
|
m_deviceScanState = true;
|
|
|
|
|
Q_EMIT stateChanged();
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Device::addDevice(const QBluetoothDeviceInfo &info)
|
|
|
|
|
{
|
2014-06-04 08:28:30 +00:00
|
|
|
if (info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
|
2013-12-16 01:12:22 +00:00
|
|
|
DeviceInfo *d = new DeviceInfo(info);
|
|
|
|
|
devices.append(d);
|
|
|
|
|
setUpdate("Last device added: " + d->getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::deviceScanFinished()
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
emit devicesUpdated();
|
2014-03-08 13:19:55 +00:00
|
|
|
m_deviceScanState = false;
|
2014-07-03 14:18:03 +00:00
|
|
|
emit stateChanged();
|
2014-02-26 11:09:23 +00:00
|
|
|
if (devices.isEmpty())
|
|
|
|
|
setUpdate("No Low Energy devices found...");
|
|
|
|
|
else
|
|
|
|
|
setUpdate("Done! Scan Again!");
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant Device::getDevices()
|
|
|
|
|
{
|
|
|
|
|
return QVariant::fromValue(devices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant Device::getServices()
|
|
|
|
|
{
|
|
|
|
|
return QVariant::fromValue(m_services);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant Device::getCharacteristics()
|
|
|
|
|
{
|
|
|
|
|
return QVariant::fromValue(m_characteristics);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Device::getUpdate()
|
|
|
|
|
{
|
|
|
|
|
return m_message;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::scanServices(const QString &address)
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
|
|
|
|
// We need the current device for service discovery.
|
|
|
|
|
for (int i = 0; i < devices.size(); i++) {
|
2014-07-03 14:18:03 +00:00
|
|
|
if (((DeviceInfo*)devices.at(i))->getAddress() == address )
|
2013-12-16 01:12:22 +00:00
|
|
|
currentDevice.setDevice(((DeviceInfo*)devices.at(i))->getDevice());
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
if (!currentDevice.getDevice().isValid()) {
|
|
|
|
|
qWarning() << "Not a valid device";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qDeleteAll(m_characteristics);
|
|
|
|
|
m_characteristics.clear();
|
2014-07-14 11:01:47 +00:00
|
|
|
emit characteristicsUpdated();
|
2014-07-03 14:18:03 +00:00
|
|
|
qDeleteAll(m_services);
|
2013-12-16 01:12:22 +00:00
|
|
|
m_services.clear();
|
2014-07-14 11:01:47 +00:00
|
|
|
emit servicesUpdated();
|
2014-07-03 14:18:03 +00:00
|
|
|
|
|
|
|
|
setUpdate("Connecting to device...");
|
|
|
|
|
|
|
|
|
|
if (controller && controller->remoteAddress() != currentDevice.getDevice().address()) {
|
|
|
|
|
controller->disconnectFromDevice();
|
|
|
|
|
delete controller;
|
|
|
|
|
controller = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!controller) {
|
2014-02-26 11:09:23 +00:00
|
|
|
// Connecting signals and slots for connecting to LE services.
|
2014-07-03 14:18:03 +00:00
|
|
|
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()));
|
2014-02-26 11:09:23 +00:00
|
|
|
}
|
2014-07-03 14:18:03 +00:00
|
|
|
|
|
|
|
|
controller->connectToDevice();
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::addLowEnergyService(const QBluetoothUuid &serviceUuid)
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
QLowEnergyService *service = controller->createServiceObject(serviceUuid);
|
|
|
|
|
if (!service) {
|
|
|
|
|
qWarning() << "Cannot create service for uuid";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-12-16 01:12:22 +00:00
|
|
|
ServiceInfo *serv = new ServiceInfo(service);
|
|
|
|
|
m_services.append(serv);
|
2014-07-03 14:18:03 +00:00
|
|
|
|
|
|
|
|
emit servicesUpdated();
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Device::serviceScanDone()
|
|
|
|
|
{
|
|
|
|
|
setUpdate("Service scan done!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Device::connectToService(const QString &uuid)
|
|
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
QLowEnergyService *service = 0;
|
2013-12-16 01:12:22 +00:00
|
|
|
for (int i = 0; i < m_services.size(); i++) {
|
2014-07-03 14:18:03 +00:00
|
|
|
ServiceInfo *serviceInfo = (ServiceInfo*)m_services.at(i);
|
|
|
|
|
if (serviceInfo->getUuid() == uuid) {
|
|
|
|
|
service = serviceInfo->service();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
2014-07-03 14:18:03 +00:00
|
|
|
|
|
|
|
|
if (!service)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
qDeleteAll(m_characteristics);
|
|
|
|
|
m_characteristics.clear();
|
2014-07-14 11:01:47 +00:00
|
|
|
emit characteristicsUpdated();
|
2014-07-03 14:18:03 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-14 11:01:47 +00:00
|
|
|
QTimer::singleShot(0, this, SIGNAL(characteristicsUpdated()));
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::deviceConnected()
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
setUpdate("Discovering services!");
|
2013-12-16 01:12:22 +00:00
|
|
|
connected = true;
|
2014-07-03 14:18:03 +00:00
|
|
|
controller->discoverServices();
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::errorReceived(QLowEnergyControllerNew::Error /*error*/)
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
qWarning() << "Error: " << controller->errorString();
|
|
|
|
|
setUpdate(controller->errorString());
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Device::setUpdate(QString message)
|
|
|
|
|
{
|
|
|
|
|
m_message = message;
|
|
|
|
|
emit updateChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::disconnectFromDevice()
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
|
|
|
|
if (connected)
|
2014-07-03 14:18:03 +00:00
|
|
|
controller->disconnectFromDevice();
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::deviceDisconnected()
|
2013-12-16 01:12:22 +00:00
|
|
|
{
|
2014-07-14 11:01:47 +00:00
|
|
|
qWarning() << "Disconnect from device";
|
|
|
|
|
emit disconnected();
|
2014-02-26 11:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::serviceDetailsDiscovered(QLowEnergyService::ServiceState newState)
|
2014-02-26 11:09:23 +00:00
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
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();
|
2014-02-26 11:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
2014-07-03 14:18:03 +00:00
|
|
|
void Device::deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error)
|
2014-02-26 11:09:23 +00:00
|
|
|
{
|
2014-07-03 14:18:03 +00:00
|
|
|
if (error == QBluetoothDeviceDiscoveryAgent::PoweredOffError)
|
2014-02-26 11:09:23 +00:00
|
|
|
setUpdate("The Bluetooth adaptor is powered off, power it on before doing discovery.");
|
2014-07-03 14:18:03 +00:00
|
|
|
else if (error == QBluetoothDeviceDiscoveryAgent::InputOutputError)
|
2014-02-26 11:09:23 +00:00
|
|
|
setUpdate("Writing or reading from the device resulted in an error.");
|
|
|
|
|
else
|
|
|
|
|
setUpdate("An unknown error has occurred.");
|
2013-12-16 01:12:22 +00:00
|
|
|
}
|
2014-03-08 13:19:55 +00:00
|
|
|
|
|
|
|
|
bool Device::state()
|
|
|
|
|
{
|
|
|
|
|
return m_deviceScanState;
|
|
|
|
|
}
|