DBus method calls with arguments
-
Hello,
I'm trying to replicate the following command using qdbus:
$ dbus-send --system --type=method_call --print-reply=literal --dest=org.bluez /org/bluez/hci1 org.freedesktop.DBus.Properties.Set string:org.bluez.Adapter1 string:Powered variant:boolean:true
Here are my two attempts which both didn't work:
#define BLUEZ_DBUS_SERVICE "org.bluez" #define BLUEZ_DBUS_PATH "/org/bluez/hci1" QDBusConnection bus = QDBusConnection::systemBus(); if (!bus.isConnected()) { qFatal("Cannot connect to the D-Bus session bus."); return; } QDBusMessage message = QDBusMessage::createMethodCall(BLUEZ_DBUS_SERVICE, BLUEZ_DBUS_PATH, "org.freedesktop.DBus.Properties", "Set"); QList<QVariant> args; args.append("org.bluez.Adapter1"); args.append("Powered"); args.append(true); message.setArguments(args); QDBusPendingReply<QVariantMap> reply = QDBusConnection::systemBus().asyncCall(message); reply.waitForFinished();
and this is my second attempt:
QDBusConnection bus = QDBusConnection::systemBus(); if (!bus.isConnected()) { qFatal("Cannot connect to the D-Bus session bus."); return; } QDBusInterface dbus_iface(BLUEZ_DBUS_SERVICE, BLUEZ_DBUS_PATH, "org.freedesktop.DBus.Properties", bus); dbus_iface.call("Set", QString("org.freedesktop.DBus.Properties"), QString("Powered"), true);
If I try this:
message = QDBusMessage::createMethodCall(BLUEZ_DBUS_SERVICE, BLUEZ_DBUS_PATH, "org.freedesktop.DBus.Properties", "Get"); args.append("org.bluez.Adapter1"); args.append("Powered"); reply = QDBusConnection::systemBus().asyncCall(message); reply.waitForFinished(); QVariantMap map = reply.value(); qDebug() << "Return value: " << map.begin().value();
I get the following output:
Return value: QVariant(Invalid)
Can anybody tell me what I am doing wrong here?
Regards
Martin -
I got it working. Problem was that instead of bool I had to use QVariant of QDBusVariant type. So the two functions look like this now:
These are the defines I've used:
#define BLUEZ_DBUS_SERVICE "org.bluez" #define BLUEZ_DBUS_PATH "/org/bluez/hci0" #define BLUEZ_DBUS_IF "org.bluez.Adapter1"
And here is the first implementationÖ
QDBusConnection bus = QDBusConnection::systemBus(); if (!bus.isConnected()) { qFatal("Cannot connect to the D-Bus session bus."); return; } QDBusMessage message = QDBusMessage::createMethodCall(BLUEZ_DBUS_SERVICE, BLUEZ_DBUS_PATH, "org.freedesktop.DBus.Properties", "Set"); QList<QVariant> arguments; arguments << BLUEZ_DBUS_IF << "Powered" << QVariant::fromValue(QDBusVariant(true)); message.setArguments(arguments); QDBusPendingReply<QVariantMap> reply = bus.asyncCall(message); reply.waitForFinished();
and here is the second possibility to implement the method call:
QDBusConnection bus = QDBusConnection::systemBus(); if (!bus.isConnected()) { qFatal("Cannot connect to the D-Bus session bus."); return; } QDBusInterface dbus_iface(BLUEZ_DBUS_SERVICE, BLUEZ_DBUS_PATH, "org.freedesktop.DBus.Properties", bus); if ( dbus_iface.isValid() ) { QDBusPendingReply<QVariantMap> reply = dbus_iface.asyncCall("Set", BLUEZ_DBUS_IF, "Powered", QVariant::fromValue(QDBusVariant(true))); reply.waitForFinished(); reply.error().message(); }
In case anybody wants to do the same thing there is also the possibility to set the "Powered" property directly:
QDBusInterface ifc( BLUEZ_DBUS_SERVICE, BLUEZ_DBUS_PATH, BLUEZ_DBUS_IF, QDBusConnection::systemBus()); if ( ifc.isValid() ) { ifc.setProperty("Powered", true); }