diff options
-rw-r--r-- | bluetooth/bluetooth.cpp | 45 | ||||
-rw-r--r-- | bluetooth/bluetooth.h | 2 | ||||
-rw-r--r-- | bluetooth/bluetoothmessage.h | 1 |
3 files changed, 35 insertions, 13 deletions
diff --git a/bluetooth/bluetooth.cpp b/bluetooth/bluetooth.cpp index eb00df4..79cc5f1 100644 --- a/bluetooth/bluetooth.cpp +++ b/bluetooth/bluetooth.cpp @@ -225,23 +225,37 @@ void Bluetooth::processDeviceChangesEvent(QJsonObject data) 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 { - BluetoothDevice *device = m_bluetooth->getDevice(id); - m_bluetooth->updateDeviceProperties(device, data); - } + BluetoothDevice *device = m_bluetooth->getDevice(id); + m_bluetooth->updateDeviceProperties(device, data); } else if (action == "removed") { BluetoothDevice *device = m_bluetooth->getDevice(id); m_bluetooth->removeDevice(device); } } +void Bluetooth::processAdapterChangesEvent(QJsonObject data) +{ + QString action = data.value("action").toString(); + if (action != "changed") + return; + + QJsonObject properties = data.value("properties").toObject(); + if (!properties.contains("powered")) + return; + + bool powered = properties.find("powered").value().toBool(); + if (!powered) + m_bluetooth->removeAllDevices(); + + if (m_power != powered) { + m_power = powered; + emit powerChanged(powered); + } + + if (!m_power) + m_discoverable = false; +} + void Bluetooth::onMessageReceived(MessageType type, Message *msg) { if (msg->isEvent() && type == BluetoothEventMessage) { @@ -249,6 +263,8 @@ void Bluetooth::onMessageReceived(MessageType type, Message *msg) if (tmsg->isDeviceChangesEvent()) { processDeviceChangesEvent(tmsg->eventData()); + } else if (tmsg->isAdapterChangesEvent()) { + processAdapterChangesEvent(tmsg->eventData()); } else if (tmsg->isAgentEvent()) { emit requestConfirmationEvent(tmsg->eventData()); } @@ -259,8 +275,11 @@ void Bluetooth::onMessageReceived(MessageType type, Message *msg) if (tmsg->requestVerb() == "managed_objects") { populateDeviceList(tmsg->replyData()); } else if (tmsg->requestVerb() == "adapter_state") { - m_power = tmsg->replyData().value("powered").toBool(); - emit powerChanged(m_power); + bool powered = tmsg->replyData().value("powered").toBool(); + if (m_power != powered) { + m_power = powered; + emit powerChanged(m_power); + } } } diff --git a/bluetooth/bluetooth.h b/bluetooth/bluetooth.h index 6cef0ce..b79e0f4 100644 --- a/bluetooth/bluetooth.h +++ b/bluetooth/bluetooth.h @@ -71,6 +71,7 @@ class Bluetooth : public QObject void discovery_command(bool); void populateDeviceList(QJsonObject data); void processDeviceChangesEvent(QJsonObject data); + void processAdapterChangesEvent(QJsonObject data); // slots void onConnected(); @@ -86,6 +87,7 @@ class Bluetooth : public QObject QMap<QString, QString> uuids; const QStringList events { + "adapter_changes", "device_changes", "agent", }; diff --git a/bluetooth/bluetoothmessage.h b/bluetooth/bluetoothmessage.h index 4fc9124..a561061 100644 --- a/bluetooth/bluetoothmessage.h +++ b/bluetooth/bluetoothmessage.h @@ -24,6 +24,7 @@ class BluetoothMessage : public Message Q_OBJECT public: bool isDeviceChangesEvent() { return (this->eventName() == "device_changes"); }; + bool isAdapterChangesEvent() { return (this->eventName() == "adapter_changes"); }; bool isAgentEvent() { return (this->eventName() == "agent"); }; bool createRequest(QString verb, QJsonObject parameter); |