DBus Introspection Example with Bluez

DBus Introspection Example with Bluez


The Qt DBus bindings are incredibly useful. Given a DBus introspection file like the following for Bluez ( http://www.bluez.org/ ) - Bluetooth for Linux, you can easily convert it to a Qt DBus interface class with the command:

qdbusxml2cpp -p headset.h:headset.cpp -i devicetypes.h xml/headset.xml

Bluez Introspection file for the Headset object:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"

"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">

<node>

<interface name="org.bluez.Headset">

<method name="Connect"/>

<method name="Disconnect"/>

<method name="IsConnected">

<arg type="b" direction="out"/>

</method>

<method name="IndicateCall"/>

<method name="CancelCall"/>

<method name="Play"/>

<method name="Stop"/>

<method name="IsPlaying">

<arg type="b" direction="out"/>

</method>

<method name="GetSpeakerGain">

<arg type="q" direction="out"/>

</method>

<method name="GetMicrophoneGain">

<arg type="q" direction="out"/>

</method>

<method name="SetSpeakerGain">

<arg type="q" direction="in"/>

</method>

<method name="SetMicrophoneGain">

<arg type="q" direction="in"/>

</method>

<method name="GetProperties">

<arg type="a{sv}" direction="out"/>

<annotation name="com.trolltech.QtDBus.QtTypeName.Out0" value="QVariantMap"/>

</method>

<method name="SetProperty">

<arg type="s" direction="in"/>

<arg type="v" direction="in"/>

</method>

<signal name="Connected"/>

<signal name="Disconnected"/>

<signal name="AnswerRequested"/>

<signal name="Stopped"/>

<signal name="Playing"/>

<signal name="SpeakerGainChanged">

<arg type="q"/> </signal>

<signal name="MicrophoneGainChanged">

<arg type="q"/>

</signal>

<signal name="CallTerminated"/>

<signal name="PropertyChanged">

<arg type="s"/>

<arg type="v"/>

</signal>

</interface>

</node>

Once generated, add the headset class source files to your project and instantiate a Headset with something like the following:

m_Headset = new OrgBluezHeadsetInterface(BLUEZ_SERVICE_NAME,pairedHeadsetPath.path(),QDBusConnection::systemBus(), this);

No you can easily do cool stuff like Connect and Play audio through a paired headset:

if (!m_Headset->isValid()) {

}

else {

// Now connect to the headset

QDBusPendingCall connectOperation = m_Headset->Connect();

connectOperation.waitForFinished();


// Determine if we are connected

bool checkConnection = m_Headset->IsConnected();

if ( checkConnection ) {


// Now play the audio

m_Headset->Play();


// Finally, send audio to the bluetooth headset

system(AUDIO_THROUGH_BLUETOOTH_HEADSET);


// Success

return 0;


}

else {


// Error

return -1;


}


}


}

Happy Coding!



ClassyBits 2016