summaryrefslogtreecommitdiffstats
path: root/bluetooth/bluetooth.cpp
diff options
context:
space:
mode:
authorJan-Simon Moeller <jsmoeller@linuxfoundation.org>2019-03-25 13:40:37 +0000
committerGerrit Code Review <gerrit@automotivelinux.org>2019-03-25 13:40:37 +0000
commitf4aad7aee0d23560a9b562057b866ea6762ca3cf (patch)
tree3b55504844060b4c4e69e21b6fbc22cb9f701e91 /bluetooth/bluetooth.cpp
parent6e0dbd4ae1da3225f62caf17326f08b1a2cb3773 (diff)
parent6a80077e386e8a74940e81268055ff5ea0d97771 (diff)
Merge "libqtappfw: bluetooth: add BluetoothModel support"
Diffstat (limited to 'bluetooth/bluetooth.cpp')
-rw-r--r--bluetooth/bluetooth.cpp47
1 files changed, 39 insertions, 8 deletions
diff --git a/bluetooth/bluetooth.cpp b/bluetooth/bluetooth.cpp
index 25ff16e..eb00df4 100644
--- a/bluetooth/bluetooth.cpp
+++ b/bluetooth/bluetooth.cpp
@@ -20,15 +20,27 @@
#include "bluetoothmessage.h"
#include "responsemessage.h"
-Bluetooth::Bluetooth (QUrl &url, QObject * parent) :
+Bluetooth::Bluetooth (QUrl &url, QQmlContext *context, QObject * parent) :
QObject(parent),
+ m_context(context),
m_mloop(nullptr)
{
m_mloop = new MessageEngine(url);
+ m_bluetooth = new BluetoothModel();
QObject::connect(m_mloop, &MessageEngine::connected, this, &Bluetooth::onConnected);
QObject::connect(m_mloop, &MessageEngine::disconnected, this, &Bluetooth::onDisconnected);
QObject::connect(m_mloop, &MessageEngine::messageReceived, this, &Bluetooth::onMessageReceived);
+ BluetoothModelFilter *m_model = new BluetoothModelFilter();
+ m_model->setSourceModel(m_bluetooth);
+ m_model->setFilterFixedString("true");
+ context->setContextProperty("BluetoothPairedModel", m_model);
+
+ m_model = new BluetoothModelFilter();
+ m_model->setSourceModel(m_bluetooth);
+ m_model->setFilterFixedString("false");
+ context->setContextProperty("BluetoothDiscoveryModel", m_model);
+
uuids.insert("a2dp", "0000110a-0000-1000-8000-00805f9b34fb");
uuids.insert("avrcp", "0000110e-0000-1000-8000-00805f9b34fb");
uuids.insert("hfp", "0000111f-0000-1000-8000-00805f9b34fb");
@@ -192,23 +204,42 @@ void Bluetooth::onDisconnected()
}
}
+void Bluetooth::populateDeviceList(QJsonObject data)
+{
+ QJsonArray devices = data.value("devices").toArray();
+
+ m_bluetooth->removeAllDevices();
+
+ for (auto value : devices) {
+ BluetoothDevice *device = m_bluetooth->updateDeviceProperties(nullptr, value.toObject());
+ m_bluetooth->addDevice(device);
+ }
+}
+
void Bluetooth::processDeviceChangesEvent(QJsonObject data)
{
QString action = data.value("action").toString();
+ QString id = data.value("device").toString();
- if (action == "added")
- emit deviceAddedEvent(data);
- else if (action == "changed") {
+ if (action == "added") {
+ BluetoothDevice *device = m_bluetooth->updateDeviceProperties(nullptr, data);
+ m_bluetooth->addDevice(device);
+ } else if (action == "changed") {
auto powered = data.find("powered").value();
if (powered.isBool()) {
m_power = powered.toBool();
+ if (!m_power)
+ m_bluetooth->removeAllDevices();
emit powerChanged(m_power);
} else {
- emit deviceUpdatedEvent(data);
+ BluetoothDevice *device = m_bluetooth->getDevice(id);
+ m_bluetooth->updateDeviceProperties(device, data);
}
- } else if (action == "removed")
- emit deviceRemovedEvent(data);
+ } else if (action == "removed") {
+ BluetoothDevice *device = m_bluetooth->getDevice(id);
+ m_bluetooth->removeDevice(device);
+ }
}
void Bluetooth::onMessageReceived(MessageType type, Message *msg)
@@ -226,7 +257,7 @@ void Bluetooth::onMessageReceived(MessageType type, Message *msg)
ResponseMessage *tmsg = qobject_cast<ResponseMessage*>(msg);
if (tmsg->requestVerb() == "managed_objects") {
- emit deviceListEvent(tmsg->replyData());
+ populateDeviceList(tmsg->replyData());
} else if (tmsg->requestVerb() == "adapter_state") {
m_power = tmsg->replyData().value("powered").toBool();
emit powerChanged(m_power);