From 1f14b62977b3deb285be13f4ede2449df76f24c1 Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Sun, 16 Apr 2017 18:31:22 -0700 Subject: bluetooth: populate data for existing avrcp/a2dp connection Bluetooth connection could exist before mediaplayer application is started. This patchset detects that and populates the initial metadata, and track position + status. AGL-Bug: SPEC-526 Change-Id: Ia0d60972c8eddd8642add708e9a4529c038e931f Signed-off-by: Matt Ranostay --- app/dbus.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/dbus.h | 3 ++ app/main.cpp | 3 +- 3 files changed, 95 insertions(+), 1 deletion(-) diff --git a/app/dbus.cpp b/app/dbus.cpp index 4c307a7..0deb4ee 100644 --- a/app/dbus.cpp +++ b/app/dbus.cpp @@ -83,6 +83,9 @@ bool DbusService::enableBluetooth() if (!system_bus.isConnected()) return false; + if (deviceConnected(system_bus)) + initialBluetoothData(system_bus); + ret = system_bus.connect(QString("org.bluez"), QString("/"), interface, "InterfacesAdded", this, SLOT(newBluetoothDevice(QDBusObjectPath,QVariantMap))); if (!ret) @@ -108,6 +111,93 @@ bool DbusService::checkIfPlayer(const QString& path) const return false; } +bool DbusService::deviceConnected(const QDBusConnection& system_bus) +{ + QDBusInterface interface("org.bluez", "/", "org.freedesktop.DBus.ObjectManager", system_bus); + QDBusMessage result = interface.call("GetManagedObjects"); + const QDBusArgument argument = result.arguments().at(0).value(); + bool ret = false; + + if (argument.currentType() != QDBusArgument::MapType) + return false; + + argument.beginMap(); + + while (!argument.atEnd()) { + QString key; + + argument.beginMapEntry(); + argument >> key; + argument.endMapEntry(); + + ret = checkIfPlayer(key); + + if (ret) { + newBluetoothDevice(QDBusObjectPath(key), QVariantMap()); + break; + } + } + + argument.endMap(); + + return ret; +} + +void DbusService::initialBluetoothData(const QDBusConnection& system_bus) +{ + QDBusInterface interface("org.bluez", getBluezPath(), "org.freedesktop.DBus.Properties", system_bus); + + QDBusMessage result = interface.call("GetAll", "org.bluez.MediaPlayer1"); + const QDBusArgument argument = result.arguments().at(0).value(); + QString status, artist, title; + int position = 0, duration = 0; + + if (argument.currentType() != QDBusArgument::MapType) + return; + + argument.beginMap(); + + while (!argument.atEnd()) { + QString key; + QVariant value; + + argument.beginMapEntry(); + argument >> key >> value; + + if (key == "Position") { + position = value.toInt(); + } else if (key == "Status") { + status = value.toString(); + } else if (key == "Track") { + const QDBusArgument argument1 = qvariant_cast(value); + QString key1; + QVariant value1; + + argument1.beginMap(); + + while (!argument1.atEnd()) { + argument1.beginMapEntry(); + argument1 >> key1 >> value1; + if (key1 == "Artist") + artist = value1.toString(); + else if (key1 == "Title") + title = value1.toString(); + else if (key1 == "Duration") + duration = value1.toInt(); + argument1.endMapEntry(); + } + argument1.endMap(); + } + argument.endMapEntry(); + } + argument.endMap(); + + emit processPlaylistHide(); + emit displayBluetoothMetadata(artist, title, duration); + emit updatePlayerStatus(status); + emit updatePosition(position); +} + void DbusService::newBluetoothDevice(const QDBusObjectPath& item, const QVariantMap&) { QString path = item.path(); diff --git a/app/dbus.h b/app/dbus.h index b470166..b2cfeb4 100644 --- a/app/dbus.h +++ b/app/dbus.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "lightmediascanner.h" @@ -43,6 +44,8 @@ private: void setBluezPath(const QString& path); QString getBluezPath() const; bool checkIfPlayer(const QString& path) const; + bool deviceConnected(const QDBusConnection& system_bus); + void initialBluetoothData(const QDBusConnection& system_bus); QString bluezPath; signals: diff --git a/app/main.cpp b/app/main.cpp index dd84f59..a8c32d7 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -90,6 +90,8 @@ int main(int argc, char *argv[]) #if defined(HAVE_DBUS) DbusService dbus_service; context->setContextProperty("dbus", &dbus_service); + + engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml"))); #if defined(HAVE_LIGHTMEDIASCANNER) if (!dbus_service.enableLMS()) qWarning() << "Cannot run enableLMS"; @@ -97,7 +99,6 @@ int main(int argc, char *argv[]) if (!dbus_service.enableBluetooth()) qWarning() << "Cannot run enableBluetooth"; #endif - engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml"))); return app.exec(); } -- cgit 1.2.3-korg