summaryrefslogtreecommitdiffstats
path: root/examples/bluetooth
diff options
context:
space:
mode:
authorAlex Blasche <alexander.blasche@qt.io>2017-03-15 13:19:46 +0100
committerAlex Blasche <alexander.blasche@qt.io>2017-04-05 08:41:58 +0000
commit6520f8af711a9b61b194d163bb8f1db53b514ef6 (patch)
tree738fb51c63408e936b7b89a6efaef063fc4c8211 /examples/bluetooth
parent39ed984d372f66299eba86be4e80e6ebffb96926 (diff)
Permit BlueZ platforms to set the address type in heartrate-game
BlueZ is the only platform that requires to set the BTLE address type. The patch introduces a new UI button which allows the user to toggle the address type. The UI element is disabled on all platforms but Linux/BlueZ systems. The number of devices returned in simulator mode is reduced to four devices. This was done purely for esthetical reasons as the additional address type button takes screen real estate away and five simulator devices require more screen real estate than the remaining space for device list. This avoid the need for scrolling. Change-Id: I14cf26b3c821db7768b9d018c2d09441cd97679a Reviewed-by: Oliver Wolff <oliver.wolff@qt.io> Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'examples/bluetooth')
-rw-r--r--examples/bluetooth/heartrate-game/connectionhandler.cpp11
-rw-r--r--examples/bluetooth/heartrate-game/connectionhandler.h2
-rw-r--r--examples/bluetooth/heartrate-game/devicefinder.cpp2
-rw-r--r--examples/bluetooth/heartrate-game/devicehandler.cpp21
-rw-r--r--examples/bluetooth/heartrate-game/devicehandler.h11
-rw-r--r--examples/bluetooth/heartrate-game/main.cpp2
-rw-r--r--examples/bluetooth/heartrate-game/qml/Connect.qml37
7 files changed, 84 insertions, 2 deletions
diff --git a/examples/bluetooth/heartrate-game/connectionhandler.cpp b/examples/bluetooth/heartrate-game/connectionhandler.cpp
index d5084179..c9c14818 100644
--- a/examples/bluetooth/heartrate-game/connectionhandler.cpp
+++ b/examples/bluetooth/heartrate-game/connectionhandler.cpp
@@ -41,6 +41,7 @@
#include "heartrate-global.h"
#include "connectionhandler.h"
#include <QtDebug>
+#include <QtBluetooth/qtbluetooth-config.h>
ConnectionHandler::ConnectionHandler(QObject *parent) : QObject(parent)
{
@@ -56,6 +57,16 @@ bool ConnectionHandler::alive() const
return m_localDevice.isValid() && m_localDevice.hostMode() != QBluetoothLocalDevice::HostPoweredOff;
#endif
}
+
+bool ConnectionHandler::requiresAddressType() const
+{
+#if QT_CONFIG(bluez)
+ return true;
+#else
+ return false;
+#endif
+}
+
QString ConnectionHandler::name() const
{
return m_localDevice.name();
diff --git a/examples/bluetooth/heartrate-game/connectionhandler.h b/examples/bluetooth/heartrate-game/connectionhandler.h
index 8c38b8ca..23168b28 100644
--- a/examples/bluetooth/heartrate-game/connectionhandler.h
+++ b/examples/bluetooth/heartrate-game/connectionhandler.h
@@ -49,12 +49,14 @@ class ConnectionHandler : public QObject
Q_PROPERTY(bool alive READ alive NOTIFY deviceChanged)
Q_PROPERTY(QString name READ name NOTIFY deviceChanged)
Q_PROPERTY(QString address READ address NOTIFY deviceChanged)
+ Q_PROPERTY(bool requiresAddressType READ requiresAddressType CONSTANT)
Q_OBJECT
public:
explicit ConnectionHandler(QObject *parent = 0);
bool alive() const;
+ bool requiresAddressType() const;
QString name() const;
QString address() const;
diff --git a/examples/bluetooth/heartrate-game/devicefinder.cpp b/examples/bluetooth/heartrate-game/devicefinder.cpp
index 71c4dcee..a56cfd6d 100644
--- a/examples/bluetooth/heartrate-game/devicefinder.cpp
+++ b/examples/bluetooth/heartrate-game/devicefinder.cpp
@@ -113,7 +113,7 @@ void DeviceFinder::scanFinished()
{
#ifdef SIMULATOR
// Only for testing
- for (int i = 0; i < 5; i++)
+ for (int i = 0; i < 4; i++)
m_devices.append(new DeviceInfo(QBluetoothDeviceInfo()));
#endif
diff --git a/examples/bluetooth/heartrate-game/devicehandler.cpp b/examples/bluetooth/heartrate-game/devicehandler.cpp
index d0f06d27..96c78313 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.cpp
+++ b/examples/bluetooth/heartrate-game/devicehandler.cpp
@@ -62,6 +62,26 @@ DeviceHandler::DeviceHandler(QObject *parent) :
#endif
}
+void DeviceHandler::setAddressType(AddressType type)
+{
+ switch (type) {
+ case DeviceHandler::AddressType::PublicAddress:
+ m_addressType = QLowEnergyController::PublicAddress;
+ break;
+ case DeviceHandler::AddressType::RandomAddress:
+ m_addressType = QLowEnergyController::RandomAddress;
+ break;
+ }
+}
+
+DeviceHandler::AddressType DeviceHandler::addressType() const
+{
+ if (m_addressType == QLowEnergyController::RandomAddress)
+ return DeviceHandler::AddressType::RandomAddress;
+
+ return DeviceHandler::AddressType::PublicAddress;
+}
+
void DeviceHandler::setDevice(DeviceInfo *device)
{
clearMessages();
@@ -84,6 +104,7 @@ void DeviceHandler::setDevice(DeviceInfo *device)
// Make connections
m_control = new QLowEnergyController(m_currentDevice->getDevice(), this);
+ m_control->setRemoteAddressType(m_addressType);
connect(m_control, &QLowEnergyController::serviceDiscovered,
this, &DeviceHandler::serviceDiscovered);
connect(m_control, &QLowEnergyController::discoveryFinished,
diff --git a/examples/bluetooth/heartrate-game/devicehandler.h b/examples/bluetooth/heartrate-game/devicehandler.h
index 08831cb3..950ed6e9 100644
--- a/examples/bluetooth/heartrate-game/devicehandler.h
+++ b/examples/bluetooth/heartrate-game/devicehandler.h
@@ -63,11 +63,21 @@ class DeviceHandler : public BluetoothBaseClass
Q_PROPERTY(float average READ average NOTIFY statsChanged)
Q_PROPERTY(int time READ time NOTIFY statsChanged)
Q_PROPERTY(float calories READ calories NOTIFY statsChanged)
+ Q_PROPERTY(AddressType addressType READ addressType WRITE setAddressType)
public:
+ enum class AddressType {
+ PublicAddress,
+ RandomAddress
+ };
+ Q_ENUM(AddressType)
+
DeviceHandler(QObject *parent = 0);
void setDevice(DeviceInfo *device);
+ void setAddressType(AddressType type);
+ AddressType addressType() const;
+
bool measuring() const;
bool alive() const;
@@ -122,6 +132,7 @@ private:
QDateTime m_stop;
QVector<int> m_measurements;
+ QLowEnergyController::RemoteAddressType m_addressType = QLowEnergyController::PublicAddress;
#ifdef SIMULATOR
QTimer m_demoTimer;
diff --git a/examples/bluetooth/heartrate-game/main.cpp b/examples/bluetooth/heartrate-game/main.cpp
index 039b0668..0b0cae12 100644
--- a/examples/bluetooth/heartrate-game/main.cpp
+++ b/examples/bluetooth/heartrate-game/main.cpp
@@ -56,6 +56,8 @@ int main(int argc, char *argv[])
DeviceHandler deviceHandler;
DeviceFinder deviceFinder(&deviceHandler);
+ qmlRegisterUncreatableType<DeviceHandler>("Shared", 1, 0, "AddressType", "Enum is not a type");
+
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("connectionHandler", &connectionHandler);
engine.rootContext()->setContextProperty("deviceFinder", &deviceFinder);
diff --git a/examples/bluetooth/heartrate-game/qml/Connect.qml b/examples/bluetooth/heartrate-game/qml/Connect.qml
index d3b4a733..9e4efb17 100644
--- a/examples/bluetooth/heartrate-game/qml/Connect.qml
+++ b/examples/bluetooth/heartrate-game/qml/Connect.qml
@@ -39,6 +39,7 @@
****************************************************************************/
import QtQuick 2.5
+import Shared 1.0
GamePage {
@@ -48,7 +49,9 @@ GamePage {
Rectangle {
id: viewContainer
anchors.top: parent.top
- anchors.bottom: searchButton.top
+ anchors.bottom:
+ // only BlueZ platform has address type selection
+ connectionHandler.requiresAddressType ? addressTypeButton.top : searchButton.top
anchors.topMargin: GameSettings.fieldMargin + messageHeight
anchors.bottomMargin: GameSettings.fieldMargin
anchors.horizontalCenter: parent.horizontalCenter
@@ -124,6 +127,38 @@ GamePage {
}
GameButton {
+ id: addressTypeButton
+ anchors.horizontalCenter: parent.horizontalCenter
+ anchors.bottom: searchButton.top
+ anchors.bottomMargin: GameSettings.fieldMargin*0.5
+ width: viewContainer.width
+ height: GameSettings.fieldHeight
+ visible: connectionHandler.requiresAddressType // only required on BlueZ
+ state: "public"
+ onClicked: state == "public" ? state = "random" : state = "public"
+
+ states: [
+ State {
+ name: "public"
+ PropertyChanges { target: addressTypeText; text: qsTr("Public Address") }
+ PropertyChanges { target: deviceHandler; addressType: AddressType.PublicAddress }
+ },
+ State {
+ name: "random"
+ PropertyChanges { target: addressTypeText; text: qsTr("Random Address") }
+ PropertyChanges { target: deviceHandler; addressType: AddressType.RandomAddress }
+ }
+ ]
+
+ Text {
+ id: addressTypeText
+ anchors.centerIn: parent
+ font.pixelSize: GameSettings.tinyFontSize
+ color: GameSettings.textColor
+ }
+ }
+
+ GameButton {
id: searchButton
anchors.horizontalCenter: parent.horizontalCenter
anchors.bottom: parent.bottom