开发者

Connecting to the new Bluez HDP plugin using DBUS from QT/C++

I'm attempting to get readings from a device using the bluetooth Health Device Profile (specifically, an Nonin Onyx II 9560BT). Using this guide, I've been able to do so using python over dbus. Now I'm trying to port it over to C++, and as I'm already using QT in the application, I'm using the QT DBus bindings.

So far I've gotten to the following short program based on this API to test it:

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringL开发者_高级运维ist>
#include <QtDBus/QtDBus>

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    if (!QDBusConnection::sessionBus().isConnected()) {
        fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
                "To start it, run:\n"
                "\teval `dbus-launch --auto-syntax`\n");
        return 1;
    }

    QDBusInterface iface("org.bluez","/org/bluez","org.bluez.HealthManager",QDBusConnection::systemBus(),0);

    QVariantMap map;
    map.insert("DataType",ushort(1004));//same result with simply 1004
    map.insert("Role","Sink");
    map.insert("Description","HDP Test Manager"); //Optional
    //map.insert("ChannelType","Reliable");//Optional, same result with or without
    //QList<QVariant> argumentList;
    //argumentList.append(map);

    QDBusPendingReply<> r = iface.call("CreateApplication",map);

    qDebug() << r.reply();
    qDebug() << r.error();
    return 0;
}

As far as I can tell, the dict object taken by "CreateApplication" corresponds to an a{sv} which in QT corresponds to the QVariantMap. However, I keep getting this error:

QDBusMessage(type=Error, service="", error name="org.bluez.Error.InvalidArguments", error message="Invalid arguments in method call", signature="", contents=([]) )

Question: What am I doing wrong? Based on the guides at freedesktop.org, the qt docs and the mighty google, this is as far as I've gotten.

Thanks for any/all help!

/Keyz182


It works now. It seems that the ushort(0x1004) was getting cast by the QVariant to an int, and thus being picked up as a uint32 by the bluez code, which is not what was expected.

To fix it I did the following (there may be another way, but this worked for me).

I added a Metatype declaration for ushort, then registered it. then, created a QVariant containing the value, and used the QVariants convert method to set the metatype as a ushort (or uint16 when exposed to dbus).

#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtDBus/QtDBus>

Q_DECLARE_METATYPE(ushort); //Added this to declare ushort as a metatype

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    int ushorttype = qDBusRegisterMetaType<ushort>(); //Register the ushort metatype and get it's id

    if (!QDBusConnection::sessionBus().isConnected()) {
        fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
                "To start it, run:\n"
                "\teval `dbus-launch --auto-syntax`\n");
        return 1;
    }

    QDBusInterface iface("org.bluez","/org/bluez","org.bluez.HealthManager",QDBusConnection::systemBus(),0);

    QVariant dt(0x1004);
    dt.convert((QVariant::Type)ushorttype); //convert to the new type

    QVariantMap map;
    map.insert("DataType",dt);
    map.insert("Role","Sink");
    map.insert("Description","HDP Test Manager"); //Optional

    QDBusPendingReply<> r = iface.call("CreateApplication",map);

    qDebug() << r.isValid();
    qDebug() << r.reply();
    return 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜