From c39189a922ce7057b410eba12e80abac589712cf Mon Sep 17 00:00:00 2001 From: Alex Blasche Date: Thu, 3 Jul 2014 16:18:03 +0200 Subject: 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 --- .../lowenergyscanner/assets/Characteristics.qml | 7 +- .../bluetooth/lowenergyscanner/assets/Menu.qml | 3 + .../bluetooth/lowenergyscanner/assets/Services.qml | 19 ++- .../bluetooth/lowenergyscanner/assets/main.qml | 22 ++- .../lowenergyscanner/characteristicinfo.cpp | 68 +++++--- .../lowenergyscanner/characteristicinfo.h | 11 +- examples/bluetooth/lowenergyscanner/device.cpp | 172 +++++++++++++-------- examples/bluetooth/lowenergyscanner/device.h | 50 +++--- examples/bluetooth/lowenergyscanner/deviceinfo.cpp | 6 +- examples/bluetooth/lowenergyscanner/deviceinfo.h | 1 + examples/bluetooth/lowenergyscanner/main.cpp | 4 +- .../bluetooth/lowenergyscanner/serviceinfo.cpp | 35 +++-- examples/bluetooth/lowenergyscanner/serviceinfo.h | 11 +- 13 files changed, 260 insertions(+), 149 deletions(-) (limited to 'examples/bluetooth/lowenergyscanner') diff --git a/examples/bluetooth/lowenergyscanner/assets/Characteristics.qml b/examples/bluetooth/lowenergyscanner/assets/Characteristics.qml index bb308f2f..37d6a999 100644 --- a/examples/bluetooth/lowenergyscanner/assets/Characteristics.qml +++ b/examples/bluetooth/lowenergyscanner/assets/Characteristics.qml @@ -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. @@ -76,7 +77,6 @@ Rectangle { textContent: modelData.characteristicName anchors.top: parent.top anchors.topMargin: 5 - } Label { @@ -114,7 +114,6 @@ Rectangle { height: 2 color: "#363636" } - } } @@ -124,6 +123,8 @@ Rectangle { menuWidth: parent.width menuText: device.update menuHeight: (parent.height/6) - onButtonClick: {device.disconnectFromService(); pageLoader.source = "Services.qml"} + onButtonClick: { + pageLoader.source = "Services.qml" + } } } diff --git a/examples/bluetooth/lowenergyscanner/assets/Menu.qml b/examples/bluetooth/lowenergyscanner/assets/Menu.qml index 796c27be..7c043f30 100644 --- a/examples/bluetooth/lowenergyscanner/assets/Menu.qml +++ b/examples/bluetooth/lowenergyscanner/assets/Menu.qml @@ -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. @@ -54,6 +55,8 @@ Rectangle { id: search width: parent.width height: parent.height + anchors.horizontalCenter: parent.horizontalCenter + anchors.verticalCenter: parent.verticalCenter; color: "#363636" border.width: 1 border.color: "#E3E3E3" diff --git a/examples/bluetooth/lowenergyscanner/assets/Services.qml b/examples/bluetooth/lowenergyscanner/assets/Services.qml index 31775884..ba9f3831 100644 --- a/examples/bluetooth/lowenergyscanner/assets/Services.qml +++ b/examples/bluetooth/lowenergyscanner/assets/Services.qml @@ -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. @@ -56,7 +57,10 @@ Rectangle { visible: false } - Component.onCompleted: { info.visible = true; info.dialogText = "Scanning for services...";} + Component.onCompleted: { + info.visible = true; + info.dialogText = "Scanning for services..."; + } ListView { id: servicesview @@ -75,8 +79,10 @@ Rectangle { anchors.fill: parent onPressed: { servicebox.height= 135; bottomarea.height = 7} onReleased: { servicebox.height= 140; bottomarea.height = 2} - onClicked: { device.connectToService(modelData.serviceUuid); pageLoader.source = "Characteristics.qml";} - + onClicked: { + device.connectToService(modelData.serviceUuid); + pageLoader.source = "Characteristics.qml"; + } } Label { @@ -88,6 +94,7 @@ Rectangle { Label { id: serviceUuid + font.pointSize: serviceName1.font.pointSize * 0.5 textContent: modelData.serviceUuid anchors.bottom: bottomarea.top anchors.bottomMargin: 5 @@ -100,7 +107,6 @@ Rectangle { height: 2 color: "#363636" } - } } @@ -110,6 +116,9 @@ Rectangle { menuWidth: parent.width menuText: "Back" menuHeight: (parent.height/6) - onButtonClick: pageLoader.source = "main.qml" + onButtonClick: { + device.disconnectFromDevice() + pageLoader.source = "main.qml" + } } } diff --git a/examples/bluetooth/lowenergyscanner/assets/main.qml b/examples/bluetooth/lowenergyscanner/assets/main.qml index c7c201e8..b5a1f14d 100644 --- a/examples/bluetooth/lowenergyscanner/assets/main.qml +++ b/examples/bluetooth/lowenergyscanner/assets/main.qml @@ -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. @@ -53,7 +54,7 @@ Rectangle { Header { id: header anchors.top: parent.top - headerText: "Start a Device Discovery" + headerText: "Start Discovery" } Dialog { @@ -74,15 +75,19 @@ Rectangle { id: box height:140 width: parent.width - Component.onCompleted: { info.visible = false; + Component.onCompleted: { + info.visible = false; header.headerText = "Select a device"; } MouseArea { anchors.fill: parent - onPressed: { box.height= 135; downpart.height = 7} - onReleased: { box.height= 140; downpart.height = 2} - onClicked: {device.scanServices(modelData.deviceAddress); pageLoader.source = "Services.qml"} + onPressed: { box.height = 135; downpart.height = 7} + onReleased: { box.height = 140; downpart.height = 2} + onClicked: { + device.scanServices(modelData.deviceAddress); + pageLoader.source = "Services.qml" + } } @@ -107,7 +112,6 @@ Rectangle { height: 2 color: "#363636" } - } } @@ -117,12 +121,14 @@ Rectangle { menuWidth: parent.width menuHeight: (parent.height/6) menuText: device.update - onButtonClick: { device.startDeviceDiscovery(); info.dialogText = "Searching..."; info.visible = true;} + onButtonClick: { + device.startDeviceDiscovery(); + info.dialogText = "Searching..."; + info.visible = true;} } Loader { id: pageLoader anchors.fill: parent - } } diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp b/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp index 2a69529a..b0a60b34 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.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. @@ -42,31 +43,54 @@ #include "qbluetoothuuid.h" #include -CharacteristicInfo::CharacteristicInfo(): - m_characteristic(QLowEnergyCharacteristicInfo()) +CharacteristicInfo::CharacteristicInfo() { } -CharacteristicInfo::CharacteristicInfo(const QLowEnergyCharacteristicInfo &characteristic): +CharacteristicInfo::CharacteristicInfo(const QLowEnergyCharacteristic &characteristic): m_characteristic(characteristic) { - emit characteristicChanged(); } -void CharacteristicInfo::setCharacteristic(const QLowEnergyCharacteristicInfo &characteristic) +void CharacteristicInfo::setCharacteristic(const QLowEnergyCharacteristic &characteristic) { - m_characteristic = QLowEnergyCharacteristicInfo(characteristic); + m_characteristic = characteristic; emit characteristicChanged(); } QString CharacteristicInfo::getName() const { - return m_characteristic.name(); + QString name = m_characteristic.name(); + if (!name.isEmpty()) + return name; + + // find descriptor with CharacteristicUserDescription + foreach (const QLowEnergyDescriptor &descriptor, m_characteristic.descriptors()) { + if (descriptor.type() == QBluetoothUuid::CharacteristicUserDescription) { + name = QByteArray::fromHex(descriptor.value()); + break; + } + } + + if (name.isEmpty()) + name = "Unknown"; + + return name; } QString CharacteristicInfo::getUuid() const { - return m_characteristic.uuid().toString().remove(QLatin1Char('{')).remove(QLatin1Char('}')); + const QBluetoothUuid uuid = m_characteristic.uuid(); + bool success = false; + quint16 result16 = uuid.toUInt16(&success); + if (success) + return QStringLiteral("0x") + QString::number(result16, 16); + + quint32 result32 = uuid.toUInt32(&success); + if (success) + return QStringLiteral("0x") + QString::number(result32, 16); + + return uuid.toString().remove(QLatin1Char('{')).remove(QLatin1Char('}')); } QString CharacteristicInfo::getValue() const @@ -89,20 +113,26 @@ QString CharacteristicInfo::getPermission() const { QString properties = "Properties:"; int permission = m_characteristic.properties(); - if (permission & QLowEnergyCharacteristicInfo::Read) - properties = properties + QStringLiteral(" Read"); - if (permission & QLowEnergyCharacteristicInfo::Write) - properties = properties + QStringLiteral(" Write"); - if (permission & QLowEnergyCharacteristicInfo::Notify) - properties = properties + QStringLiteral(" Notify"); - if (permission & QLowEnergyCharacteristicInfo::Indicate) - properties = properties + QStringLiteral(" Indicate"); - if (permission & QLowEnergyCharacteristicInfo::ExtendedProperty) - properties = properties + QStringLiteral(" ExtendedProperty"); + if (permission & QLowEnergyCharacteristic::Read) + properties += QStringLiteral(" Read"); + if (permission & QLowEnergyCharacteristic::Write) + properties += QStringLiteral(" Write"); + if (permission & QLowEnergyCharacteristic::Notify) + properties += QStringLiteral(" Notify"); + if (permission & QLowEnergyCharacteristic::Indicate) + properties += QStringLiteral(" Indicate"); + if (permission & QLowEnergyCharacteristic::ExtendedProperty) + properties += QStringLiteral(" ExtendedProperty"); + if (permission & QLowEnergyCharacteristic::Broadcasting) + properties += QStringLiteral(" Broadcast"); + if (permission & QLowEnergyCharacteristic::WriteNoResponse) + properties += QStringLiteral(" WriteNoResp"); + if (permission & QLowEnergyCharacteristic::WriteSigned) + properties += QStringLiteral(" WriteSigned"); return properties; } -QLowEnergyCharacteristicInfo CharacteristicInfo::getCharacteristic() const +QLowEnergyCharacteristic CharacteristicInfo::getCharacteristic() const { return m_characteristic; } diff --git a/examples/bluetooth/lowenergyscanner/characteristicinfo.h b/examples/bluetooth/lowenergyscanner/characteristicinfo.h index 7f746960..d06c962e 100644 --- a/examples/bluetooth/lowenergyscanner/characteristicinfo.h +++ b/examples/bluetooth/lowenergyscanner/characteristicinfo.h @@ -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. @@ -42,7 +43,7 @@ #define CHARACTERISTICINFO_H #include #include -#include +#include class CharacteristicInfo: public QObject { @@ -55,20 +56,20 @@ class CharacteristicInfo: public QObject public: CharacteristicInfo(); - CharacteristicInfo(const QLowEnergyCharacteristicInfo &characteristic); - void setCharacteristic(const QLowEnergyCharacteristicInfo &characteristic); + CharacteristicInfo(const QLowEnergyCharacteristic &characteristic); + void setCharacteristic(const QLowEnergyCharacteristic &characteristic); QString getName() const; QString getUuid() const; QString getValue() const; QString getHandle() const; QString getPermission() const; - QLowEnergyCharacteristicInfo getCharacteristic() const; + QLowEnergyCharacteristic getCharacteristic() const; Q_SIGNALS: void characteristicChanged(); private: - QLowEnergyCharacteristicInfo m_characteristic; + QLowEnergyCharacteristic m_characteristic; }; #endif // CHARACTERISTICINFO_H 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 #include #include -#include -#include -#include #include #include 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 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(sender()); + if (!service) + return; + + const QList 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."); diff --git a/examples/bluetooth/lowenergyscanner/device.h b/examples/bluetooth/lowenergyscanner/device.h index 3ac28c8f..053b04f1 100644 --- a/examples/bluetooth/lowenergyscanner/device.h +++ b/examples/bluetooth/lowenergyscanner/device.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the demonstration applications of the Qt Toolkit. @@ -48,23 +48,22 @@ #include #include #include +#include +#include #include "deviceinfo.h" -#include "qlowenergycontroller.h" -#include "qlowenergyserviceinfo.h" #include "serviceinfo.h" #include "characteristicinfo.h" QT_FORWARD_DECLARE_CLASS (QBluetoothDeviceInfo) QT_FORWARD_DECLARE_CLASS (QLowEnergyServiceInfo) -QT_FORWARD_DECLARE_CLASS (QLowEnergyCharacteristicInfo) QT_FORWARD_DECLARE_CLASS (QBluetoothServiceInfo) class Device: public QObject { Q_OBJECT - Q_PROPERTY(QVariant devicesList READ getDevices NOTIFY devicesDone) - Q_PROPERTY(QVariant servicesList READ getServices NOTIFY servicesDone) - Q_PROPERTY(QVariant characteristicList READ getCharacteristics NOTIFY characteristicsDone) + Q_PROPERTY(QVariant devicesList READ getDevices NOTIFY devicesUpdated) + Q_PROPERTY(QVariant servicesList READ getServices NOTIFY servicesUpdated) + Q_PROPERTY(QVariant characteristicList READ getCharacteristics NOTIFY characteristicsUpdated) Q_PROPERTY(QString update READ getUpdate NOTIFY updateChanged) Q_PROPERTY(bool state READ state NOTIFY stateChanged) public: @@ -77,38 +76,45 @@ public: bool state(); public slots: - void addDevice(const QBluetoothDeviceInfo&); void startDeviceDiscovery(); - void scanFinished(); + void scanServices(const QString &address); + + void connectToService(const QString &uuid); + void disconnectFromDevice(); + +private slots: + // QBluetoothDeviceDiscoveryAgent related + void addDevice(const QBluetoothDeviceInfo&); + void deviceScanFinished(); void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error); - void scanServices(QString address); - void addLowEnergyService(const QLowEnergyServiceInfo&); + + // QLowEnergyControllerNew realted + void addLowEnergyService(const QBluetoothUuid &uuid); + void deviceConnected(); + void errorReceived(QLowEnergyControllerNew::Error); void serviceScanDone(); - void serviceConnected(const QLowEnergyServiceInfo &service); - void connectToService(const QString &uuid); - void errorReceived(const QLowEnergyServiceInfo &service, QLowEnergyController::Error); - void disconnectFromService(); - void serviceDisconnected(const QLowEnergyServiceInfo &service); - void serviceScanError(QBluetoothServiceDiscoveryAgent::Error); + void deviceDisconnected(); + + // QLowEnergyService related + void serviceDetailsDiscovered(QLowEnergyService::ServiceState newState); Q_SIGNALS: - void devicesDone(); - void servicesDone(); - void characteristicsDone(); + void devicesUpdated(); + void servicesUpdated(); + void characteristicsUpdated(); void updateChanged(); void stateChanged(); private: void setUpdate(QString message); QBluetoothDeviceDiscoveryAgent *discoveryAgent; - QBluetoothServiceDiscoveryAgent *serviceDiscoveryAgent; DeviceInfo currentDevice; QList devices; QList m_services; QList m_characteristics; QString m_message; bool connected; - QLowEnergyController *info; + QLowEnergyControllerNew *controller; bool m_deviceScanState; }; diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp b/examples/bluetooth/lowenergyscanner/deviceinfo.cpp index 552f5ec3..d6b7a734 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.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. @@ -42,13 +43,11 @@ DeviceInfo::DeviceInfo() { - device = QBluetoothDeviceInfo(); } DeviceInfo::DeviceInfo(const QBluetoothDeviceInfo &d) { - device = QBluetoothDeviceInfo(d); - Q_EMIT deviceChanged(); + device = d; } QString DeviceInfo::getAddress() const @@ -69,4 +68,5 @@ QBluetoothDeviceInfo DeviceInfo::getDevice() void DeviceInfo::setDevice(const QBluetoothDeviceInfo &dev) { device = QBluetoothDeviceInfo(dev); + Q_EMIT deviceChanged(); } diff --git a/examples/bluetooth/lowenergyscanner/deviceinfo.h b/examples/bluetooth/lowenergyscanner/deviceinfo.h index 383ecfac..48f11cfe 100644 --- a/examples/bluetooth/lowenergyscanner/deviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/deviceinfo.h @@ -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. diff --git a/examples/bluetooth/lowenergyscanner/main.cpp b/examples/bluetooth/lowenergyscanner/main.cpp index 89f7153d..ce359545 100644 --- a/examples/bluetooth/lowenergyscanner/main.cpp +++ b/examples/bluetooth/lowenergyscanner/main.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. @@ -47,10 +48,9 @@ int main(int argc, char *argv[]) { - QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); + //QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")); QGuiApplication app(argc, argv); - Device d; QQuickView *view = new QQuickView; view->rootContext()->setContextProperty("device", &d); diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp b/examples/bluetooth/lowenergyscanner/serviceinfo.cpp index 1028fb40..19b99b9f 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.cpp +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.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. @@ -40,29 +41,43 @@ #include "serviceinfo.h" -ServiceInfo::ServiceInfo(): - m_serviceLe(QLowEnergyServiceInfo()) +ServiceInfo::ServiceInfo() { - } -ServiceInfo::ServiceInfo(const QLowEnergyServiceInfo &service): - m_serviceLe(service) +ServiceInfo::ServiceInfo(QLowEnergyService *service): + m_service(service) { - + m_service->setParent(this); } -QLowEnergyServiceInfo ServiceInfo::getLeService() const +QLowEnergyService *ServiceInfo::service() const { - return m_serviceLe; + return m_service; } QString ServiceInfo::getName() const { - return m_serviceLe.serviceName(); + if (!m_service) + return QString(); + + return m_service->serviceName(); } QString ServiceInfo::getUuid() const { - return m_serviceLe.serviceUuid().toString().remove(QLatin1Char('{')).remove(QLatin1Char('}')); + if (!m_service) + return QString(); + + const QBluetoothUuid uuid = m_service->serviceUuid(); + bool success = false; + quint16 result16 = uuid.toUInt16(&success); + if (success) + return QStringLiteral("0x") + QString::number(result16, 16); + + quint32 result32 = uuid.toUInt32(&success); + if (success) + return QStringLiteral("0x") + QString::number(result32, 16); + + return uuid.toString().remove(QLatin1Char('{')).remove(QLatin1Char('}')); } diff --git a/examples/bluetooth/lowenergyscanner/serviceinfo.h b/examples/bluetooth/lowenergyscanner/serviceinfo.h index 56140e4c..d02c79ba 100644 --- a/examples/bluetooth/lowenergyscanner/serviceinfo.h +++ b/examples/bluetooth/lowenergyscanner/serviceinfo.h @@ -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. @@ -40,9 +41,7 @@ #ifndef SERVICEINFO_H #define SERVICEINFO_H -#include -#include "qlowenergyserviceinfo.h" -#include "qbluetoothuuid.h" +#include class ServiceInfo: public QObject { @@ -51,8 +50,8 @@ class ServiceInfo: public QObject Q_PROPERTY(QString serviceUuid READ getUuid NOTIFY serviceChanged) public: ServiceInfo(); - ServiceInfo(const QLowEnergyServiceInfo &service); - QLowEnergyServiceInfo getLeService() const; + ServiceInfo(QLowEnergyService *service); + QLowEnergyService *service() const; QString getUuid() const; QString getName() const; @@ -60,7 +59,7 @@ Q_SIGNALS: void serviceChanged(); private: - QLowEnergyServiceInfo m_serviceLe; + QLowEnergyService *m_service; }; #endif // SERVICEINFO_H -- cgit v1.2.3